refactor(FE-114,136): update flock references to use ProjectFlock and adjust RecordingForm for new API

This commit is contained in:
rstubryan
2025-10-17 13:16:54 +07:00
parent 79acdb4b7b
commit c77968940e
6 changed files with 71 additions and 25 deletions
@@ -18,7 +18,7 @@ import {
UpdateRecordingFormSchema,
} from './RecordingForm.schema';
import { useRecordingFormHandlers } from './useRecordingFormHandlers';
import { FlockApi } from '@/services/api/flock';
import { ProjectFlockApi } from '@/services/api/production';
import { isResponseSuccess } from '@/lib/api-helper';
import { RECORDING_FLAG_OPTIONS } from '@/config/constant';
import useSWR from 'swr';
@@ -29,15 +29,10 @@ interface RecordingFormProps {
initialValues?: Recording;
}
const DUMMY_FLOCKS = [
{ value: 1, label: 'Flock A' },
{ value: 2, label: 'Flock B' },
{ value: 3, label: 'Flock C' },
{ value: 4, label: 'Flock D' },
];
const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const [flockSelectInputValue, setFlockSelectInputValue] = useState('');
const [locationSelectInputValue, setLocationSelectInputValue] = useState('');
const [coopSelectInputValue, setCoopSelectInputValue] = useState('');
const [selectedFeed, setSelectedFeed] = useState<number[]>([]);
const [selectedWeight, setSelectedWeight] = useState<number[]>([]);
const [selectedVaccine, setSelectedVaccine] = useState<number[]>([]);
@@ -107,20 +102,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
},
});
// // Flock selection
// const flocksUrl = `${FlockApi.basePath}?${new URLSearchParams({ search: flockSelectInputValue }).toString()}`;
// const { data: flocks, isLoading: isLoadingFlocks } = useSWR(
// flocksUrl,
// FlockApi.getAllFetcher
// );
// const flockOptions = isResponseSuccess(flocks)
// ? flocks?.data.map((f) => ({ value: f.id, label: f.name }))
// : [];
// Flock selection
const projectFlocksUrl = `${ProjectFlockApi.basePath}?${new URLSearchParams({ search: flockSelectInputValue }).toString()}`;
const { data: projectFlocks, isLoading: isLoadingFlocks } = useSWR(
projectFlocksUrl,
ProjectFlockApi.getAllFetcher
);
const flockOptions = isResponseSuccess(projectFlocks)
? projectFlocks?.data.map((flock) => ({
value: flock.id,
label: flock.flock.name,
}))
: [];
const flockOptions = DUMMY_FLOCKS;
const [locationSelectInputValue, setLocationSelectInputValue] = useState('');
const [coopSelectInputValue, setCoopSelectInputValue] = useState('');
// Pakan selection
const locationsUrl = `${LocationApi.basePath}?${new URLSearchParams({ search: locationSelectInputValue ?? '' }).toString()}`;
const { data: locations, isLoading: isLoadingLocations } = useSWR(
@@ -366,7 +361,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
onChange={flockChangeHandler}
options={flockOptions}
onInputChange={setFlockSelectInputValue}
isLoading={false}
isLoading={isLoadingFlocks}
isError={
formik.touched.flock_id && Boolean(formik.errors.flock_id)
}