From a24fb57d141d886bb050ead70a675123bd646bd2 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 29 Jul 2021 10:29:48 +0200 Subject: [PATCH] feat: display report --- src/Router.tsx | 2 +- src/api/reports.ts | 8 ++++- src/pages/ReportPage.tsx | 55 ------------------------------- src/pages/report/index.tsx | 47 ++++++++++++++++++++++++++ src/pages/report/reportForm.tsx | 41 +++++++++++++++++++++++ src/pages/report/reportResult.tsx | 45 +++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 57 deletions(-) delete mode 100644 src/pages/ReportPage.tsx create mode 100644 src/pages/report/index.tsx create mode 100644 src/pages/report/reportForm.tsx create mode 100644 src/pages/report/reportResult.tsx diff --git a/src/Router.tsx b/src/Router.tsx index 2dd00e9..62242b4 100644 --- a/src/Router.tsx +++ b/src/Router.tsx @@ -4,7 +4,7 @@ import BillsPage from './pages/bills'; import HomePage from './pages/home'; import NewBillPage from './pages/newBill'; import NotFoundPage from './pages/notFound'; -import ReportPage from './pages/ReportPage'; +import ReportPage from './pages/report'; type RouteConfig = { path: string; diff --git a/src/api/reports.ts b/src/api/reports.ts index 5bffb3f..1fb9925 100644 --- a/src/api/reports.ts +++ b/src/api/reports.ts @@ -14,7 +14,13 @@ export const fetchReport = async ({ try { const { data } = await client.get(queryURL); - return { data }; + return { + data: { + // @ts-ignore + type: data.report_type, + ...data + } + }; } catch (error) { console.error(error); return { error }; diff --git a/src/pages/ReportPage.tsx b/src/pages/ReportPage.tsx deleted file mode 100644 index 3654dc8..0000000 --- a/src/pages/ReportPage.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import { Button, Form } from 'antd'; -import { useForm } from 'react-hook-form'; -import { fetchReport } from '../api/reports'; -import { FormInput } from '../components/formInput'; -import { InputSelect } from '../components/inputSelect'; -import { RadioInput } from '../components/radioInput'; -import { withLayout } from '../layouts/main'; -import { monthToList, ReportFormType, ReportType } from '../types/report'; - -function ReportPage() { - // hooks - const { control, handleSubmit, watch } = useForm({ - defaultValues: { - type: ReportType.monthly, - year: new Date().getFullYear(), - month: new Date().getMonth() - } - }); - - // Logic - const onFinish = handleSubmit((data) => fetchReport(data)); - - const isMonthlyReport = watch('type') === ReportType.monthly; - - return ( - <> -

Reports

-
- - - - - {isMonthlyReport && ( - - )} - - - - ); -} - -export default withLayout(ReportPage); diff --git a/src/pages/report/index.tsx b/src/pages/report/index.tsx new file mode 100644 index 0000000..2af0d87 --- /dev/null +++ b/src/pages/report/index.tsx @@ -0,0 +1,47 @@ +import { Divider } from 'antd'; +import { useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { fetchReport } from '../../api/reports'; +import { withLayout } from '../../layouts/main'; +import { Report, ReportFormType, ReportType } from '../../types/report'; +import { ReportResult } from './reportResult'; +import { ReportForm } from './reportForm'; + +function ReportPage() { + // hooks + const { control, handleSubmit, watch } = useForm({ + defaultValues: { + type: ReportType.monthly, + year: new Date().getFullYear(), + month: new Date().getMonth() + } + }); + + // Local state + const [reportFetched, setReportFetched] = useState(false); + const [report, setReport] = useState({} as Report); + + // Logic + const onFinish = handleSubmit(async (data) => { + const { data: content } = await fetchReport(data); + if (content) { + setReport(() => content); + setReportFetched(() => true); + return; + } + setReportFetched(() => false); + }); + + const isMonthlyReport = watch('type') === ReportType.monthly; + + return ( + <> +

Reports

+ {reportFetched && } + + + + ); +} + +export default withLayout(ReportPage); diff --git a/src/pages/report/reportForm.tsx b/src/pages/report/reportForm.tsx new file mode 100644 index 0000000..2f187c7 --- /dev/null +++ b/src/pages/report/reportForm.tsx @@ -0,0 +1,41 @@ +import { Button, Form } from 'antd'; +import { BaseSyntheticEvent } from 'react'; +import { Control } from 'react-hook-form'; +import { FormInput } from '../../components/formInput'; +import { InputSelect } from '../../components/inputSelect'; +import { RadioInput } from '../../components/radioInput'; +import { monthToList, ReportFormType } from '../../types/report'; + +type Props = { + onFinish: (e?: BaseSyntheticEvent) => Promise; + control: Control; + monthlyReport: boolean; +}; + +export function ReportForm({ control, monthlyReport, onFinish }: Props) { + return ( +
+ + + + + {monthlyReport && ( + + )} + + + ); +} diff --git a/src/pages/report/reportResult.tsx b/src/pages/report/reportResult.tsx new file mode 100644 index 0000000..8433067 --- /dev/null +++ b/src/pages/report/reportResult.tsx @@ -0,0 +1,45 @@ +import { Card, Col, Divider, List, Row, Statistic } from 'antd'; +import { Link } from 'react-router-dom'; +import { Report } from '../../types/report'; + +type Props = { + content: Report; +}; + +export function ReportResult({ content }: Props) { + return ( + <> + +

{content.type.toUpperCase()} report

+ + + + + + + + + + + + +

Bills

+ + ( + + + +

{bill.name}

+
+
+ + )} + /> +
+ + ); +}