mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-149): create getTransferToLayingFormInitialValues and getFilledTransferToLayingFormInitialValues helper function
This commit is contained in:
+156
-2
@@ -1,4 +1,7 @@
|
||||
import * as Yup from 'yup';
|
||||
import { TransferToLaying } from '@/types/api/production/transfer-to-laying';
|
||||
import { TransferToLayingApi } from '@/services/api/production/transfer-to-laying';
|
||||
import { formatDate } from '@/lib/helper';
|
||||
|
||||
type TransferToLayingFormSchemaType = {
|
||||
transfer_date?: string;
|
||||
@@ -14,7 +17,7 @@ type TransferToLayingFormSchemaType = {
|
||||
totalQuantity?: number;
|
||||
maxTotalQuantity?: number; // original cap (hidden), helper
|
||||
|
||||
kandangs: {
|
||||
flockSourceKandangs: {
|
||||
kandang: {
|
||||
value: number;
|
||||
label: string;
|
||||
@@ -22,6 +25,16 @@ type TransferToLayingFormSchemaType = {
|
||||
quantity: number | string; // editable
|
||||
maxQuantity?: number; // original cap (hidden), helper
|
||||
}[];
|
||||
|
||||
flockDestinationKandangs: {
|
||||
kandang: {
|
||||
value: number;
|
||||
label: string;
|
||||
};
|
||||
quantity: number | string; // editable
|
||||
maxQuantity?: number; // original cap (hidden), helper
|
||||
}[];
|
||||
|
||||
reason?: string;
|
||||
};
|
||||
|
||||
@@ -51,7 +64,29 @@ export const TransferToLayingFormSchema: Yup.ObjectSchema<TransferToLayingFormSc
|
||||
.min(1, 'Jumlah transfer minimal 1')
|
||||
.required('Jumlah transfer wajib diisi!'),
|
||||
|
||||
kandangs: Yup.array()
|
||||
flockSourceKandangs: Yup.array()
|
||||
.of(
|
||||
Yup.object({
|
||||
kandang: Yup.object({
|
||||
value: Yup.number().min(1).required(),
|
||||
label: Yup.string().required(),
|
||||
}).required('Kandang wajib diisi!'),
|
||||
|
||||
quantity: Yup.number()
|
||||
.min(0, 'Kuantitas minimal 0!')
|
||||
.max(
|
||||
Yup.ref('maxQuantity'),
|
||||
({ max }) => `Kuantitas maksimal ${max}!`
|
||||
)
|
||||
.required('Kuantitas wajib diisi!'),
|
||||
|
||||
maxQuantity: Yup.number().min(1).required(), // internal helper field
|
||||
})
|
||||
)
|
||||
.min(1, 'Minimal 1 kandang terisi!')
|
||||
.required('Kandang wajib diisi!'),
|
||||
|
||||
flockDestinationKandangs: Yup.array()
|
||||
.of(
|
||||
Yup.object({
|
||||
kandang: Yup.object({
|
||||
@@ -81,3 +116,122 @@ export const UpdateTransferToLayingFormSchema = TransferToLayingFormSchema;
|
||||
export type TransferToLayingFormValues = Yup.InferType<
|
||||
typeof TransferToLayingFormSchema
|
||||
>;
|
||||
|
||||
export const getTransferToLayingFormInitialValues = (
|
||||
initialValues?: TransferToLaying
|
||||
): TransferToLayingFormValues => {
|
||||
return {
|
||||
transfer_date: initialValues?.transfer_date
|
||||
? formatDate(initialValues.transfer_date, 'YYYY-MM-DD')
|
||||
: '',
|
||||
flockSource: initialValues?.from_project_flock
|
||||
? {
|
||||
value: initialValues?.from_project_flock.id,
|
||||
label: initialValues?.from_project_flock.flock_name,
|
||||
}
|
||||
: undefined,
|
||||
flockDestination: initialValues?.to_project_flock
|
||||
? {
|
||||
value: initialValues?.to_project_flock.id,
|
||||
label: initialValues?.to_project_flock.flock_name,
|
||||
}
|
||||
: undefined,
|
||||
totalQuantity:
|
||||
initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? undefined,
|
||||
|
||||
flockSourceKandangs: initialValues?.sources
|
||||
? initialValues.sources.map((sourceKandang) => ({
|
||||
kandang: {
|
||||
value: sourceKandang.source_project_flock_kandang.kandang.id,
|
||||
label: sourceKandang.source_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: sourceKandang.qty,
|
||||
}))
|
||||
: [],
|
||||
|
||||
flockDestinationKandangs: initialValues?.targets
|
||||
? initialValues.targets.map((targetKandang) => ({
|
||||
kandang: {
|
||||
value: targetKandang.target_project_flock_kandang.kandang.id,
|
||||
label: targetKandang.target_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: targetKandang.qty,
|
||||
}))
|
||||
: [],
|
||||
|
||||
reason: initialValues?.notes ?? undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export const getFilledTransferToLayingFormInitialValues = async (
|
||||
initialValues?: TransferToLaying
|
||||
): Promise<TransferToLayingFormValues> => {
|
||||
const mappedFlockSourceKandangsAvailableQty =
|
||||
await TransferToLayingApi.getMappedFlockKandangsAvailability(
|
||||
initialValues?.from_project_flock.id as number
|
||||
);
|
||||
|
||||
const formattedFlockSourceKandangs = initialValues?.sources
|
||||
? initialValues.sources.map((sourceKandang) => ({
|
||||
kandang: {
|
||||
value: sourceKandang.source_project_flock_kandang.kandang.id,
|
||||
label: sourceKandang.source_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: sourceKandang.qty,
|
||||
maxQuantity:
|
||||
(mappedFlockSourceKandangsAvailableQty &&
|
||||
mappedFlockSourceKandangsAvailableQty[
|
||||
sourceKandang.source_project_flock_kandang.id
|
||||
].available_qty) ??
|
||||
0,
|
||||
}))
|
||||
: [];
|
||||
|
||||
let maxTotalQuantity = 0;
|
||||
formattedFlockSourceKandangs.forEach((item) => {
|
||||
maxTotalQuantity += item.maxQuantity;
|
||||
});
|
||||
|
||||
return {
|
||||
transfer_date: initialValues?.transfer_date
|
||||
? formatDate(initialValues.transfer_date, 'YYYY-MM-DD')
|
||||
: '',
|
||||
flockSource: initialValues?.from_project_flock
|
||||
? {
|
||||
value: initialValues?.from_project_flock.id,
|
||||
label: initialValues?.from_project_flock.flock_name,
|
||||
}
|
||||
: undefined,
|
||||
flockDestination: initialValues?.to_project_flock
|
||||
? {
|
||||
value: initialValues?.to_project_flock.id,
|
||||
label: initialValues?.to_project_flock.flock_name,
|
||||
}
|
||||
: undefined,
|
||||
totalQuantity:
|
||||
initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? undefined,
|
||||
maxTotalQuantity: maxTotalQuantity,
|
||||
|
||||
flockSourceKandangs: formattedFlockSourceKandangs,
|
||||
|
||||
flockDestinationKandangs: initialValues?.targets
|
||||
? initialValues.targets.map((targetKandang) => ({
|
||||
kandang: {
|
||||
value: targetKandang.target_project_flock_kandang.kandang.id,
|
||||
label: targetKandang.target_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: targetKandang.qty,
|
||||
|
||||
// maxQuantity:
|
||||
// targetKandang.target_project_flock_kandang.kandang.capacity,
|
||||
|
||||
// TODO: integrate this to real API kandang capacity
|
||||
maxQuantity:
|
||||
targetKandang.target_project_flock_kandang.kandang.capacity ??
|
||||
Infinity,
|
||||
}))
|
||||
: [],
|
||||
|
||||
reason: initialValues?.notes ?? undefined,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user