From 2b948f82715949fd804d4d6c7aaa6481d2bf38c7 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 15 Jul 2021 18:38:37 +0200 Subject: [PATCH] refactor: extract billfrom component --- src/components/billForm.tsx | 99 +++++++++++++++++++++++++++++++++++ src/pages/bill.tsx | 61 ---------------------- src/pages/bill/index.tsx | 89 +++++++++++++++++++++++++++++++ src/pages/home.tsx | 101 +++--------------------------------- 4 files changed, 196 insertions(+), 154 deletions(-) create mode 100644 src/components/billForm.tsx delete mode 100644 src/pages/bill.tsx create mode 100644 src/pages/bill/index.tsx diff --git a/src/components/billForm.tsx b/src/components/billForm.tsx new file mode 100644 index 0000000..7bd082e --- /dev/null +++ b/src/components/billForm.tsx @@ -0,0 +1,99 @@ +import { Button, Form, Input, Switch } from 'antd'; +import { BaseSyntheticEvent } from 'react'; +import { Control, Controller, UseFormRegister } from 'react-hook-form'; +import { BillFormType } from '../types/bill'; +import { InputSelect } from './inputSelect'; + +type Props = { + onFinish: (e?: BaseSyntheticEvent) => Promise; + control: Control; + register: UseFormRegister; +}; + +export function BillForm({ onFinish, control, register }: Props) { + return ( +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + const { value, ...props } = field; + return ( + + + + ); + }} + /> + + + + + + + + + + ); +} diff --git a/src/pages/bill.tsx b/src/pages/bill.tsx deleted file mode 100644 index df32221..0000000 --- a/src/pages/bill.tsx +++ /dev/null @@ -1,61 +0,0 @@ -import { Button, message, Space, Typography } from 'antd'; -import { useEffect, useState } from 'react'; -import { useParams } from 'react-router-dom'; -import { fetchOneBill, sendBillAsPDF } from '../api'; -import { withLayout } from '../layouts/main'; -import { Bill } from '../types/bill'; -import NotFoundPage from './notFound'; - -export type QueryParams = { id: string }; - -const BillPage = () => { - // Hooks - const { id } = useParams(); - const [sent, setSent] = useState(false); - - // Local State - const [bill, setBill] = useState({} as Bill); - - // Effects - useEffect(() => { - const loadBIllById = async (id: string) => { - const billID = parseInt(id); - const { data: loadedBill, error } = await fetchOneBill(billID); - if (!loadedBill || error) { - return ; - } - setBill(() => loadedBill); - return; - }; - - loadBIllById(id); - }, [id]); - - // Logic - const handleSendPDF = (id: number) => { - sendBillAsPDF(id); - message.success('The bill will be sent to the customer'); - setSent(() => true); - }; - - return ( -
- Facture #VFNI{`${bill.id}`.padStart(4, '0')} - - - - - - {bill?.name} - {bill?.price} € - - from {bill?.start} to {bill?.end} - - -
- ); -}; - -export default withLayout(BillPage); diff --git a/src/pages/bill/index.tsx b/src/pages/bill/index.tsx new file mode 100644 index 0000000..1edcb7a --- /dev/null +++ b/src/pages/bill/index.tsx @@ -0,0 +1,89 @@ +import { Button, Col, Divider, message, PageHeader, Space, Typography } from 'antd'; +import { useEffect, useState } from 'react'; +import { useHistory, useParams } from 'react-router-dom'; +import { fetchOneBill, sendBillAsPDF } from '../../api'; +import { withLayout } from '../../layouts/main'; +import { Bill } from '../../types/bill'; +import NotFoundPage from '../notFound'; +import { BillSent } from './billSent'; +import { EditBillForm } from './editBillForm'; + +export type QueryParams = { id: string }; + +const BillPage = () => { + // Hooks + const { id } = useParams(); + const history = useHistory(); + + // Local State + const [sent, setSent] = useState(false); + const [edit, setEdit] = useState(false); + const [bill, setBill] = useState({} as Bill); + + // Effects + useEffect(() => { + const loadBIllById = async (id: string) => { + const billID = parseInt(id); + const { data: loadedBill, error } = await fetchOneBill(billID); + if (!loadedBill || error) { + return ; + } + setBill(() => loadedBill); + return; + }; + + loadBIllById(id); + }, [id]); + + // Logic + const handleSendPDF = (id: number) => { + sendBillAsPDF(id); + message.success('The bill will be sent to the customer'); + setSent(() => true); + }; + + const content = edit ? ( + + ) : sent ? ( + + ) : ( + + {bill?.name} + {bill?.price} € + + from {bill?.start} to {bill?.end} + + + ); + + return ( + <> + history.goBack()} + title={`Facture #VFNI${bill.id?.toString().padStart(4, '0')}`} + subTitle={bill.name} + /> + + {content} + + + + + + + + + ); +}; + +export default withLayout(BillPage); diff --git a/src/pages/home.tsx b/src/pages/home.tsx index 9377e5d..f07763d 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -1,14 +1,13 @@ -import { Button, Form, Input, Switch } from 'antd'; -import { Controller, useForm } from 'react-hook-form'; +import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom'; import { createBill } from '../api'; -import { InputSelect } from '../components/inputSelect'; +import { BillForm } from '../components/billForm'; import { withLayout } from '../layouts/main'; -import { BillForm } from '../types/bill'; +import { BillFormType } from '../types/bill'; const HomePage = () => { // Hooks - const { register, handleSubmit, control } = useForm(); + const { register, handleSubmit, control } = useForm(); const history = useHistory(); // Logic @@ -18,95 +17,11 @@ const HomePage = () => { }); return ( -
-
-

Create a new bill

+ <> +

Create a new bill

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - const { value, ...props } = field; - return ( - - - - ); - }} - /> - - - - - - - - - -
-
+ + ); };