refactor(FE): Fix week calculation logic in UniformityForm

This commit is contained in:
rstubryan
2026-02-20 09:21:42 +07:00
parent 3e30dcb04e
commit bbbd767cf2
@@ -203,16 +203,19 @@ const UniformityForm = ({
// ===== RECORDINGS DATA (FOR WEEK CALCULATION) ===== // ===== RECORDINGS DATA (FOR WEEK CALCULATION) =====
const recordingsUrl = useMemo(() => { const recordingsUrl = useMemo(() => {
if (!projectFlockKandangLookup?.project_flock_kandang_id) return null;
const params = new URLSearchParams({ const params = new URLSearchParams({
page: '1', page: '1',
limit: '100', limit: '100',
project_flock_kandang_id:
projectFlockKandangLookup.project_flock_kandang_id.toString(),
}); });
return `${RecordingApi.basePath}?${params.toString()}`; return `${RecordingApi.basePath}?${params.toString()}`;
}, []); }, [projectFlockKandangLookup?.project_flock_kandang_id]);
const { data: recordingsData } = useSWR( const { data: recordingsData } = useSWR(
recordingsUrl, recordingsUrl,
RecordingApi.getAllFetcher recordingsUrl ? RecordingApi.getAllFetcher : null
); );
// ===== FORM CONFIGURATION ===== // ===== FORM CONFIGURATION =====
@@ -400,50 +403,46 @@ const UniformityForm = ({
useEffect(() => { useEffect(() => {
if ( if (
projectFlockKandangLookup?.chick_in_date && projectFlockKandangLookup?.chick_in_date &&
projectFlockKandangLookup?.project_flock_kandang_id && projectFlockKandangLookup?.project_flock_kandang_id
isResponseSuccess(recordingsData) &&
recordingsData.data
) { ) {
const matchingRecordings = recordingsData.data.filter( const chickInDate = new Date(projectFlockKandangLookup.chick_in_date);
(recording: Recording) => chickInDate.setHours(0, 0, 0, 0);
recording.project_flock?.project_flock_kandang_id ===
projectFlockKandangLookup.project_flock_kandang_id
);
matchingRecordings.sort( let initialWeek = 18;
(a: Recording, b: Recording) =>
new Date(a.record_datetime).getTime() -
new Date(b.record_datetime).getTime()
);
const earliestRecording = matchingRecordings[0]; if (
isResponseSuccess(recordingsData) &&
recordingsData.data &&
recordingsData.data.length > 0
) {
const sortedRecordings = [...recordingsData.data].sort(
(a: Recording, b: Recording) =>
new Date(a.record_datetime).getTime() -
new Date(b.record_datetime).getTime()
);
if (earliestRecording) { const earliestRecording = sortedRecordings[0];
const chickInDate = new Date(projectFlockKandangLookup.chick_in_date); if (earliestRecording?.project_flock?.production_standart?.week) {
chickInDate.setHours(0, 0, 0, 0); initialWeek =
earliestRecording.project_flock.production_standart.week;
const earliestRecordDate = new Date(earliestRecording.record_datetime);
earliestRecordDate.setHours(0, 0, 0, 0);
const initialWeek =
earliestRecording.project_flock?.production_standart?.week || 18;
if (formik.values.date) {
const selectedDate = new Date(formik.values.date);
selectedDate.setHours(0, 0, 0, 0);
const daysDiff = Math.floor(
(selectedDate.getTime() - chickInDate.getTime()) /
(1000 * 60 * 60 * 24)
);
const weeksDiff = Math.floor(daysDiff / 7);
formik.setFieldValue('week', initialWeek + weeksDiff);
} else {
formik.setFieldValue('week', initialWeek);
} }
} }
if (formik.values.date) {
const selectedDate = new Date(formik.values.date);
selectedDate.setHours(0, 0, 0, 0);
const daysDiff = Math.floor(
(selectedDate.getTime() - chickInDate.getTime()) /
(1000 * 60 * 60 * 24)
);
const weeksDiff = Math.floor(daysDiff / 7);
formik.setFieldValue('week', initialWeek + weeksDiff);
} else {
formik.setFieldValue('week', initialWeek);
}
} }
}, [ }, [
projectFlockKandangLookup?.chick_in_date, projectFlockKandangLookup?.chick_in_date,