From d61c0ab84458515942151f849b36676d0f8b8e3e Mon Sep 17 00:00:00 2001 From: rstubryan Date: Thu, 23 Oct 2025 20:59:20 +0700 Subject: [PATCH] feat(FE-114): integrate date time handling in RecordingForm for on-time status --- .../recording/form/RecordingForm.tsx | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index a6b9f23c..26879c91 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useMemo, useState } from 'react'; +import { useMemo, useState, useEffect } from 'react'; import { useFormik } from 'formik'; import { Icon } from '@iconify/react'; import Button from '@/components/Button'; @@ -119,6 +119,45 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { formik.setFieldValue('project_flock_kandang_id', projectFlockId, false); }; + // EVENT HANDLERS - Date Time + const recordDateTimeChangeHandler = (datetime: Date | null) => { + formik.setFieldValue('record_datetime', datetime, false); + + // Auto-set ontime based on date difference + if (datetime) { + const today = new Date(); + const recordDate = new Date(datetime); + + // Reset time to compare only dates + const todayDateOnly = new Date(today.getFullYear(), today.getMonth(), today.getDate()); + const recordDateOnly = new Date(recordDate.getFullYear(), recordDate.getMonth(), recordDate.getDate()); + + // Set ontime to true if recording date is today, false otherwise + const isOnTime = todayDateOnly.getTime() === recordDateOnly.getTime(); + formik.setFieldValue('ontime', isOnTime, false); + } + }; + + // Set initial ontime value when form loads or record_datetime changes + useEffect(() => { + if (formik.values.record_datetime) { + const today = new Date(); + const recordDate = new Date(formik.values.record_datetime); + + // Reset time to compare only dates + const todayDateOnly = new Date(today.getFullYear(), today.getMonth(), today.getDate()); + const recordDateOnly = new Date(recordDate.getFullYear(), recordDate.getMonth(), recordDate.getDate()); + + // Set ontime to true if recording date is today, false otherwise + const isOnTime = todayDateOnly.getTime() === recordDateOnly.getTime(); + + // Only update if ontime is not set or different from calculated value + if (formik.values.ontime !== isOnTime) { + formik.setFieldValue('ontime', isOnTime, false); + } + } + }, [formik.values.record_datetime]); + // EVENT HANDLERS - Body Weights const addBodyWeight = () => { const newBodyWeights = [ @@ -304,7 +343,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { const datetime = e.target.value ? new Date(e.target.value) : null; - formik.setFieldValue('record_datetime', datetime); + recordDateTimeChangeHandler(datetime); }} onBlur={formik.handleBlur} isError={ @@ -332,13 +371,13 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { />