add table page for postoadms by municipioId
This commit is contained in:
@ -1,10 +1,16 @@
|
|||||||
|
import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext';
|
||||||
|
import { Container, DataGridInner } from '@/components';
|
||||||
|
|
||||||
const PostoAdmsMaster = () => {
|
const PostoAdmsMaster = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<ManagePostoAdmsContextProvider>
|
||||||
<div className="container mx-auto p-5">
|
<Container>
|
||||||
<h1 className="text-xl font-medium leading-none text-gray-900">PostoAdms Master Data</h1>
|
<h1>Postu Administrativo</h1>
|
||||||
</div>
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
</div>
|
<DataGridInner />
|
||||||
|
</div>
|
||||||
|
</Container>
|
||||||
|
</ManagePostoAdmsContextProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
63
src/pages/master/postoadms/blocks/ListToolbar.tsx
Normal file
63
src/pages/master/postoadms/blocks/ListToolbar.tsx
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
|
||||||
|
const ListToolbar = () => {
|
||||||
|
const { table, reload } = useDataGrid();
|
||||||
|
const { handleAddDialog, handleSearchDialog } = useManagePostoAdmsContext();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card-header flex-wrap gap-2 border-b-0 px-5">
|
||||||
|
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
||||||
|
<div className="flex justify-between w-full items-center">
|
||||||
|
<div className="flex w-[50%] gap-3 items-center">
|
||||||
|
<label className="input input-sm w-1/3">
|
||||||
|
<KeenIcon icon="magnifier" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search Postu Administrativo"
|
||||||
|
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
|
||||||
|
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<DefaultTooltip title={'Filter'} placement={'top'}>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 disabled:bg-gray-400"
|
||||||
|
// disabled={isLoading}
|
||||||
|
// onClick={handleFilterData}
|
||||||
|
>
|
||||||
|
{/* {loadingButton === 'filter' ? <ContentLoader /> : <KeenIcon icon="filter" />} */}
|
||||||
|
<KeenIcon icon="filter" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleSearchDialog(true)}
|
||||||
|
>
|
||||||
|
Search Postu Administrativo
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 items-center">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleAddDialog(true)}
|
||||||
|
>
|
||||||
|
Add Data
|
||||||
|
</Button>
|
||||||
|
<DefaultTooltip title={'Refresh'} placement={'top'}>
|
||||||
|
<Button variant="outline" className="h-7.5" onClick={() => reload()}>
|
||||||
|
<KeenIcon icon="arrows-circle" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListToolbar;
|
||||||
178
src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx
Normal file
178
src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
import { DataGridColumnHeader, DataGridProvider } from '@/components';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Toaster } from '@/components/ui/sonner';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { createContext, useCallback, useMemo, useState } from 'react';
|
||||||
|
import { useNavigate, useParams } from 'react-router';
|
||||||
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
|
||||||
|
interface PostoAdmsProps {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContextProps {
|
||||||
|
showSearchDialog: boolean;
|
||||||
|
handleSearchDialog: (show: boolean) => void;
|
||||||
|
showEditDialog: boolean;
|
||||||
|
handleEditDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
showAddDialog: boolean;
|
||||||
|
handleAddDialog: (show: boolean) => void;
|
||||||
|
showDeleteDialog: boolean;
|
||||||
|
handleDeleteDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
selectedPostoAdms: string | null;
|
||||||
|
getPostoAdmsLists: (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: any,
|
||||||
|
order_direction: any
|
||||||
|
) => Promise<{ data: PostoAdmsProps[]; totalCount: number } | undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialProps: ContextProps = {
|
||||||
|
showSearchDialog: false,
|
||||||
|
handleSearchDialog: (show: boolean) => {},
|
||||||
|
showEditDialog: false,
|
||||||
|
handleEditDialog: (show: boolean, selected_postoAdms: string | null) => {},
|
||||||
|
showAddDialog: false,
|
||||||
|
handleAddDialog: (show: boolean) => {},
|
||||||
|
showDeleteDialog: false,
|
||||||
|
handleDeleteDialog: (show: boolean, selected_postoAdms: string | null) => {},
|
||||||
|
selectedPostoAdms: null,
|
||||||
|
getPostoAdmsLists: async () => ({ data: [], totalCount: 0 })
|
||||||
|
};
|
||||||
|
|
||||||
|
const ManagePostoAdmsContext = createContext<ContextProps>(initialProps);
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
const [showSearchDialog, setShowSearchDialog] = useState(false);
|
||||||
|
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||||
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||||
|
const [selectedPostoAdms, setSelectedPostoAdms] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { municipioId } = useParams();
|
||||||
|
|
||||||
|
const handleSearchDialog = useCallback((show: boolean) => {
|
||||||
|
setShowSearchDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleAddDialog = useCallback((show: boolean) => {
|
||||||
|
setShowAddDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleEditDialog = useCallback((show: boolean, selected_postoAdms: string | null) => {
|
||||||
|
setSelectedPostoAdms(show ? selected_postoAdms : null);
|
||||||
|
setShowEditDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDeleteDialog = useCallback((show: boolean, selected_postoAdms: string | null) => {
|
||||||
|
setSelectedPostoAdms(show ? selected_postoAdms : null);
|
||||||
|
setShowDeleteDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const columns = useMemo<ColumnDef<any>[]>(
|
||||||
|
() => [
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.id,
|
||||||
|
id: 'id',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.name,
|
||||||
|
id: 'name',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Municipios Name" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[1000px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'actions',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px], text-center',
|
||||||
|
cellClassName: 'text-center'
|
||||||
|
},
|
||||||
|
cell: (info) => (
|
||||||
|
<Button
|
||||||
|
variant={'outline'}
|
||||||
|
onClick={() => navigate(`/master-data/municipios/postoadms/${info.row.original.id}`)}
|
||||||
|
>
|
||||||
|
Details
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[handleEditDialog, handleDeleteDialog]
|
||||||
|
);
|
||||||
|
|
||||||
|
const getPostoAdmsLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
||||||
|
sorting: sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||||
|
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/municipios/postoadms/${municipioId}`, {
|
||||||
|
params: {
|
||||||
|
limit,
|
||||||
|
page: page + 1,
|
||||||
|
with_deleted: false,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc ? 'DESC' : 'ASC'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(response.data);
|
||||||
|
return { data: response.data.data, totalCount: response.data.data.total_count };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching Postu Administrativo', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ManagePostoAdmsContext.Provider
|
||||||
|
value={{
|
||||||
|
showSearchDialog,
|
||||||
|
handleSearchDialog,
|
||||||
|
showAddDialog,
|
||||||
|
handleAddDialog,
|
||||||
|
showEditDialog,
|
||||||
|
handleEditDialog,
|
||||||
|
showDeleteDialog,
|
||||||
|
handleDeleteDialog,
|
||||||
|
selectedPostoAdms,
|
||||||
|
getPostoAdmsLists
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Toaster expand visibleToasts={9} duration={3000} />
|
||||||
|
|
||||||
|
<DataGridProvider
|
||||||
|
columns={columns}
|
||||||
|
pagination={{ size: 25 }}
|
||||||
|
toolbar={<ListToolbar />}
|
||||||
|
layout={{ card: true }}
|
||||||
|
sorting={[{ id: 'id', desc: false }]}
|
||||||
|
serverSide={true}
|
||||||
|
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||||
|
getPostoAdmsLists(pageIndex, pageSize, sorting, columnFilters)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</DataGridProvider>
|
||||||
|
</ManagePostoAdmsContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { ManagePostoAdmsContextProvider, ManagePostoAdmsContext };
|
||||||
|
export type { PostoAdmsProps };
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { ManagePostoAdmsContext } from './ManagePostoAdmsContext';
|
||||||
|
|
||||||
|
const useManagePostoAdmsContext = () => {
|
||||||
|
const context = useContext(ManagePostoAdmsContext);
|
||||||
|
|
||||||
|
if (!context) throw new Error('useManagePostoAdmsContext must be used within AuthProvider');
|
||||||
|
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useManagePostoAdmsContext };
|
||||||
Reference in New Issue
Block a user