add crud aldeias
This commit is contained in:
@ -1,10 +1,22 @@
|
||||
import { Container, DataGridInner } from '@/components';
|
||||
import { ManageAldeiasContextProvider } from './hooks/ManageAldeiasContext';
|
||||
import AddDialog from './blocks/AddDialog';
|
||||
import EditDialog from './blocks/EditDialog';
|
||||
import DeleteDialog from './blocks/DeleteDialog';
|
||||
|
||||
const AldeiasMaster = () => {
|
||||
return (
|
||||
<div>
|
||||
<div className="container mx-auto p-5">
|
||||
<h1 className="text-xl font-medium leading-none text-gray-900">Aldeias Master Data</h1>
|
||||
</div>
|
||||
</div>
|
||||
<ManageAldeiasContextProvider>
|
||||
<Container>
|
||||
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">Aldeias</h1>
|
||||
<div className="grid gap-5 lg:gap-7.5">
|
||||
<DataGridInner />
|
||||
</div>
|
||||
<AddDialog />
|
||||
<EditDialog />
|
||||
<DeleteDialog />
|
||||
</Container>
|
||||
</ManageAldeiasContextProvider>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
152
src/pages/master/aldeias/blocks/AddDialog.tsx
Normal file
152
src/pages/master/aldeias/blocks/AddDialog.tsx
Normal file
@ -0,0 +1,152 @@
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||
import { Alert, useDataGrid } from '@/components';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { getAuth } from '@/auth';
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
Dialog,
|
||||
DialogBody,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
|
||||
const AddDialog = () => {
|
||||
const parentRef = useRef<any | null>(null);
|
||||
const { showAddDialog, handleAddDialog } = useManageAldeiasContext();
|
||||
const { reload } = useDataGrid();
|
||||
const { PostData } = useCallApi();
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const initialState = {
|
||||
name: '',
|
||||
sucos: 0,
|
||||
created_by: '',
|
||||
created_at: ''
|
||||
};
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const created_time = new Date();
|
||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
const doCreateAldeias = useCallback(
|
||||
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
const response = await PostData(`${API_URL}/aldeias/create`, formField);
|
||||
|
||||
if (response?.status) {
|
||||
resetForm();
|
||||
handleAddDialog(false);
|
||||
toast.success('Success Create Aldeias');
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Create Aldeias');
|
||||
setAlert({ show: true, message: 'Failed to create Aldeias. Please try again.' });
|
||||
}
|
||||
},
|
||||
[formField]
|
||||
);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (formField.name === '' || formField.sucos === 0) {
|
||||
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||
return;
|
||||
}
|
||||
|
||||
// doCreateAldeias(e);
|
||||
console.log(formField);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (showAddDialog) {
|
||||
setFormField({
|
||||
...formField,
|
||||
created_by: parsedUser.username,
|
||||
created_at: formattedTime
|
||||
});
|
||||
}
|
||||
}, [formattedTime]);
|
||||
|
||||
return (
|
||||
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Aldeias - Create</DialogTitle>
|
||||
<DialogDescription></DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogBody ref={parentRef}>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="card-body grid gap-5">
|
||||
<div className="w-full">
|
||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label className="form-label flex items-center gap-1 max-w-56">
|
||||
Name<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
className="input"
|
||||
type="text"
|
||||
value={formField.name}
|
||||
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label className="form-label flex items-center gap-1 max-w-56">
|
||||
Sucos ID<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
className="input"
|
||||
type="number"
|
||||
min={0}
|
||||
value={formField.sucos === 0 ? '' : formField.sucos}
|
||||
onChange={(e) => {
|
||||
const value = parseInt(e.target.value, 10);
|
||||
setFormField({ ...formField, sucos: isNaN(value) ? 0 : value });
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-5">
|
||||
<Button type="button" variant="outline" onClick={resetForm}>
|
||||
Reset
|
||||
</Button>
|
||||
<Button variant="default">Save Changes</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</DialogBody>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default AddDialog;
|
||||
74
src/pages/master/aldeias/blocks/DeleteDialog.tsx
Normal file
74
src/pages/master/aldeias/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,74 @@
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||
import { Alert, useDataGrid } from '@/components';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { ChangeEvent, useCallback, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@/components/ui/dialog';
|
||||
import { EnforceSwitch } from '@/components/switch';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
const DeleteDialog = () => {
|
||||
const { showDeleteDialog, handleDeleteDialog, selectedAldeias } = useManageAldeiasContext();
|
||||
const { reload } = useDataGrid();
|
||||
const { DeleteData } = useCallApi();
|
||||
const [enforce, setEnforce] = useState(false);
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
})
|
||||
|
||||
const doDeleteAldeias = useCallback(async () => {
|
||||
const response = await DeleteData(`${API_URL}/aldeias/delete/${selectedAldeias}/${enforce}`, {
|
||||
id: selectedAldeias
|
||||
});
|
||||
|
||||
if (response?.status) {
|
||||
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||
handleDeleteDialog(false, null);
|
||||
toast.success('Success Delete Aldeias');
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Failed Delete Aldeias');
|
||||
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||
}
|
||||
}, [selectedAldeias, enforce]);
|
||||
|
||||
return (
|
||||
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||
<DialogHeader className="p-0 border-0 block">
|
||||
<Alert variant="warning">
|
||||
<h3 className="text-lg">Are you sure?</h3>
|
||||
<span className="text-sm">you will delete this data!</span>
|
||||
<div className="mt-2 flex items-center gap-x-2">
|
||||
<label className="form-label max-w-56">Hard Delete</label>
|
||||
<EnforceSwitch
|
||||
enforce={enforce}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
setEnforce(e.target.checked);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</Alert>
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
</DialogHeader>
|
||||
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant={'destructive'} onClick={() => doDeleteAldeias()}>
|
||||
Delete
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default DeleteDialog;
|
||||
148
src/pages/master/aldeias/blocks/EditDialog.tsx
Normal file
148
src/pages/master/aldeias/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,148 @@
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||
import { Alert, useDataGrid } from '@/components';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { getAuth } from '@/auth';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
Dialog,
|
||||
DialogBody,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
|
||||
const EditDialog = () => {
|
||||
const { showEditDialog, handleEditDialog, selectedAldeias } = useManageAldeiasContext();
|
||||
const { reload } = useDataGrid();
|
||||
const { PutData } = useCallApi();
|
||||
const parsedUser = getAuth()?.user;
|
||||
const [alert, setAlert] = useState({
|
||||
show: false,
|
||||
message: ''
|
||||
});
|
||||
const initialState = {
|
||||
name: '',
|
||||
sucos: 0,
|
||||
updated_by: '',
|
||||
updated_at: ''
|
||||
};
|
||||
const [formField, setFormField] = useState(initialState);
|
||||
const created_time = new Date();
|
||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
const resetForm = () => {
|
||||
setFormField(initialState);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
const doUpdateAldeias = useCallback(
|
||||
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
const response = await PutData(`${API_URL}/aldeias/update/${selectedAldeias}`, formField);
|
||||
|
||||
if (response?.status) {
|
||||
resetForm();
|
||||
handleEditDialog(false, null);
|
||||
toast.success('Success Update Aldeias');
|
||||
reload();
|
||||
} else {
|
||||
toast.error('Error Update Aldeias');
|
||||
setAlert({ show: true, message: 'Error Update Aldeias' });
|
||||
}
|
||||
},
|
||||
[selectedAldeias, formField]
|
||||
);
|
||||
|
||||
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (formField.name === '' || formField.sucos === 0) {
|
||||
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||
return;
|
||||
}
|
||||
|
||||
// doUpdateAldeias(e);
|
||||
console.log(formField);
|
||||
setAlert({ show: false, message: '' });
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (showEditDialog) {
|
||||
setFormField({
|
||||
...formField,
|
||||
updated_by: parsedUser?.username,
|
||||
updated_at: formattedTime
|
||||
});
|
||||
}
|
||||
}, [formattedTime]);
|
||||
|
||||
return (
|
||||
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
|
||||
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Aldeias - Update</DialogTitle>
|
||||
<DialogDescription></DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogBody>
|
||||
<div className="flex flex-col">
|
||||
{alert.show && (
|
||||
<Alert variant="danger">
|
||||
<h3>{alert.message}</h3>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleUpdate}>
|
||||
<div className="card-body grid gap-5">
|
||||
<div className="w-full">
|
||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label className="form-label flex items-center gap-1 max-w-56">
|
||||
Name<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
className="input"
|
||||
type="text"
|
||||
value={formField.name}
|
||||
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label className="form-label flex items-center gap-1 max-w-56">
|
||||
Sucos ID<span className="text-red-500">*</span>
|
||||
</label>
|
||||
<Input
|
||||
className="input"
|
||||
type="number"
|
||||
min={0}
|
||||
value={formField.sucos === 0 ? '' : formField.sucos}
|
||||
onChange={(e) => {
|
||||
const value = parseInt(e.target.value, 10);
|
||||
setFormField({ ...formField, sucos: isNaN(value) ? 0 : value });
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button className="btn btn-primary">Save Changes</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</DialogBody>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default EditDialog;
|
||||
@ -0,0 +1,62 @@
|
||||
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||
|
||||
const ListToolbar = () => {
|
||||
const { table, reload } = useDataGrid();
|
||||
const { handleAddDialog, handleSearchDialog } = useManageAldeiasContext();
|
||||
|
||||
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 Aldeias"
|
||||
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 Sucos
|
||||
</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;
|
||||
|
||||
196
src/pages/master/aldeias/hooks/ManageAldeiasContext.tsx
Normal file
196
src/pages/master/aldeias/hooks/ManageAldeiasContext.tsx
Normal file
@ -0,0 +1,196 @@
|
||||
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||
import { apiConfig } from '@/config/api.config';
|
||||
import { useCallApi } from '@/hooks';
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||
import { Toaster } from 'sonner';
|
||||
import ListToolbar from '../blocks/ListToolbar';
|
||||
|
||||
interface AldeiasProps {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface ContextProps {
|
||||
aldeias: AldeiasProps[];
|
||||
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;
|
||||
selectedAldeias: string | null;
|
||||
getAldeiasLists: (
|
||||
limit: number,
|
||||
page: number,
|
||||
with_deleted: boolean,
|
||||
order_field: any,
|
||||
order_direction: any
|
||||
) => Promise<{ data: AldeiasProps[]; totalCount: number } | undefined>;
|
||||
}
|
||||
|
||||
const initialProps: ContextProps = {
|
||||
aldeias: [],
|
||||
showSearchDialog: false,
|
||||
handleSearchDialog: () => {},
|
||||
showEditDialog: false,
|
||||
handleEditDialog: () => {},
|
||||
showAddDialog: false,
|
||||
handleAddDialog: () => {},
|
||||
showDeleteDialog: false,
|
||||
handleDeleteDialog: () => {},
|
||||
selectedAldeias: null,
|
||||
getAldeiasLists: async () => undefined
|
||||
};
|
||||
|
||||
const ManageAldeiasContext = createContext<ContextProps>(initialProps);
|
||||
const API_URL = apiConfig.service_master_data;
|
||||
|
||||
const ManageAldeiasContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const [aldeias, setAldeias] = useState<AldeiasProps[]>([]);
|
||||
const [showSearchDialog, setShowSearchDialog] = useState(false);
|
||||
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||
const [selectedAldeias, setSelectedAldeias] = useState<string | null>(null);
|
||||
const { GetData } = useCallApi();
|
||||
|
||||
const handleAddDialog = useCallback((show: boolean) => {
|
||||
setShowAddDialog(show);
|
||||
}, []);
|
||||
|
||||
const handleEditDialog = useCallback((show: boolean, selected_aldeias: string | null) => {
|
||||
setSelectedAldeias(show ? selected_aldeias : null);
|
||||
setShowEditDialog(show);
|
||||
}, []);
|
||||
|
||||
const handleDeleteDialog = useCallback((show: boolean, selected_aldeias: string | null) => {
|
||||
setSelectedAldeias(show ? selected_aldeias : null);
|
||||
setShowDeleteDialog(show);
|
||||
}, []);
|
||||
|
||||
const handleSearchDialog = useCallback((show: boolean) => {
|
||||
setShowSearchDialog(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="Name" column={column} />,
|
||||
enableSorting: true,
|
||||
enableHiding: false,
|
||||
meta: {
|
||||
headerClassName: 'w-[250px]'
|
||||
}
|
||||
},
|
||||
{
|
||||
accessorFn: (row) => row.sucos.name,
|
||||
id: 'sucos',
|
||||
header: ({ column }) => <DataGridColumnHeader title="Sucos" column={column} />,
|
||||
enableSorting: true,
|
||||
enableHiding: false,
|
||||
meta: {
|
||||
headerClassName: 'w-[250px]'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||
enableSorting: false,
|
||||
enableHiding: false,
|
||||
cell: (data) => {
|
||||
const row = data.row.original;
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||
onClick={() => handleEditDialog(true, row.id)}
|
||||
>
|
||||
<KeenIcon icon="notepad-edit" />
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||
onClick={() => handleDeleteDialog(true, row.id)}
|
||||
>
|
||||
<KeenIcon icon="trash" />
|
||||
</button>
|
||||
</>
|
||||
);
|
||||
},
|
||||
meta: {
|
||||
headerClassName: 'w-[100px]',
|
||||
cellClassName: 'text-center'
|
||||
}
|
||||
}
|
||||
],
|
||||
[]
|
||||
);
|
||||
|
||||
const getAldeiasLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
||||
try {
|
||||
const response = await GetData(`${API_URL}/aldeias/list`, {
|
||||
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);
|
||||
setAldeias(response?.data.list);
|
||||
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||
} catch (error) {
|
||||
console.error('Error fetching Aldeias', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ManageAldeiasContext.Provider
|
||||
value={{
|
||||
aldeias,
|
||||
showSearchDialog,
|
||||
handleSearchDialog,
|
||||
showEditDialog,
|
||||
handleEditDialog,
|
||||
showAddDialog,
|
||||
handleAddDialog,
|
||||
showDeleteDialog,
|
||||
handleDeleteDialog,
|
||||
selectedAldeias,
|
||||
getAldeiasLists
|
||||
}}
|
||||
>
|
||||
<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 }) =>
|
||||
getAldeiasLists(pageIndex, pageSize, sorting, columnFilters)
|
||||
}
|
||||
>
|
||||
{children}
|
||||
</DataGridProvider>
|
||||
</ManageAldeiasContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { ManageAldeiasContext, ManageAldeiasContextProvider };
|
||||
export type { AldeiasProps };
|
||||
12
src/pages/master/aldeias/hooks/useManageAldeiasContext.tsx
Normal file
12
src/pages/master/aldeias/hooks/useManageAldeiasContext.tsx
Normal file
@ -0,0 +1,12 @@
|
||||
import { useContext } from 'react';
|
||||
import { ManageAldeiasContext } from './ManageAldeiasContext';
|
||||
|
||||
const useManageAldeiasContext = () => {
|
||||
const context = useContext(ManageAldeiasContext);
|
||||
|
||||
if (!context) throw new Error('useManageAldeiasContext must be used within AuthProvider');
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
export { useManageAldeiasContext };
|
||||
Reference in New Issue
Block a user