From 4e88e76538346c2df193655bb6c96695819568e9 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Mon, 17 Nov 2025 14:10:08 +0700 Subject: [PATCH] feat(FE-193): add existing documents link --- .../pages/expense/form/ExpenseRequestForm.tsx | 61 +++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/src/components/pages/expense/form/ExpenseRequestForm.tsx b/src/components/pages/expense/form/ExpenseRequestForm.tsx index 4268ca92..0fd68c88 100644 --- a/src/components/pages/expense/form/ExpenseRequestForm.tsx +++ b/src/components/pages/expense/form/ExpenseRequestForm.tsx @@ -6,6 +6,7 @@ import { useFormik } from 'formik'; import { toast } from 'react-hot-toast'; import { Icon } from '@iconify/react'; +import Link from 'next/link'; import Button from '@/components/Button'; import { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; @@ -151,20 +152,26 @@ const ExpenseRequestForm = ({ const locationChangeHandler = (val: OptionType | OptionType[] | null) => { formik.setFieldTouched('location', true); formik.setFieldValue('location', val); + + formik.setFieldValue('kandangs', []); + formik.setFieldValue('kandangExpenses', []); }; const kandangsChangeHandler = (kandangs: { id: number; name: string }[]) => { formik.setFieldTouched('kandangs', true); formik.setFieldValue('kandangs', kandangs); + const newKandangExpenses = [...(formik.values.kandangExpenses ?? [])]; + + // add new kandangExpenses kandangs.forEach((kandangItem) => { - const isKandangExistInKandangExpense = formik.values.kandangExpenses.find( + const isKandangExistInKandangExpense = newKandangExpenses.find( (kandangExpenseItem) => kandangExpenseItem.kandangId === kandangItem.id ); if (isKandangExistInKandangExpense) return; - formik.values.kandangExpenses.push({ + newKandangExpenses.push({ kandangId: kandangItem.id, expenses: [ { @@ -176,6 +183,24 @@ const ExpenseRequestForm = ({ ], }); }); + + // prune kandangExpenses + const kandangIds = new Set(kandangs.map((kandang) => kandang.id)); + const deletedKandangExpensesIdx: number[] = []; + + newKandangExpenses.forEach((kandangExpense, idx) => { + const isKandangExpenseValid = kandangIds.has(kandangExpense.kandangId); + + if (!isKandangExpenseValid) { + deletedKandangExpensesIdx.push(idx); + } + }); + + deletedKandangExpensesIdx.forEach((deletedKandangExpenseIdx) => { + newKandangExpenses.splice(deletedKandangExpenseIdx, 1); + }); + + formik.setFieldValue('kandangExpenses', newKandangExpenses); }; const vendorChangeHandler = (val: OptionType | OptionType[] | null) => { @@ -218,10 +243,6 @@ const ExpenseRequestForm = ({ formikSetValues(getExpenseFormInitialValues(initialValues)); }, [formikSetValues, getExpenseFormInitialValues, initialValues]); - useEffect(() => { - formik.setFieldValue('kandangs', undefined); - }, [formik.values.location]); - return ( <>
@@ -308,6 +329,34 @@ const ExpenseRequestForm = ({ }} /> + {formik.values.existing_documents && + formik.values.existing_documents.length > 0 && ( +
+
    + {formik.values.existing_documents.map( + (existingDocument, existingDocumentIdx) => ( +
  • + + {existingDocument.name}{' '} + + +
  • + ) + )} +
+
+ )} +