refactor(FE-114): improve recording date handling in RecordingForm

This commit is contained in:
rstubryan
2025-10-16 10:08:49 +07:00
parent 27d2792a9c
commit 23d5a41d56
@@ -74,7 +74,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
recording_date: recording_date:
values.recording_date instanceof Date values.recording_date instanceof Date
? values.recording_date.toISOString() ? values.recording_date.toISOString()
: new Date().toISOString(), : '',
feed_data: (values.feed_data ?? []).map((p) => ({ feed_data: (values.feed_data ?? []).map((p) => ({
feed_name: p.feed_name, feed_name: p.feed_name,
feed_qty: p.feed_qty, feed_qty: p.feed_qty,
@@ -119,11 +119,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const flockOptions = DUMMY_FLOCKS; const flockOptions = DUMMY_FLOCKS;
// Location and Coop state/handlers
const [locationSelectInputValue, setLocationSelectInputValue] = useState(''); const [locationSelectInputValue, setLocationSelectInputValue] = useState('');
const [coopSelectInputValue, setCoopSelectInputValue] = useState(''); const [coopSelectInputValue, setCoopSelectInputValue] = useState('');
// Location fetch
const locationsUrl = `${LocationApi.basePath}?${new URLSearchParams({ search: locationSelectInputValue ?? '' }).toString()}`; const locationsUrl = `${LocationApi.basePath}?${new URLSearchParams({ search: locationSelectInputValue ?? '' }).toString()}`;
const { data: locations, isLoading: isLoadingLocations } = useSWR( const { data: locations, isLoading: isLoadingLocations } = useSWR(
locationsUrl, locationsUrl,
@@ -133,14 +131,12 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
? locations?.data.map((loc) => ({ value: loc.id, label: loc.name })) ? locations?.data.map((loc) => ({ value: loc.id, label: loc.name }))
: []; : [];
// Coop fetch
const coopsUrl = `${KandangApi.basePath}?${new URLSearchParams({ search: coopSelectInputValue ?? '' }).toString()}`; const coopsUrl = `${KandangApi.basePath}?${new URLSearchParams({ search: coopSelectInputValue ?? '' }).toString()}`;
const { data: coops, isLoading: isLoadingCoops } = useSWR( const { data: coops, isLoading: isLoadingCoops } = useSWR(
coopsUrl, coopsUrl,
KandangApi.getAllFetcher KandangApi.getAllFetcher
); );
// Filter coop options based on selected location
const coopOptions = useMemo(() => { const coopOptions = useMemo(() => {
if (!isResponseSuccess(coops) || !formik.values.location_id) return []; if (!isResponseSuccess(coops) || !formik.values.location_id) return [];
return coops.data return coops.data
@@ -148,14 +144,12 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
.map((coop) => ({ value: coop.id, label: coop.name })); .map((coop) => ({ value: coop.id, label: coop.name }));
}, [coops, formik.values.location_id]); }, [coops, formik.values.location_id]);
// Handlers
const locationChangeHandler = (val: OptionType | OptionType[] | null) => { const locationChangeHandler = (val: OptionType | OptionType[] | null) => {
const locationValue = (val as OptionType)?.value; const locationValue = (val as OptionType)?.value;
formik.setFieldValue('location', val); formik.setFieldValue('location', val);
formik.setFieldValue('location_id', locationValue || 0); formik.setFieldValue('location_id', locationValue || 0);
// Only set touched if there's a value
if (locationValue) { if (locationValue) {
formik.setFieldTouched('location', true); formik.setFieldTouched('location', true);
formik.setFieldTouched('location_id', true); formik.setFieldTouched('location_id', true);
@@ -164,7 +158,6 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
formik.setFieldTouched('location_id', false); formik.setFieldTouched('location_id', false);
} }
// Reset coop selection when location changes or is cleared
formik.setFieldValue('coop', null); formik.setFieldValue('coop', null);
formik.setFieldValue('coop_id', 0); formik.setFieldValue('coop_id', 0);
formik.setFieldTouched('coop', false); formik.setFieldTouched('coop', false);
@@ -178,7 +171,6 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
formik.setFieldValue('coop', val); formik.setFieldValue('coop', val);
formik.setFieldValue('coop_id', coopValue || 0); formik.setFieldValue('coop_id', coopValue || 0);
// Only set touched if there's a value
if (coopValue) { if (coopValue) {
formik.setFieldTouched('coop', true); formik.setFieldTouched('coop', true);
formik.setFieldTouched('coop_id', true); formik.setFieldTouched('coop_id', true);
@@ -229,6 +221,21 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
}; };
}; };
const flockChangeHandler = (val: OptionType | OptionType[] | null) => {
const flockValue = (val as OptionType)?.value;
formik.setFieldValue('flock', val);
formik.setFieldValue('flock_id', flockValue || 0);
if (flockValue) {
formik.setFieldTouched('flock', true);
formik.setFieldTouched('flock_id', true);
} else {
formik.setFieldTouched('flock', false);
formik.setFieldTouched('flock_id', false);
}
};
const addFeedData = () => { const addFeedData = () => {
const newFeedData = [ const newFeedData = [
...(formik.values.feed_data || []), ...(formik.values.feed_data || []),
@@ -382,17 +389,9 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
required required
label='Flock' label='Flock'
value={formik.values.flock ?? undefined} value={formik.values.flock ?? undefined}
onChange={(val) => { onChange={flockChangeHandler}
formik.setFieldValue('flock', val);
formik.setFieldValue(
'flock_id',
(val as OptionType)?.value
);
}}
options={flockOptions} options={flockOptions}
onInputChange={(val) => { onInputChange={setFlockSelectInputValue}
return val;
}}
isLoading={false} isLoading={false}
isError={ isError={
formik.touched.flock_id && Boolean(formik.errors.flock_id) formik.touched.flock_id && Boolean(formik.errors.flock_id)
@@ -405,7 +404,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
required required
label='Tanggal Recording' label='Tanggal Recording'
type='date' type='date'
name='tanggal_recording' name='recording_date'
value={ value={
formik.values.recording_date instanceof Date formik.values.recording_date instanceof Date
? formik.values.recording_date ? formik.values.recording_date
@@ -416,8 +415,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
onChange={(e) => { onChange={(e) => {
const date = e.target.value const date = e.target.value
? new Date(e.target.value) ? new Date(e.target.value)
: new Date(); : null;
formik.setFieldValue('tanggal_recording', date); formik.setFieldValue('recording_date', date);
}} }}
onBlur={formik.handleBlur} onBlur={formik.handleBlur}
isError={ isError={
@@ -1129,6 +1128,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
options={RECORDING_FLAG_OPTIONS} options={RECORDING_FLAG_OPTIONS}
isDisabled={type === 'detail'} isDisabled={type === 'detail'}
isClearable
/> />
</td> </td>
<td> <td>