From 0463def200e2dddd6feafad22ce1c7f3f2c3f20b Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 29 Jul 2021 09:58:56 +0200 Subject: [PATCH] feat: request report form --- src/api/bills.ts | 52 ++++++++++++++++++++++++++++++++ src/api/index.ts | 56 ++--------------------------------- src/api/reports.ts | 22 ++++++++++++++ src/components/radioInput.tsx | 4 ++- src/lib/enums.ts | 5 +--- src/pages/ReportPage.tsx | 15 ++++++++-- src/pages/bill/index.tsx | 2 +- src/pages/bills.tsx | 2 +- src/pages/newBill/index.tsx | 2 +- src/types/report/index.ts | 18 +++++++++-- 10 files changed, 112 insertions(+), 66 deletions(-) create mode 100644 src/api/bills.ts create mode 100644 src/api/reports.ts diff --git a/src/api/bills.ts b/src/api/bills.ts new file mode 100644 index 0000000..694f3c0 --- /dev/null +++ b/src/api/bills.ts @@ -0,0 +1,52 @@ +import { client, Response } from '.'; +import { Bill, BillFormType, billFrom } from '../types/bill'; + +export const createBill = async (data: BillFormType) => { + try { + const { data: response } = await client.post('/bills', data); + return response; + } catch (error) { + console.error(error); + return 0; + } +}; + +export const updateBill = async (id: number, data: BillFormType): Promise> => { + try { + const { data: response } = await client.put(`/bills/${id}`, data); + return { data: response }; + } catch (error) { + console.error(error); + return { error }; + } +}; + +export const fetchOneBill = async (id: number): Promise> => { + try { + const { data } = await client.get(`/bills/${id}`); + return { data: billFrom(data) }; + } catch (error) { + console.error(error); + return { error }; + } +}; + +export const fetchAllBills = async () => { + try { + const { data } = await client.get('/bills'); + return data; + } catch (error) { + console.error(error); + return [] as Bill[]; + } +}; + +export const sendBillAsPDF = async (id: number) => { + try { + const { data } = await client.post(`/bills/${id}/send`); + return data; + } catch (error) { + console.error(error); + return false; + } +}; diff --git a/src/api/index.ts b/src/api/index.ts index 941d772..a02300e 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,60 +1,10 @@ import axios from 'axios'; -import { Bill, BillFormType, billFrom } from '../types/bill'; const BASE_URL = process.env.REACT_APP_API_URL; -const client = axios.create({ baseURL: BASE_URL }); -type Response = { +export const client = axios.create({ baseURL: BASE_URL }); + +export type Response = { data?: T; error?: any; }; - -export const createBill = async (data: BillFormType) => { - try { - const { data: response } = await client.post('/', data); - return response; - } catch (error) { - console.error(error); - return 0; - } -}; - -export const updateBill = async (id: number, data: BillFormType): Promise> => { - try { - const { data: response } = await client.put(`/${id}`, data); - return { data: response }; - } catch (error) { - console.error(error); - return { error }; - } -}; - -export const fetchOneBill = async (id: number): Promise> => { - try { - const { data } = await client.get(`/${id}`); - return { data: billFrom(data) }; - } catch (error) { - console.error(error); - return { error }; - } -}; - -export const fetchAllBills = async () => { - try { - const { data } = await client.get('/'); - return data; - } catch (error) { - console.error(error); - return [] as Bill[]; - } -}; - -export const sendBillAsPDF = async (id: number) => { - try { - const { data } = await client.post(`/${id}/send`); - return data; - } catch (error) { - console.error(error); - return false; - } -}; diff --git a/src/api/reports.ts b/src/api/reports.ts new file mode 100644 index 0000000..5bffb3f --- /dev/null +++ b/src/api/reports.ts @@ -0,0 +1,22 @@ +import { client, Response } from '.'; +import { Report, ReportFormType, ReportType } from '../types/report'; + +export const fetchReport = async ({ + type, + year, + month +}: ReportFormType): Promise> => { + const baseQueryURL = `/reports/?report_type=${type}&year=${year}`; + const queryURL = + type === ReportType.monthly && month + ? baseQueryURL.concat(`&month=${month + 1}`) + : baseQueryURL; + + try { + const { data } = await client.get(queryURL); + return { data }; + } catch (error) { + console.error(error); + return { error }; + } +}; diff --git a/src/components/radioInput.tsx b/src/components/radioInput.tsx index b560251..e0c4099 100644 --- a/src/components/radioInput.tsx +++ b/src/components/radioInput.tsx @@ -16,7 +16,9 @@ export const RadioInput = ({ control, name, label, options }: RadioInputProps) = {options.map((label, value) => ( - {label} + + {label} + ))} diff --git a/src/lib/enums.ts b/src/lib/enums.ts index eff80e3..821acac 100644 --- a/src/lib/enums.ts +++ b/src/lib/enums.ts @@ -1,4 +1 @@ -export const enumToList = (enumerable: any) => - Object.keys(enumerable) - .filter((v) => !parseInt(v)) - .slice(1); +export const enumToList = (enumerable: any) => Object.keys(enumerable).filter((v) => !parseInt(v)); diff --git a/src/pages/ReportPage.tsx b/src/pages/ReportPage.tsx index 4b7ad52..3654dc8 100644 --- a/src/pages/ReportPage.tsx +++ b/src/pages/ReportPage.tsx @@ -1,5 +1,7 @@ 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'; @@ -7,10 +9,16 @@ import { monthToList, ReportFormType, ReportType } from '../types/report'; function ReportPage() { // hooks - const { control, handleSubmit, watch } = useForm(); + const { control, handleSubmit, watch } = useForm({ + defaultValues: { + type: ReportType.monthly, + year: new Date().getFullYear(), + month: new Date().getMonth() + } + }); // Logic - const onFinish = handleSubmit((data) => console.log(data)); + const onFinish = handleSubmit((data) => fetchReport(data)); const isMonthlyReport = watch('type') === ReportType.monthly; @@ -24,6 +32,9 @@ function ReportPage() { label="Report type" options={['Monthly', 'Yearly']} /> + + + {isMonthlyReport && (