mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE): Refactor formik field updates to use destructured methods
This commit is contained in:
@@ -279,6 +279,8 @@ const UniformityForm = ({
|
||||
},
|
||||
});
|
||||
|
||||
const { setFieldValue, setFieldTouched } = formik;
|
||||
|
||||
const handleValidateForm = async () => {
|
||||
const errors = await formik.validateForm();
|
||||
|
||||
@@ -301,10 +303,10 @@ const UniformityForm = ({
|
||||
const location = val as OptionType | null;
|
||||
const locationId = Number(location?.value);
|
||||
|
||||
formik.setFieldTouched('location', true);
|
||||
formik.setFieldValue('location', location);
|
||||
formik.setFieldTouched('location_id', true);
|
||||
formik.setFieldValue('location_id', locationId);
|
||||
setFieldTouched('location', true);
|
||||
setFieldValue('location', location);
|
||||
setFieldTouched('location_id', true);
|
||||
setFieldValue('location_id', locationId);
|
||||
|
||||
setSelectedLocation(location);
|
||||
setSelectedProjectFlock(null);
|
||||
@@ -312,7 +314,7 @@ const UniformityForm = ({
|
||||
location ? location.value.toString() : ''
|
||||
);
|
||||
},
|
||||
[]
|
||||
[setFieldTouched, setFieldValue]
|
||||
);
|
||||
|
||||
const handleProjectFlockChange = useCallback(
|
||||
@@ -320,14 +322,14 @@ const UniformityForm = ({
|
||||
const projectFlock = val as OptionType | null;
|
||||
const projectFlockId = Number(projectFlock?.value);
|
||||
|
||||
formik.setFieldTouched('project_flock', true);
|
||||
formik.setFieldValue('project_flock', projectFlock);
|
||||
formik.setFieldTouched('project_flock_id', true);
|
||||
formik.setFieldValue('project_flock_id', projectFlockId);
|
||||
setFieldTouched('project_flock', true);
|
||||
setFieldValue('project_flock', projectFlock);
|
||||
setFieldTouched('project_flock_id', true);
|
||||
setFieldValue('project_flock_id', projectFlockId);
|
||||
|
||||
setSelectedProjectFlock(projectFlock);
|
||||
},
|
||||
[]
|
||||
[setFieldTouched, setFieldValue]
|
||||
);
|
||||
|
||||
const handleKandangChange = useCallback(
|
||||
@@ -335,24 +337,24 @@ const UniformityForm = ({
|
||||
const kandang = val as OptionType | null;
|
||||
const kandangId = Number(kandang?.value);
|
||||
|
||||
formik.setFieldTouched('kandang', true);
|
||||
formik.setFieldValue('kandang', kandang);
|
||||
formik.setFieldTouched('kandang_id', true);
|
||||
formik.setFieldValue('kandang_id', kandangId);
|
||||
setFieldTouched('kandang', true);
|
||||
setFieldValue('kandang', kandang);
|
||||
setFieldTouched('kandang_id', true);
|
||||
setFieldValue('kandang_id', kandangId);
|
||||
|
||||
setSelectedKandang(kandang);
|
||||
},
|
||||
[]
|
||||
[setFieldTouched, setFieldValue]
|
||||
);
|
||||
|
||||
const handleFileChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const document = e.target.files?.[0];
|
||||
|
||||
formik.setFieldTouched('document', true);
|
||||
setFieldTouched('document', true);
|
||||
|
||||
if (!document) {
|
||||
formik.setFieldValue('document', undefined);
|
||||
setFieldValue('document', undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -372,24 +374,24 @@ const UniformityForm = ({
|
||||
return;
|
||||
}
|
||||
|
||||
formik.setFieldValue('document', document);
|
||||
setFieldValue('document', document);
|
||||
},
|
||||
[]
|
||||
[setFieldTouched, setFieldValue]
|
||||
);
|
||||
|
||||
const handleDateChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
formik.setFieldValue('date', e.target.value);
|
||||
setFieldValue('date', e.target.value);
|
||||
},
|
||||
[]
|
||||
[setFieldValue]
|
||||
);
|
||||
|
||||
const handleRemoveFile = useCallback(() => {
|
||||
formik.setFieldValue('document', undefined);
|
||||
setFieldValue('document', undefined);
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = '';
|
||||
}
|
||||
}, [formik]);
|
||||
}, [setFieldValue]);
|
||||
|
||||
const handleDownloadTemplate = useCallback(() => {
|
||||
const population = projectFlockKandangLookup?.population;
|
||||
@@ -442,9 +444,9 @@ const UniformityForm = ({
|
||||
|
||||
const weeksDiff = Math.floor(daysDiff / 7);
|
||||
|
||||
formik.setFieldValue('week', initialWeek + weeksDiff);
|
||||
setFieldValue('week', initialWeek + weeksDiff);
|
||||
} else {
|
||||
formik.setFieldValue('week', initialWeek);
|
||||
setFieldValue('week', initialWeek);
|
||||
}
|
||||
}
|
||||
}, [
|
||||
@@ -452,6 +454,7 @@ const UniformityForm = ({
|
||||
projectFlockKandangLookup?.project_flock_kandang_id,
|
||||
recordingsData,
|
||||
formik.values.date,
|
||||
setFieldValue,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user