mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE): Use useSelect for project flock filter
This commit is contained in:
@@ -37,7 +37,10 @@ import DateInput from '@/components/input/DateInput';
|
||||
import { LocationApi } from '@/services/api/master-data';
|
||||
import { ProjectFlockApi } from '@/services/api/production';
|
||||
import { Kandang } from '@/types/api/master-data/kandang';
|
||||
import { ProjectFlockKandangLookup } from '@/types/api/production/project-flock';
|
||||
import {
|
||||
ProjectFlockKandangLookup,
|
||||
ProjectFlock,
|
||||
} from '@/types/api/production/project-flock';
|
||||
import {
|
||||
getStatusColor,
|
||||
getStatusIndicatorColor,
|
||||
@@ -229,63 +232,37 @@ const UniformityTable = () => {
|
||||
useState<number | undefined>(undefined);
|
||||
const [filterStartDate, setFilterStartDate] = useState('');
|
||||
const [filterEndDate, setFilterEndDate] = useState('');
|
||||
const [projectFlockSearchValue, setProjectFlockSearchValue] = useState('');
|
||||
const [filterProjectFlockLocationId, setFilterProjectFlockLocationId] =
|
||||
useState<string>('');
|
||||
const [filterErrors, setFilterErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const {
|
||||
setInputValue: setFilterLocationInputValue,
|
||||
options: filterLocationOptions,
|
||||
isLoadingOptions: isLoadingFilterLocations,
|
||||
} = useSelect(LocationApi.basePath, 'id', 'name', 'search', {
|
||||
limit: '100',
|
||||
});
|
||||
loadMore: loadMoreFilterLocations,
|
||||
hasMore: hasMoreFilterLocations,
|
||||
} = useSelect(LocationApi.basePath, 'id', 'name', 'search');
|
||||
|
||||
// ===== FETCH PROJECT FLOCKS DATA FOR FILTER =====
|
||||
const filterProjectFlocksUrl = useMemo(() => {
|
||||
const params = new URLSearchParams({
|
||||
search: projectFlockSearchValue || '',
|
||||
limit: '100',
|
||||
});
|
||||
if (filterLocation) {
|
||||
params.append('location_id', filterLocation.value.toString());
|
||||
}
|
||||
return `${ProjectFlockApi.basePath}?${params.toString()}`;
|
||||
}, [projectFlockSearchValue, filterLocation]);
|
||||
|
||||
const {
|
||||
data: filterProjectFlocksData,
|
||||
isLoading: isLoadingFilterProjectFlocks,
|
||||
} = useSWR(filterProjectFlocksUrl, ProjectFlockApi.getAllFetcher);
|
||||
|
||||
const filterProjectFlocksDataList = useMemo(
|
||||
() =>
|
||||
isResponseSuccess(filterProjectFlocksData)
|
||||
? filterProjectFlocksData.data
|
||||
: undefined,
|
||||
[filterProjectFlocksData]
|
||||
);
|
||||
|
||||
const filterProjectFlockOptions = useMemo(() => {
|
||||
let options: OptionType[] = [];
|
||||
|
||||
if (isResponseSuccess(filterProjectFlocksData)) {
|
||||
const flockOptions =
|
||||
filterProjectFlocksData?.data.map((projectFlock) => ({
|
||||
value: projectFlock.id,
|
||||
label: projectFlock.flock_name || '',
|
||||
})) || [];
|
||||
options = options.concat(flockOptions);
|
||||
}
|
||||
|
||||
return options;
|
||||
}, [filterProjectFlocksData]);
|
||||
setInputValue: setFilterProjectFlockSearchValue,
|
||||
options: filterProjectFlockOptions,
|
||||
rawData: filterProjectFlocksRawData,
|
||||
isLoadingOptions: isLoadingFilterProjectFlocks,
|
||||
loadMore: loadMoreFilterProjectFlocks,
|
||||
hasMore: hasMoreFilterProjectFlocks,
|
||||
} = useSelect(ProjectFlockApi.basePath, 'id', 'flock_name', 'search', {
|
||||
location_id: filterProjectFlockLocationId,
|
||||
});
|
||||
|
||||
// ===== KANDANG OPTIONS FOR FILTER =====
|
||||
const filterKandangOptions = useMemo(() => {
|
||||
let options: OptionType[] = [];
|
||||
|
||||
if (filterProjectFlock && filterProjectFlocksDataList) {
|
||||
const selectedProjectFlockData = filterProjectFlocksDataList.find(
|
||||
if (filterProjectFlock && isResponseSuccess(filterProjectFlocksRawData)) {
|
||||
const data = filterProjectFlocksRawData.data as unknown as ProjectFlock[];
|
||||
const selectedProjectFlockData = data.find(
|
||||
(pf) => pf.id === filterProjectFlock.value
|
||||
);
|
||||
|
||||
@@ -301,7 +278,7 @@ const UniformityTable = () => {
|
||||
}
|
||||
|
||||
return options;
|
||||
}, [filterProjectFlock, filterProjectFlocksDataList]);
|
||||
}, [filterProjectFlock, filterProjectFlocksRawData]);
|
||||
|
||||
// ===== PROJECT FLOCK KANDANG LOOKUP =====
|
||||
const projectFlockKandangLookupUrl = useMemo(() => {
|
||||
@@ -394,9 +371,13 @@ const UniformityTable = () => {
|
||||
// ===== FILTER HANDLERS =====
|
||||
const handleFilterLocationChange = useCallback(
|
||||
(val: OptionType | OptionType[] | null) => {
|
||||
setFilterLocation(val as OptionType | null);
|
||||
const location = val as OptionType | null;
|
||||
setFilterLocation(location);
|
||||
setFilterProjectFlock(null);
|
||||
setFilterKandang(null);
|
||||
setFilterProjectFlockLocationId(
|
||||
location ? location.value.toString() : ''
|
||||
);
|
||||
},
|
||||
[]
|
||||
);
|
||||
@@ -1206,6 +1187,7 @@ const UniformityTable = () => {
|
||||
options={filterLocationOptions}
|
||||
onInputChange={setFilterLocationInputValue}
|
||||
isLoading={isLoadingFilterLocations}
|
||||
onMenuScrollToBottom={loadMoreFilterLocations}
|
||||
className={{ wrapper: 'w-full' }}
|
||||
/>
|
||||
{filterErrors.location && (
|
||||
@@ -1225,8 +1207,9 @@ const UniformityTable = () => {
|
||||
setFilterErrors((prev) => ({ ...prev, project_flock: '' }));
|
||||
}}
|
||||
options={filterProjectFlockOptions}
|
||||
onInputChange={setProjectFlockSearchValue}
|
||||
onInputChange={setFilterProjectFlockSearchValue}
|
||||
isLoading={isLoadingFilterProjectFlocks}
|
||||
onMenuScrollToBottom={loadMoreFilterProjectFlocks}
|
||||
isDisabled={!filterLocation}
|
||||
className={{ wrapper: 'w-full' }}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user