mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +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 =====
|
// ===== SIDE EFFECTS =====
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
|
projectFlockKandangLookup?.chick_in_date &&
|
||||||
projectFlockKandangLookup?.project_flock_kandang_id &&
|
projectFlockKandangLookup?.project_flock_kandang_id &&
|
||||||
isResponseSuccess(recordingsData) &&
|
isResponseSuccess(recordingsData) &&
|
||||||
recordingsData.data
|
recordingsData.data
|
||||||
) {
|
) {
|
||||||
const matchingRecording = recordingsData.data.find(
|
const matchingRecordings = recordingsData.data.filter(
|
||||||
(recording: Recording) =>
|
(recording: Recording) =>
|
||||||
recording.project_flock?.project_flock_kandang_id ===
|
recording.project_flock?.project_flock_kandang_id ===
|
||||||
projectFlockKandangLookup.project_flock_kandang_id
|
projectFlockKandangLookup.project_flock_kandang_id
|
||||||
);
|
);
|
||||||
|
|
||||||
if (matchingRecording?.project_flock?.production_standart?.week) {
|
matchingRecordings.sort(
|
||||||
const weekValue =
|
(a: Recording, b: Recording) =>
|
||||||
matchingRecording.project_flock.production_standart.week;
|
new Date(a.record_datetime).getTime() -
|
||||||
formik.setFieldValue('week', weekValue);
|
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(() => {
|
useEffect(() => {
|
||||||
const unsub = subscribeValidate(() => {
|
const unsub = subscribeValidate(() => {
|
||||||
|
|||||||
+1
@@ -76,6 +76,7 @@ export type ProjectFlockKandangLookup = {
|
|||||||
quantity: number;
|
quantity: number;
|
||||||
available_quantity?: number;
|
available_quantity?: number;
|
||||||
population: number;
|
population: number;
|
||||||
|
chick_in_date: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProjectFlockAvailableQuantity = {
|
export type ProjectFlockAvailableQuantity = {
|
||||||
|
|||||||
Reference in New Issue
Block a user