refactor(FE): Use useSelect for warehouse options

This commit is contained in:
rstubryan
2026-01-15 10:17:08 +07:00
parent 4b88b684af
commit 3bc5030a3d
@@ -67,12 +67,6 @@ const PurchaseRequestForm = ({
const [selectedLocation, setSelectedLocation] = useState('');
const [disabledLocation, setDisabledLocation] = useState(true);
// ===== TYPE DEFINITIONS =====
interface ProductOptionType {
value: number;
label: string;
}
// ===== UTILITY FUNCTIONS =====
const isRepeaterInputError = (
idx: number,
@@ -179,8 +173,20 @@ const PurchaseRequestForm = ({
const {
inputValue: warehouseSelectInputValue,
setInputValue: setWarehouseSelectInputValue,
options: warehouseOptions,
isLoadingOptions: isLoadingWarehouses,
} = useSelect(WarehouseApi.basePath, 'id', 'name', 'search');
loadMore: loadMoreWarehouses,
hasMore: hasMoreWarehouses,
} = useSelect(WarehouseApi.basePath, 'id', 'name', 'search', {
area_id:
selectedArea != ''
? selectedArea
: ((initialValues?.area?.id ?? '') as string),
location_id:
selectedLocation != ''
? selectedLocation
: ((initialValues?.location?.id ?? '') as string),
});
// ===== FORM CONFIGURATION =====
const formikInitialValues = useMemo<PurchaseRequestFormValues>(
@@ -283,45 +289,6 @@ const PurchaseRequestForm = ({
return data;
}, [supplierData]);
const warehousesUrl = useMemo(() => {
const params = new URLSearchParams({ search: warehouseSelectInputValue });
if (formik.values.area_id && formik.values.area_id > 0) {
params.append('area_id', formik.values.area_id.toString());
}
if (formik.values.location_id && formik.values.location_id > 0) {
params.append('location_id', formik.values.location_id.toString());
}
return `${WarehouseApi.basePath}?${params.toString()}`;
}, [
warehouseSelectInputValue,
formik.values.area_id,
formik.values.location_id,
]);
const { data: warehouses } = useSWR(
warehousesUrl,
WarehouseApi.getAllFetcher
);
const warehouseOptions = useMemo(() => {
if (!isResponseSuccess(warehouses)) return [];
return (
warehouses?.data.map((w) => ({
value: w.id,
label: w.name,
area: w.area?.name,
location:
'type' in w && (w.type === 'LOKASI' || w.type === 'KANDANG')
? w.location?.name
: undefined,
})) || []
);
}, [warehouses]);
const addPurchaseItem = () => {
const newItems = [
...(formik.values.items || []),
@@ -733,6 +700,7 @@ const PurchaseRequestForm = ({
options={warehouseOptions}
onInputChange={setWarehouseSelectInputValue}
isLoading={isLoadingWarehouses}
onMenuScrollToBottom={loadMoreWarehouses}
isError={
isRepeaterInputError(idx, 'warehouse_id').isError
}
@@ -752,9 +720,9 @@ const PurchaseRequestForm = ({
required
value={item.product ?? undefined}
onChange={(val) => {
const product = val as ProductOptionType | null;
const product = val as OptionType | null;
const productId =
(product as ProductOptionType)?.value || 0;
(product as OptionType)?.value || 0;
formik.setFieldTouched(
`items.${idx}.product`,