mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +00:00
refactor(FE-316): Use useSelect for location options
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import useSWR from 'swr';
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import { toast } from 'react-hot-toast';
|
import { toast } from 'react-hot-toast';
|
||||||
@@ -24,7 +23,7 @@ import {
|
|||||||
} from '@/components/pages/uniformity/form/UniformityForm.schema';
|
} from '@/components/pages/uniformity/form/UniformityForm.schema';
|
||||||
import { LocationApi, KandangApi } from '@/services/api/master-data';
|
import { LocationApi, KandangApi } from '@/services/api/master-data';
|
||||||
import { UniformityApi } from '@/services/api/uniformity';
|
import { UniformityApi } from '@/services/api/uniformity';
|
||||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseError } from '@/lib/api-helper';
|
||||||
import {
|
import {
|
||||||
Uniformity,
|
Uniformity,
|
||||||
CreateUniformityPayload,
|
CreateUniformityPayload,
|
||||||
@@ -46,11 +45,16 @@ const UniformityForm = ({
|
|||||||
const expandedDrawerOpen = useUiStore((s) => s.expandedDrawerOpen);
|
const expandedDrawerOpen = useUiStore((s) => s.expandedDrawerOpen);
|
||||||
const setExpandedDrawerOpen = useUiStore((s) => s.setExpandedDrawerOpen);
|
const setExpandedDrawerOpen = useUiStore((s) => s.setExpandedDrawerOpen);
|
||||||
|
|
||||||
const [locationSelectInputValue, setLocationSelectInputValue] = useState('');
|
|
||||||
const [uniformityFormErrorMessage, setUniformityFormErrorMessage] =
|
const [uniformityFormErrorMessage, setUniformityFormErrorMessage] =
|
||||||
useState('');
|
useState('');
|
||||||
|
|
||||||
// ===== SELECT INPUT DATA =====
|
// ===== SELECT INPUT DATA =====
|
||||||
|
const {
|
||||||
|
setInputValue: setLocationSelectInputValue,
|
||||||
|
options: locationOptions,
|
||||||
|
isLoadingOptions: isLoadingLocations,
|
||||||
|
} = useSelect(LocationApi.basePath, 'id', 'name', 'search');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
setInputValue: setKandangSelectInputValue,
|
setInputValue: setKandangSelectInputValue,
|
||||||
options: kandangOptions,
|
options: kandangOptions,
|
||||||
@@ -98,29 +102,6 @@ const UniformityForm = ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// ===== API DATA FETCHING =====
|
|
||||||
const locationsUrl = useMemo(() => {
|
|
||||||
const params = new URLSearchParams({
|
|
||||||
search: locationSelectInputValue,
|
|
||||||
});
|
|
||||||
return `${LocationApi.basePath}?${params.toString()}`;
|
|
||||||
}, [locationSelectInputValue]);
|
|
||||||
|
|
||||||
const { data: locations, isLoading: isLoadingLocations } = useSWR(
|
|
||||||
locationsUrl,
|
|
||||||
LocationApi.getAllFetcher
|
|
||||||
);
|
|
||||||
|
|
||||||
const locationOptions = useMemo(() => {
|
|
||||||
if (!locations || !isResponseSuccess(locations)) return [];
|
|
||||||
return (
|
|
||||||
locations.data.map((location) => ({
|
|
||||||
value: location.id,
|
|
||||||
label: location.name,
|
|
||||||
})) || []
|
|
||||||
);
|
|
||||||
}, [locations]);
|
|
||||||
|
|
||||||
// ===== FORM HANDLERS =====
|
// ===== FORM HANDLERS =====
|
||||||
const handleLocationChange = useCallback(
|
const handleLocationChange = useCallback(
|
||||||
(val: OptionType | OptionType[] | null) => {
|
(val: OptionType | OptionType[] | null) => {
|
||||||
@@ -185,7 +166,7 @@ const UniformityForm = ({
|
|||||||
formik.setFieldValue('files', undefined);
|
formik.setFieldValue('files', undefined);
|
||||||
}, [formik]);
|
}, [formik]);
|
||||||
|
|
||||||
// ===== EFFECTS =====
|
// ===== SIDE EFFECTS =====
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsub = subscribeValidate(() => {
|
const unsub = subscribeValidate(() => {
|
||||||
setIsValid(true);
|
setIsValid(true);
|
||||||
@@ -194,6 +175,7 @@ const UniformityForm = ({
|
|||||||
return unsub;
|
return unsub;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// ===== EVENT HANDLERS =====
|
||||||
const handleOpenExpandedDrawer = () => {
|
const handleOpenExpandedDrawer = () => {
|
||||||
setExpandedDrawerOpen(true);
|
setExpandedDrawerOpen(true);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user