refactor(FE-208,212): update PurchaseOrderForm and PurchaseOrderStaffApprovalForm for improved validation and dynamic item handling

This commit is contained in:
rstubryan
2025-11-18 21:00:46 +07:00
parent 1b90d657ff
commit 0c79e86736
2 changed files with 70 additions and 48 deletions
@@ -4,11 +4,17 @@ import { Purchase } from '@/types/api/purchase/purchase';
type PurchaseRequestStaffApprovalFormSchemaType = { type PurchaseRequestStaffApprovalFormSchemaType = {
notes: string | null; notes: string | null;
items: { items: {
purchase_item?: { product?: {
value: number; value: number;
label: string; label: string;
} | null; } | null;
purchase_item_id: number; product_id: number;
warehouse?: {
value: number;
label: string;
} | null;
warehouse_id: number;
qty: number;
price: number | string; price: number | string;
total_price: number | string; total_price: number | string;
}[]; }[];
@@ -47,11 +53,17 @@ type PurchaseRequestAcceptApprovalFormSchemaType = {
}; };
export type PurchaseStaffApprovalItemSchema = { export type PurchaseStaffApprovalItemSchema = {
purchase_item?: { product?: {
value: number; value: number;
label: string; label: string;
} | null; } | null;
purchase_item_id: number; product_id: number;
warehouse?: {
value: number;
label: string;
} | null;
warehouse_id: number;
qty: number;
price: number | string; price: number | string;
total_price: number | string; total_price: number | string;
}; };
@@ -87,49 +99,35 @@ export type PurchaseDeleteItemsSchema = {
const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> = const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> =
Yup.object({ Yup.object({
purchase_item: Yup.object({ product: Yup.object({
value: Yup.number().min(1).required(), value: Yup.number().min(1).required(),
label: Yup.string().required(), label: Yup.string().required(),
}) }).nullable(),
.nullable() product_id: Yup.number()
.optional(), .required('Produk wajib diisi!')
purchase_item_id: Yup.number() .min(1, 'Produk wajib diisi!')
.min(1, 'Purchase item is required!') .typeError('Produk wajib diisi!'),
.required('Purchase item is required!') warehouse: Yup.object({
.test( value: Yup.number().min(1).required(),
'is-valid-purchase-item-id', label: Yup.string().required(),
'Purchase item ID must be valid!', }).nullable(),
function (value) { warehouse_id: Yup.number()
return Boolean(value && value > 0); .required('Gudang wajib diisi!')
} .min(1, 'Gudang wajib diisi!')
) .typeError('Gudang wajib diisi!'),
.typeError('Purchase item must be selected!'), qty: Yup.number()
price: Yup.mixed<string | number>() .required('Jumlah wajib diisi!')
.min(1, 'Jumlah harus berupa angka lebih dari 0!')
.typeError('Jumlah harus berupa angka lebih dari 0!'),
price: Yup.number()
.required('Harga wajib diisi!') .required('Harga wajib diisi!')
.test( .min(0, 'Harga harus berupa angka lebih dari atau sama dengan 0!')
'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 lebih dari atau sama dengan 0!'), .typeError('Harga harus berupa angka lebih dari atau sama dengan 0!'),
total_price: Yup.mixed<string | number>() total_price: Yup.number()
.required('Total harga wajib diisi!') .required('Total harga wajib diisi!')
.test( .min(0, 'Total harga harus berupa angka lebih dari atau sama dengan 0!')
'is-valid-total-price', .typeError(
'Total harga harus berupa angka lebih dari atau sama dengan 0!', '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;
}
), ),
}); });
@@ -273,7 +271,11 @@ export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaff
notes: '', notes: '',
items: [ items: [
{ {
purchase_item_id: 0, product: null,
product_id: 0,
warehouse: null,
warehouse_id: 0,
qty: 0,
price: '', price: '',
total_price: '', total_price: '',
}, },
@@ -287,13 +289,21 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = (
notes: purchase?.notes ?? null, notes: purchase?.notes ?? null,
items: purchase?.items items: purchase?.items
? purchase.items.map((item) => ({ ? purchase.items.map((item) => ({
purchase_item_id: item.id, product: null,
price: '', product_id: item.product_id,
total_price: '', warehouse: null,
warehouse_id: item.warehouse.id,
qty: item.qty,
price: item.price,
total_price: item.total_price,
})) }))
: [ : [
{ {
purchase_item_id: 0, product: null,
product_id: 0,
warehouse: null,
warehouse_id: 0,
qty: 0,
price: '', price: '',
total_price: '', total_price: '',
}, },
+13 -1
View File
@@ -76,12 +76,24 @@ export type CreatePurchaseRequestPayload = {
export type CreateStaffApprovalRequestPayload = { export type CreateStaffApprovalRequestPayload = {
notes?: string | null; notes?: string | null;
items: { items: {
purchase_item_id: number; product_id: number;
warehouse_id: number;
qty: number;
price: number; price: number;
total_price: number; total_price: number;
}[]; }[];
}; };
export type UpdateStaffApprovalRequestPayload = {
items: {
purchase_item_id: number;
qty: number;
price: number;
total_price: number;
}[];
'notes?': string | null;
};
export type CreateManagerApprovalRequestPayload = { export type CreateManagerApprovalRequestPayload = {
notes?: string | null; notes?: string | null;
}; };