From 3d468d950783faeace4bb988f310bb26b2c2e201 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Tue, 18 Nov 2025 22:13:14 +0700 Subject: [PATCH] refactor(FE-208,213): simplify PurchaseOrderForm and PurchaseOrderStaffApprovalForm by removing unused product and warehouse fields and enhancing price validation --- .../form/order/PurchaseOrderForm.schema.ts | 64 ++++++++----------- .../order/PurchaseOrderStaffApprovalForm.tsx | 30 +++------ 2 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts b/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts index d3e6ff17..209671cf 100644 --- a/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts +++ b/src/components/pages/purchase/form/order/PurchaseOrderForm.schema.ts @@ -4,15 +4,7 @@ import { Purchase } from '@/types/api/purchase/purchase'; type PurchaseRequestStaffApprovalFormSchemaType = { notes: string | null; items: { - product?: { - value: number; - label: string; - } | null; product_id: number; - warehouse?: { - value: number; - label: string; - } | null; warehouse_id: number; qty: number; price: number | string; @@ -53,15 +45,7 @@ type PurchaseRequestAcceptApprovalFormSchemaType = { }; export type PurchaseStaffApprovalItemSchema = { - product?: { - value: number; - label: string; - } | null; product_id: number; - warehouse?: { - value: number; - label: string; - } | null; warehouse_id: number; qty: number; price: number | string; @@ -99,18 +83,10 @@ export type PurchaseDeleteItemsSchema = { const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema = Yup.object({ - product: Yup.object({ - value: Yup.number().min(1).required(), - label: Yup.string().required(), - }).nullable(), product_id: Yup.number() .required('Produk wajib diisi!') .min(1, 'Produk wajib diisi!') .typeError('Produk wajib diisi!'), - warehouse: Yup.object({ - value: Yup.number().min(1).required(), - label: Yup.string().required(), - }).nullable(), warehouse_id: Yup.number() .required('Gudang wajib diisi!') .min(1, 'Gudang wajib diisi!') @@ -119,16 +95,34 @@ const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema() .required('Harga wajib diisi!') - .min(0, 'Harga harus berupa angka lebih dari atau sama dengan 0!') - .typeError('Harga harus berupa angka lebih dari atau sama dengan 0!'), - total_price: Yup.number() + .test( + 'is-valid-price', + 'Harga harus berupa angka lebih dari atau sama dengan 0!', + function (value) { + if (value === '' || value === null || value === undefined) + return false; + const numValue = + typeof value === 'string' ? parseFloat(value) : value; + return !isNaN(numValue) && numValue >= 0; + } + ) + .typeError('Harga harus berupa angka!'), + total_price: Yup.mixed() .required('Total harga wajib diisi!') - .min(0, 'Total harga harus berupa angka lebih dari atau sama dengan 0!') - .typeError( - 'Total harga harus berupa angka lebih dari atau sama dengan 0!' - ), + .test( + 'is-valid-total-price', + 'Total harga harus berupa angka lebih dari atau sama dengan 0!', + function (value) { + if (value === '' || value === null || value === undefined) + return false; + const numValue = + typeof value === 'string' ? parseFloat(value) : value; + return !isNaN(numValue) && numValue >= 0; + } + ) + .typeError('Total harga harus berupa angka!'), }); const PurchaseManagerApprovalObjectSchema: Yup.ObjectSchema = @@ -271,9 +265,7 @@ export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaff notes: '', items: [ { - product: null, product_id: 0, - warehouse: null, warehouse_id: 0, qty: 0, price: '', @@ -289,9 +281,7 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = ( notes: purchase?.notes ?? null, items: purchase?.items ? purchase.items.map((item) => ({ - product: null, product_id: item.product_id, - warehouse: null, warehouse_id: item.warehouse.id, qty: item.qty, price: item.price, @@ -299,9 +289,7 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = ( })) : [ { - product: null, product_id: 0, - warehouse: null, warehouse_id: 0, qty: 0, price: '', diff --git a/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx b/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx index 4a9086a8..9a61448b 100644 --- a/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx +++ b/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx @@ -8,7 +8,6 @@ import { useSearchParams } from 'next/navigation'; import Button from '@/components/Button'; import TextInput from '@/components/input/TextInput'; import NumberInput from '@/components/input/NumberInput'; -import { OptionType } from '@/components/input/SelectInput'; import { PurchaseRequestStaffApprovalFormDefaultValues, @@ -37,22 +36,6 @@ const PurchaseOrderStaffApprovalForm = ({ const [purchaseOrderFormErrorMessage, setPurchaseOrderFormErrorMessage] = useState(''); - // ===== TYPE DEFINITIONS ===== - interface PurchaseItemOptionType extends OptionType { - id: number; - quantity: number; - product: { - name: string; - type?: string; - uom?: { - name: string; - }; - }; - warehouse: { - name: string; - }; - } - // ===== UTILITY FUNCTIONS ===== const getPurchaseItemError = ( idx: number, @@ -138,7 +121,9 @@ const PurchaseOrderStaffApprovalForm = ({ items: purchaseItems.map((purchaseItem, idx) => { const formItem = values.items?.[idx]; return { - purchase_item_id: purchaseItem.value, + product_id: purchaseItem.product_id || 0, + warehouse_id: purchaseItem.warehouse_id || 0, + qty: purchaseItem.quantity || 0, price: typeof formItem?.price === 'string' ? parseFloat(formItem.price) || 0 @@ -170,9 +155,11 @@ const PurchaseOrderStaffApprovalForm = ({ if (initialValues?.items) { return initialValues.items.map((item) => ({ value: item.id, - label: `${item.product.name} ${item.sub_qty}`, + label: item.product.name, id: item.id, quantity: item.sub_qty, + product_id: item.product_id, + warehouse_id: item.warehouse.id, product: { name: item.product.name, product_category: item.product.product_category, @@ -187,11 +174,12 @@ const PurchaseOrderStaffApprovalForm = ({ return []; }, [initialValues?.items]); - // Ensure form values are properly set when purchaseItems change useEffect(() => { if (purchaseItems.length > 0) { const updatedItems = purchaseItems.map((purchaseItem) => ({ - purchase_item_id: purchaseItem.value, + product_id: purchaseItem.product_id || 0, + warehouse_id: purchaseItem.warehouse_id || 0, + qty: purchaseItem.quantity || 0, price: '', total_price: '', }));