diff --git a/src/app/api/invoices/actions.ts b/src/app/api/invoices/actions.ts index eaa78c6..8e7fdbc 100644 --- a/src/app/api/invoices/actions.ts +++ b/src/app/api/invoices/actions.ts @@ -17,6 +17,12 @@ export interface InvoiceResult { reminder: string; } +type InvoiceListError = { + [field in keyof NewInvoice]?: string; +} & { +message?: string; +}; + export type NewInvoice = { invoiceNo: string | undefined, projectCode: string | undefined, @@ -24,7 +30,11 @@ export type NewInvoice = { issuedAmount: number, receiptDate: Date receivedAmount: number +} & { + _isNew: boolean; + _error: InvoiceListError } + export type InvoiceType = { data: NewInvoice[] } @@ -151,4 +161,15 @@ export const deleteInvoice = async (id: number) => { revalidateTag("invoices"); return invoice; -}; \ No newline at end of file +}; + +export const createInvoices = async (data: any) => { + console.log(data) + const createInvoices = serverFetchString(`${BASE_API_URL}/invoices/create`, { + method: "Post", + body: JSON.stringify(data), + headers: { "Content-Type": "application/json" }, +}); + revalidateTag("invoices") + return createInvoices; +} \ No newline at end of file diff --git a/src/components/InvoiceSearch/CreateInvoiceModal.tsx b/src/components/InvoiceSearch/CreateInvoiceModal.tsx index 8b6c2f5..305a845 100644 --- a/src/components/InvoiceSearch/CreateInvoiceModal.tsx +++ b/src/components/InvoiceSearch/CreateInvoiceModal.tsx @@ -10,18 +10,22 @@ import { Card } from '@mui/material'; import { useTranslation } from 'react-i18next'; -import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; +import { FormProvider, SubmitHandler, useForm, useFormContext } from 'react-hook-form'; import { Check, Close } from "@mui/icons-material"; import InvoiceTable from './InvoiceTable'; import { ProjectResult } from '@/app/api/projects'; -import { InvoiceType, NewInvoice, PostInvoiceData } from '@/app/api/invoices/actions'; +import { createInvoices, InvoiceType, NewInvoice, PostInvoiceData } from '@/app/api/invoices/actions'; import dayjs from 'dayjs'; import { INPUT_DATE_FORMAT } from '@/app/utils/formatUtil'; +import { invoiceList } from '@/app/api/invoices'; +import { submitDialog, successDialog } from '../Swal/CustomAlerts'; +import { useRouter } from 'next/navigation'; interface Props { isOpen: boolean, - onClose: () => void - projects: ProjectResult[] + onClose: () => void, + projects: ProjectResult[], + invoices: invoiceList[] } const modalSx: SxProps= { @@ -35,24 +39,41 @@ const modalSx: SxProps= { bgcolor: 'background.paper', }; -const CreateInvoiceModal: React.FC = ({isOpen, onClose, projects}) => { +const CreateInvoiceModal: React.FC = ({isOpen, onClose, projects, invoices}) => { const { t } = useTranslation() const formProps = useForm(); + const router = useRouter(); const onSubmit = useCallback>( async ( data ) => { const _data = data.data + // console.log(_data) + if(_data.some(data => data._error !== undefined)){ + // console.log(data) + return + } // const postData: PostInvoiceData = _data.map(item => ({ const postData: any = _data.map(item => ({ invoiceNo: item.invoiceNo || '', - projectId: projects.find(p => p.code === item.projectCode)!.id, + // projectId: projects.find(p => p.code === item.projectCode)!.id, projectCode: item.projectCode || '', issuedAmount: item.issuedAmount || 0, - issueDate: dayjs(item.issuedDate).format(INPUT_DATE_FORMAT), - receiptDate: item.receiptDate || null, + issuedDate: dayjs(item.issuedDate).format(INPUT_DATE_FORMAT), + receiptDate: item.receiptDate ? dayjs(item.receiptDate).format(INPUT_DATE_FORMAT) : null, receivedAmount: item.receivedAmount || null, })) console.log(postData) + submitDialog(async () => { + const response = await createInvoices(postData) + console.log(response) + if (response === "OK") { + onClose() + successDialog(t("Submit Success"), t).then(() => { + router.replace("/invoice"); + }) + } + }, t) + } , []) @@ -77,7 +98,7 @@ const CreateInvoiceModal: React.FC = ({isOpen, onClose, projects}) => { marginBlock: 2, }} > - + + { + hasRowErrors && + + {t("There are errors!")} {selectedRow.find(row => row._error !== null)?._error?.message} + + } );