refactor(FE-114,136): improve location and coop field handling in RecordingForm

This commit is contained in:
rstubryan
2025-10-15 17:53:08 +07:00
parent 64a32fd214
commit 8bfce061e6
@@ -150,21 +150,42 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
// Handlers // Handlers
const locationChangeHandler = (val: OptionType | OptionType[] | null) => { const locationChangeHandler = (val: OptionType | OptionType[] | null) => {
formik.setFieldTouched('location', true); const locationValue = (val as OptionType)?.value;
formik.setFieldValue('location', val); formik.setFieldValue('location', val);
formik.setFieldTouched('location_id', true); formik.setFieldValue('location_id', locationValue || 0);
formik.setFieldValue('location_id', (val as OptionType)?.value);
// Reset coop selection when location changes // Only set touched if there's a value
if (locationValue) {
formik.setFieldTouched('location', true);
formik.setFieldTouched('location_id', true);
} else {
formik.setFieldTouched('location', 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_id', false);
setCoopSelectInputValue(''); setCoopSelectInputValue('');
}; };
const coopChangeHandler = (val: OptionType | OptionType[] | null) => { const coopChangeHandler = (val: OptionType | OptionType[] | null) => {
formik.setFieldTouched('coop', true); const coopValue = (val as OptionType)?.value;
formik.setFieldValue('coop', val); formik.setFieldValue('coop', val);
formik.setFieldTouched('coop_id', true); formik.setFieldValue('coop_id', coopValue || 0);
formik.setFieldValue('coop_id', (val as OptionType)?.value);
// Only set touched if there's a value
if (coopValue) {
formik.setFieldTouched('coop', true);
formik.setFieldTouched('coop_id', true);
} else {
formik.setFieldTouched('coop', false);
formik.setFieldTouched('coop_id', false);
}
}; };
const isRepeaterInputError = <T extends keyof CreateRecordingPayload>( const isRepeaterInputError = <T extends keyof CreateRecordingPayload>(
@@ -423,6 +444,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
/> />
<SelectInput <SelectInput
key={`coop-select-${formik.values.location_id || 'no-location'}`}
required required
label='Kandang' label='Kandang'
value={formik.values.coop ?? undefined} value={formik.values.coop ?? undefined}