diff --git a/src/pages/master/sucos/blocks/AddDialog.tsx b/src/pages/master/sucos/blocks/AddDialog.tsx index e9e0ad0..1eae604 100644 --- a/src/pages/master/sucos/blocks/AddDialog.tsx +++ b/src/pages/master/sucos/blocks/AddDialog.tsx @@ -15,15 +15,30 @@ import { } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, + CommandList +} from '@/components/ui/command'; +interface PostoAdmsProps { + PostoAdms_id: number; + PostoAdms_name: string; +} const API_URL = apiConfig.service_master_data; const AddDialog = () => { const parentRef = useRef(null); const { showAddDialog, handleAddDialog } = useManageSucosContext(); const { reload } = useDataGrid(); - const { PostData } = useCallApi(); + const { PostData, GetData } = useCallApi(); const parsedUser = getAuth()?.user; + const [posto_adms, setPostoadms] = useState([]); + const [open, setOpen] = useState(false); const [alert, setAlert] = useState({ show: false, message: '' @@ -44,20 +59,23 @@ const AddDialog = () => { setAlert({ show: false, message: '' }); }; - const doCreateSucos = useCallback(async (e: React.FormEvent) => { - e.preventDefault(); - const response = await PostData(`${API_URL}/sucos/create`, formField); + const doCreateSucos = useCallback( + async (e: React.FormEvent) => { + e.preventDefault(); + const response = await PostData(`${API_URL}/sucos/create`, formField); - if (response?.status) { - resetForm(); - handleAddDialog(false); - reload(); - toast.success('Success Create Sucos'); - } else { - toast.error('Failed Create Sucos'); - setAlert({ show: true, message: response?.message }); - } - }, []); + if (response?.status) { + resetForm(); + handleAddDialog(false); + reload(); + toast.success('Sucos created successfully!'); + } else { + toast.error('Failed to create Sucos Please try again.'); + setAlert({ show: true, message: 'Failed to create Sucos Please try again.' }); + } + }, + [formField] + ); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -76,7 +94,7 @@ const AddDialog = () => { if (showAddDialog) { setFormField({ ...formField, - created_by: parsedUser.username, + created_by: parsedUser?.username, created_at: formattedTime }); } @@ -88,9 +106,30 @@ const AddDialog = () => { } }, [showAddDialog]); + useEffect(() => { + const fetchPostoAdms = async (sorting: any) => { + try { + sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; + const response = await GetData(`${API_URL}/postoadms/list`, { + limit: 100, + page: 1, + with_deleted: false, + order_field: sorting[0].id, + order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' + }); + console.log('ini data posto :', response?.data); + setPostoadms(response?.data.list || []); + } catch (error) { + console.log('Error fetching posto', error); + } + }; + + fetchPostoAdms([{ id: 'name', desc: false }]); + }, []); + return ( handleAddDialog(open)}> - + Sucos - Create @@ -124,7 +163,40 @@ const AddDialog = () => { - + + + + + + + + No Posto Adms Found. + + {posto_adms.map((posto) => ( + { + setFormField({ + ...formField, + posto_adm_id: posto.PostoAdms_id + }); + setOpen(false); + }} + > + {posto.PostoAdms_name} + + ))} + + + + + + {/* { const value = parseInt(e.target.value, 10); setFormField({ ...formField, posto_adm_id: isNaN(value) ? 0 : value }); }} - /> + /> */} diff --git a/src/pages/master/sucos/blocks/ListToolbar.tsx b/src/pages/master/sucos/blocks/ListToolbar.tsx index 8779cad..f5cd607 100644 --- a/src/pages/master/sucos/blocks/ListToolbar.tsx +++ b/src/pages/master/sucos/blocks/ListToolbar.tsx @@ -1,23 +1,10 @@ import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { Button } from '@/components/ui/button'; -import { useCallback, useState } from 'react'; -import { toast } from 'sonner'; import { useManageSucosContext } from '../hooks/useManageSucosContext'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog, handleSearchDialog } = useManageSucosContext(); - const [searchName, setSearchName] = useState(''); - const [isLoading, setIsLoading] = useState(false); - - const handleFilterData = useCallback(() => { - try { - table.getColumn('name')?.setFilterValue(searchName); - } catch (error) { - toast.error('Error applying filter'); - console.error('Error applying filter:', error); - } - }, [searchName, table]); return (
@@ -29,8 +16,10 @@ const ListToolbar = () => { setSearchName(event.target.value)} + value={table.getColumn(`sucos_id`)?.getFilterValue() as string} + onChange={(event) => + table.getColumn('sucos_name')?.setFilterValue(event.target.value) + } /> @@ -38,7 +27,7 @@ const ListToolbar = () => { variant="outline" className="h-7.5 disabled:bg-gray-400" // disabled={isLoading} - onClick={handleFilterData} + // onClick={handleFilterData} > {/* {loadingButton === 'filter' ? : } */} diff --git a/src/pages/master/sucos/blocks/SearchDialog.tsx b/src/pages/master/sucos/blocks/SearchDialog.tsx index 22ef4c9..e744347 100644 --- a/src/pages/master/sucos/blocks/SearchDialog.tsx +++ b/src/pages/master/sucos/blocks/SearchDialog.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useRef, useState } from 'react'; +import { useRef, useState } from 'react'; import { Dialog, DialogBody, @@ -8,18 +8,19 @@ import { DialogTitle } from '@/components/ui/dialog'; import { Alert, KeenIcon } from '@/components'; -import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { apiConfig } from '@/config/api.config'; import axios from 'axios'; -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue -} from '@/components/ui/select'; import { useManageSucosContext } from '../hooks/useManageSucosContext'; +import { + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, + CommandList +} from '@/components/ui/command'; +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; interface AldeiasProps { id: number; @@ -30,14 +31,8 @@ const API_URL = apiConfig.service_master_data; const SearchDialog = () => { const parentRef = useRef(null); + const [open, setOpen] = useState(false); const { showSearchDialog, handleSearchDialog, sucos } = useManageSucosContext(); - console.log('Sucos:', sucos); - // const { sucos, getSucosLists } = useContext(ManageSucosContext); - - // useEffect(() => { - // getSucosLists(1, 1000, [], []); // Memuat semua sucos - // }, []); - const [alert, setAlert] = useState({ show: false, message: '' @@ -51,7 +46,7 @@ const SearchDialog = () => { const resetForm = () => { setFormField(initialState); }; - const [aldeias, setAldeias] = useState([]); + const [aldeia, setAldeia] = useState([]); const [isFound, setIsFound] = useState(false); const handleSubmit = async (e: React.FormEvent) => { @@ -64,23 +59,21 @@ const SearchDialog = () => { } try { - const response = await axios.get(`${API_URL}/sucos/aldeias/${id}`); - console.log('API Response Data:', response.data.data); - console.log('aldeias data:', aldeias); - console.log('isFound:', isFound); + const response = await axios.get(`${API_URL}/postoadms/sucos/${id}`); if (response.data.status) { - setAldeias(response.data.data); + setAldeia(response.data.data); + console.log(aldeia); setIsFound(true); - console.log('Found aldeias: ', response.data.data); + console.log('Found Sucos: ', response.data.data); } else { - setAldeias([]); + setAldeia([]); setIsFound(false); - setAlert({ show: true, message: 'No aldeias found.' }); + setAlert({ show: true, message: 'No sucos found.' }); } } catch (error) { - console.error('Error fetching aldeias', error); - setAlert({ show: true, message: 'Failed to fetch aldeias. Please try again.' }); + console.error('Error fetching sucos', error); + setAlert({ show: true, message: 'Failed to fetch sucos. Please try again.' }); } setAlert({ show: false, message: '' }); }; @@ -88,101 +81,93 @@ const SearchDialog = () => { const handleReset = () => { setFormField(initialState); setIsFound(false); - setAldeias([]); + setAldeia([]); }; - // console.log(aldeias); + return ( - handleSearchDialog(open)}> + - - + +

Search Aldeias

-
{ handleSearchDialog(false); - resetForm(); + handleReset(); }} >
- -
- {alert.show && ( - - {alert.message} - - )} -
-
-
- + + + {alert.show && {alert.message}} - -
+ + ))} + + + + + +
- {isFound && aldeias.length > 0 && ( -
-

Aldeias:

-
-
- - {aldeias.map((aldeia) => aldeia.name).join(', ')} - -
-
- )} - -
- - + {isFound && sucos.length > 0 && ( +
+

Aldeias:

+
+ + {aldeia.map((aldeias) => aldeias.name).join(', ')} +
- -
+ )} + +
+ + +
+
diff --git a/src/pages/master/sucos/hooks/ManageSucosContext.tsx b/src/pages/master/sucos/hooks/ManageSucosContext.tsx index d1ae6ac..212be5a 100644 --- a/src/pages/master/sucos/hooks/ManageSucosContext.tsx +++ b/src/pages/master/sucos/hooks/ManageSucosContext.tsx @@ -11,6 +11,7 @@ import axios from 'axios'; interface SucosProps { sucos_id: number; sucos_name: string; + posto_name: string; } interface ContextProps { @@ -159,7 +160,7 @@ const ManageSucosContextProvider = ({ children }: { children: React.ReactNode }) order_direction: sorting[0].desc == false ? 'ASC' : 'DESC', filter: JSON.stringify(filter) }); - console.log('Sucos List Response:', response?.data); + // console.log('Sucos List Response:', response?.data); setSucos(response?.data.list || []); // Pastikan default value adalah array kosong return { data: response?.data.list, totalCount: response?.data.total_count }; } catch (error) {