mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE-208,212): update PurchaseRequestForm schema and validation, improve credit term handling and reset logic for supplier changes
This commit is contained in:
@@ -17,7 +17,7 @@ type PurchaseRequestFormSchemaType = {
|
||||
label: string;
|
||||
} | null;
|
||||
location_id: number;
|
||||
credit_term: number | string | null;
|
||||
credit_term: number | string | undefined;
|
||||
notes: string | null;
|
||||
purchase_items: {
|
||||
warehouse?: {
|
||||
@@ -85,7 +85,7 @@ const PurchaseItemObjectSchema: Yup.ObjectSchema<PurchaseItemSchema> =
|
||||
.min(1, 'Produk wajib dipilih!')
|
||||
.typeError('Produk wajib dipilih!'),
|
||||
sub_qty: Yup.mixed<string | number>()
|
||||
.required('Sub Qty wajib diisi!')
|
||||
.required('Kuantitas wajib diisi!')
|
||||
.test(
|
||||
'is-valid-sub-qty',
|
||||
'Kuantitas harus berupa angka lebih dari 0!',
|
||||
@@ -134,21 +134,18 @@ export const PurchaseRequestFormSchema: Yup.ObjectSchema<PurchaseRequestFormSche
|
||||
return Boolean(value && value > 0);
|
||||
})
|
||||
.typeError('Lokasi wajib dipilih!'),
|
||||
credit_term: Yup.lazy((value, context) => {
|
||||
const supplier_id = context.parent.supplier_id;
|
||||
const hasSupplier = supplier_id && supplier_id > 0;
|
||||
|
||||
if (!hasSupplier) {
|
||||
return Yup.mixed<string | number>()
|
||||
.nullable()
|
||||
.default(null)
|
||||
.transform(() => null);
|
||||
}
|
||||
|
||||
return Yup.number()
|
||||
credit_term: Yup.mixed<string | number>().when('supplier_id', {
|
||||
is: (supplier_id: number) => supplier_id && supplier_id > 0,
|
||||
then: () =>
|
||||
Yup.number()
|
||||
.required('Jumlah hari jatuh tempo wajib diisi!')
|
||||
.min(1, 'Jumlah hari jatuh tempo minimal 1 hari!')
|
||||
.typeError('Jumlah hari jatuh tempo harus berupa angka!');
|
||||
.typeError('Jumlah hari jatuh tempo harus berupa angka!'),
|
||||
otherwise: () =>
|
||||
Yup.mixed<string | number>()
|
||||
.nullable()
|
||||
.default(null)
|
||||
.transform(() => null),
|
||||
}),
|
||||
notes: Yup.string().nullable().default(null),
|
||||
purchase_items: Yup.array()
|
||||
|
||||
@@ -85,7 +85,7 @@ const PurchaseRequestForm = ({
|
||||
| Record<string, string>
|
||||
| undefined;
|
||||
|
||||
if (!touchedItem || !errorItem) {
|
||||
if (!touchedItem) {
|
||||
return { isError: false, errorMessage: '' };
|
||||
}
|
||||
|
||||
@@ -377,6 +377,8 @@ const PurchaseRequestForm = ({
|
||||
formik.setFieldValue('credit_term', supplierData.due_date.toString());
|
||||
}
|
||||
} else {
|
||||
// Reset credit_term field and its touched state when supplier is cleared
|
||||
formik.setFieldTouched('credit_term', false);
|
||||
formik.setFieldValue('credit_term', '');
|
||||
}
|
||||
};
|
||||
@@ -787,7 +789,7 @@ const PurchaseRequestForm = ({
|
||||
/>
|
||||
</td>
|
||||
<td>
|
||||
<TextInput
|
||||
<NumberInput
|
||||
required
|
||||
name={`purchase_items.${idx}.sub_qty`}
|
||||
value={item.sub_qty || ''}
|
||||
@@ -799,9 +801,14 @@ const PurchaseRequestForm = ({
|
||||
)
|
||||
}
|
||||
onBlur={formik.handleBlur}
|
||||
type='number'
|
||||
placeholder='Masukkan kuantitas'
|
||||
readOnly={type === 'detail'}
|
||||
allowNegative={false}
|
||||
decimalScale={0}
|
||||
isError={getPurchaseItemError(idx, 'sub_qty').isError}
|
||||
errorMessage={
|
||||
getPurchaseItemError(idx, 'sub_qty').errorMessage
|
||||
}
|
||||
className={{
|
||||
wrapper: 'min-w-24',
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user