search aldeias sucos master
This commit is contained in:
@ -5,6 +5,8 @@ import { useCallApi } from '@/hooks';
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||
import ListToolbar from '../blocks/ListToolbar';
|
||||
import { useNavigate } from 'react-router';
|
||||
import axios from 'axios';
|
||||
|
||||
interface SucosProps {
|
||||
id: number;
|
||||
@ -34,15 +36,15 @@ interface ContextProps {
|
||||
const initialProps: ContextProps = {
|
||||
sucos: [],
|
||||
showSearchDialog: false,
|
||||
handleSearchDialog: () => {},
|
||||
handleSearchDialog: (show: boolean) => {},
|
||||
showEditDialog: false,
|
||||
handleEditDialog: () => {},
|
||||
handleEditDialog: (show: boolean, selected_sucos: string | null) => {},
|
||||
showAddDialog: false,
|
||||
handleAddDialog: () => {},
|
||||
handleAddDialog: (show: boolean) => {},
|
||||
showDeleteDialog: false,
|
||||
handleDeleteDialog: () => {},
|
||||
handleDeleteDialog: (show: boolean, selected_sucos: string | null) => {},
|
||||
selectedSucos: null,
|
||||
getSucosLists: async () => undefined
|
||||
getSucosLists: async () => ({ data: [], totalCount: 0 })
|
||||
};
|
||||
|
||||
const ManageSucosContext = createContext<ContextProps>(initialProps);
|
||||
@ -56,6 +58,7 @@ const ManageSucosContextProvider = ({ children }: { children: React.ReactNode })
|
||||
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||
const [selectedSucos, setSelectedSucos] = useState<string | null>(null);
|
||||
const { GetData } = useCallApi();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleSearchDialog = useCallback((show: boolean) => {
|
||||
setShowSearchDialog(show);
|
||||
@ -75,6 +78,10 @@ const ManageSucosContextProvider = ({ children }: { children: React.ReactNode })
|
||||
setShowDeleteDialog(show);
|
||||
}, []);
|
||||
|
||||
const handleNavigate = (path: string) => {
|
||||
const url = navigate(`${API_URL}/sucos/aldeias/${path}`);
|
||||
};
|
||||
|
||||
const columns = useMemo<ColumnDef<any>[]>(
|
||||
() => [
|
||||
{
|
||||
@ -145,22 +152,67 @@ const ManageSucosContextProvider = ({ children }: { children: React.ReactNode })
|
||||
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
||||
const response = await GetData(`${API_URL}/sucos/list`, {
|
||||
limit,
|
||||
limit: limit,
|
||||
page: page + 1,
|
||||
with_deleted: true,
|
||||
order_field: sorting[0].id,
|
||||
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
|
||||
filter: JSON.stringify(filter)
|
||||
});
|
||||
console.log(response?.data);
|
||||
setSucos(response?.data.list);
|
||||
// console.log(sucos);
|
||||
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) {
|
||||
console.error('Error fetching Sucos', error);
|
||||
}
|
||||
};
|
||||
|
||||
const getAldeiasBySucos = async (name: string) => {
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/sucos/aldeias/${name}`);
|
||||
const data = response.data;
|
||||
console.log(data);
|
||||
} catch (error) {
|
||||
console.log(`Error fetching sucos by ${name}`, error);
|
||||
}
|
||||
};
|
||||
|
||||
const createSucos = async (data: Partial<SucosProps>) => {
|
||||
try {
|
||||
await axios.post(`${API_URL}/sucos/create`, data);
|
||||
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||
} catch (error) {
|
||||
console.error('Error creating municipios', error);
|
||||
}
|
||||
};
|
||||
|
||||
const updateSucos = async (id: number, data: Partial<SucosProps>) => {
|
||||
try {
|
||||
await axios.put(`${API_URL}/sucos/update/${id}`, data);
|
||||
// getSucosLists(10, 1, false, 'name', 'ASC');
|
||||
} catch (error) {
|
||||
console.error('Error updating sucos', error);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteSucos = async (id: number, hardDelete?: boolean) => {
|
||||
try {
|
||||
await axios.delete(`${API_URL}/sucos/delete/${id}/${hardDelete}`);
|
||||
// getSucosLists(10, 1, false, 'name', 'ASC');
|
||||
} catch (error) {
|
||||
console.error('Error deleting sucos', error);
|
||||
}
|
||||
};
|
||||
|
||||
const restoreSucos = async (id: number) => {
|
||||
try {
|
||||
await axios.put(`${API_URL}/sucos/restore/${id}`);
|
||||
// getSucosLists(10, 1, false, 'name', 'ASC');
|
||||
} catch (error) {
|
||||
console.error('Error restoring sucos', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ManageSucosContext.Provider
|
||||
value={{
|
||||
|
||||
@ -4,7 +4,8 @@ import { ManageSucosContext } from './ManageSucosContext';
|
||||
const useManageSucosContext = () => {
|
||||
const context = useContext(ManageSucosContext);
|
||||
|
||||
if (!context) throw new Error('useManageSucosContext must be used within AuthProvider');
|
||||
if (!context)
|
||||
throw new Error('useManageSucosContext must be used within ManageSucosContextProvider');
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user