feat(FE-170): remove total_weight from body_weights and update validation logic in RecordingForm

This commit is contained in:
rstubryan
2025-11-19 18:04:15 +07:00
parent 7b28e47c68
commit 9c69369a51
3 changed files with 6 additions and 20 deletions
@@ -17,7 +17,6 @@ type RecordingGrowingFormSchemaType = {
weight: number | string;
avg_weight: number | string;
qty: number | string;
total_weight: number | string;
}[];
stocks: {
product_warehouse_id: number;
@@ -48,7 +47,6 @@ export type BodyWeightSchema = {
weight: number | string;
avg_weight: number | string;
qty: number | string;
total_weight: number | string;
};
export type StockSchema = {
@@ -73,16 +71,11 @@ const BodyWeightObjectSchema: Yup.ObjectSchema<BodyWeightSchema> = Yup.object({
.typeError('Berat ayam total harus berupa angka!'),
avg_weight: Yup.number()
.required('Berat ayam rata-rata wajib diisi!')
.min(1, 'Berat ayam rata-rata minimal 1 gram!')
.typeError('Berat ayam rata-rata harus berupa angka!'),
qty: Yup.number()
.required('Jumlah ayam wajib diisi!')
.min(1, 'Jumlah ayam minimal 1 ekor!')
.typeError('Jumlah ayam harus berupa angka!'),
total_weight: Yup.number()
.required('Berat ayam total wajib diisi!')
.min(0, 'Berat ayam total tidak boleh negatif!')
.typeError('Berat ayam total harus berupa angka!'),
});
const StockObjectSchema: Yup.ObjectSchema<StockSchema> = Yup.object({
@@ -259,14 +252,12 @@ export const getRecordingGrowingFormInitialValues = (
weight: bw.avg_weight * bw.qty,
avg_weight: bw.avg_weight,
qty: bw.qty,
total_weight: bw.total_weight,
})
) ?? [
{
weight: '',
avg_weight: '',
qty: '',
total_weight: 0,
},
],
stocks: initialValues?.stocks?.map((stock) => ({
@@ -157,16 +157,12 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
return {
project_flock_kandang_id: values.project_flock_kandang_id,
body_weights: (values.body_weights ?? []).map((bw) => {
const qty = Number(bw.qty) || 0;
const weight = Number(bw.weight) || 0;
const totalWeight = qty * weight;
return {
avg_weight:
typeof bw.avg_weight === 'number'
? bw.avg_weight
: parseFloat(String(bw.avg_weight)) || 0,
qty: qty,
total_weight: parseFloat(String(totalWeight)) || 0,
qty: Number(bw.qty) || 0,
};
}),
stocks: (values.stocks ?? []).map((stock) => ({
@@ -1188,9 +1184,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const newBodyWeights = [
...(formik.values.body_weights || []),
{
weight: 0,
avg_weight: 0,
qty: 1,
weight: '',
avg_weight: '',
qty: '',
},
];
formik.setFieldValue('body_weights', newBodyWeights);
@@ -1214,7 +1210,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const avgWeight = parseFloat((value / qty).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else {
formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0);
formik.setFieldValue(`body_weights.${idx}.avg_weight`, '');
}
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
@@ -1262,7 +1258,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const avgWeight = parseFloat((weight / value).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else {
formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0);
formik.setFieldValue(`body_weights.${idx}.avg_weight`, '');
}
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
-1
View File
@@ -92,7 +92,6 @@ export type CreateGrowingRecordingPayload = {
body_weights: {
avg_weight: number;
qty: number;
total_weight: number;
}[];
stocks?: {
product_warehouse_id: number;