From 6ab5c8afe98d77a29746b4ddc679e00001f6121a Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 16 Jul 2021 12:17:37 +0200 Subject: [PATCH] feat: populate edit form with existing data --- src/components/billForm.tsx | 43 ++++++++++++++++----------------- src/components/formInput.tsx | 32 +++++++++++++++++++++++++ src/pages/bill/index.tsx | 46 ++++++++++-------------------------- src/pages/home.tsx | 4 ++-- 4 files changed, 68 insertions(+), 57 deletions(-) create mode 100644 src/components/formInput.tsx diff --git a/src/components/billForm.tsx b/src/components/billForm.tsx index 7bd082e..ad8c813 100644 --- a/src/components/billForm.tsx +++ b/src/components/billForm.tsx @@ -1,37 +1,32 @@ -import { Button, Form, Input, Switch } from 'antd'; +import { Button, Form, Switch } from 'antd'; import { BaseSyntheticEvent } from 'react'; -import { Control, Controller, UseFormRegister } from 'react-hook-form'; +import { Control, Controller } from 'react-hook-form'; import { BillFormType } from '../types/bill'; +import { FormInput } from './formInput'; import { InputSelect } from './inputSelect'; type Props = { onFinish: (e?: BaseSyntheticEvent) => Promise; control: Control; - register: UseFormRegister; }; -export function BillForm({ onFinish, control, register }: Props) { +export function BillForm({ onFinish, control }: Props) { return (
- - - + - - - + - - - + - - - + - - - + - - - + ( + + + + )} + /> + ); +} diff --git a/src/pages/bill/index.tsx b/src/pages/bill/index.tsx index dc8508d..0bee6e4 100644 --- a/src/pages/bill/index.tsx +++ b/src/pages/bill/index.tsx @@ -5,8 +5,7 @@ import { useHistory, useParams } from 'react-router-dom'; import { createBill, fetchOneBill, sendBillAsPDF } from '../../api'; import { BillForm } from '../../components/billForm'; import { withLayout } from '../../layouts/main'; -import { Bill } from '../../types/bill'; -import NotFoundPage from '../notFound'; +import { Bill, BillFormType } from '../../types/bill'; import { BillSent } from './billSent'; export type QueryParams = { id: string }; @@ -15,24 +14,7 @@ const BillPage = () => { // Hooks const { id } = useParams(); const history = useHistory(); - - const defaultValues = { - customers: 0, - start: '', - end: '', - name: '', - paymentMethod: 0, - paymentStatus: 0, - phoneNumber: '', - platform: 0, - price: 0, - room: 0, - taxes: false - }; - - const { register, handleSubmit, control, reset } = useForm({ - defaultValues - }); + const { handleSubmit, control, reset } = useForm(); // Local State const [sent, setSent] = useState(false); @@ -41,18 +23,16 @@ const BillPage = () => { // Effects useEffect(() => { - const loadBIllById = async (id: string) => { + (async (id: string) => { const billID = parseInt(id); const { data: loadedBill, error } = await fetchOneBill(billID); - if (!loadedBill || error) { - return ; + if (loadedBill && error) { + setBill(() => loadedBill); + const { id, ...values } = loadedBill; + reset(values); } - setBill(() => loadedBill); - return; - }; - - loadBIllById(id); - }, [id]); + })(id); + }, [id, reset]); // Logic const handleSendPDF = (id: number) => { @@ -66,15 +46,15 @@ const BillPage = () => { }); const content = edit ? ( - + ) : sent ? ( ) : ( - {bill?.name} - {bill?.price} € + {bill.name} + {bill.price} € - from {bill?.start} to {bill?.end} + from {bill.start} to {bill.end} ); diff --git a/src/pages/home.tsx b/src/pages/home.tsx index f07763d..1a85752 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -7,7 +7,7 @@ import { BillFormType } from '../types/bill'; const HomePage = () => { // Hooks - const { register, handleSubmit, control } = useForm(); + const { handleSubmit, control } = useForm(); const history = useHistory(); // Logic @@ -20,7 +20,7 @@ const HomePage = () => { <>

Create a new bill

- + ); };