mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE): Set week using chick_in_date and earliest record
This commit is contained in:
@@ -399,23 +399,58 @@ const UniformityForm = ({
|
||||
// ===== SIDE EFFECTS =====
|
||||
useEffect(() => {
|
||||
if (
|
||||
projectFlockKandangLookup?.chick_in_date &&
|
||||
projectFlockKandangLookup?.project_flock_kandang_id &&
|
||||
isResponseSuccess(recordingsData) &&
|
||||
recordingsData.data
|
||||
) {
|
||||
const matchingRecording = recordingsData.data.find(
|
||||
const matchingRecordings = recordingsData.data.filter(
|
||||
(recording: Recording) =>
|
||||
recording.project_flock?.project_flock_kandang_id ===
|
||||
projectFlockKandangLookup.project_flock_kandang_id
|
||||
);
|
||||
|
||||
if (matchingRecording?.project_flock?.production_standart?.week) {
|
||||
const weekValue =
|
||||
matchingRecording.project_flock.production_standart.week;
|
||||
formik.setFieldValue('week', weekValue);
|
||||
matchingRecordings.sort(
|
||||
(a: Recording, b: Recording) =>
|
||||
new Date(a.record_datetime).getTime() -
|
||||
new Date(b.record_datetime).getTime()
|
||||
);
|
||||
|
||||
const earliestRecording = matchingRecordings[0];
|
||||
|
||||
if (earliestRecording) {
|
||||
const chickInDate = new Date(projectFlockKandangLookup.chick_in_date);
|
||||
chickInDate.setHours(0, 0, 0, 0);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [projectFlockKandangLookup?.project_flock_kandang_id, recordingsData]);
|
||||
}, [
|
||||
projectFlockKandangLookup?.chick_in_date,
|
||||
projectFlockKandangLookup?.project_flock_kandang_id,
|
||||
recordingsData,
|
||||
formik.values.date,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
const unsub = subscribeValidate(() => {
|
||||
|
||||
+1
@@ -76,6 +76,7 @@ export type ProjectFlockKandangLookup = {
|
||||
quantity: number;
|
||||
available_quantity?: number;
|
||||
population: number;
|
||||
chick_in_date: string;
|
||||
};
|
||||
|
||||
export type ProjectFlockAvailableQuantity = {
|
||||
|
||||
Reference in New Issue
Block a user