add crud postoadm
This commit is contained in:
@ -1,3 +1,6 @@
|
|||||||
|
import AddDialog from './blocks/AddDialog';
|
||||||
|
import DeleteDialog from './blocks/DeleteDialog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
import SearchDialog from './blocks/SearchDialog';
|
import SearchDialog from './blocks/SearchDialog';
|
||||||
import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext';
|
import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext';
|
||||||
import { Container, DataGridInner } from '@/components';
|
import { Container, DataGridInner } from '@/components';
|
||||||
@ -6,10 +9,15 @@ const PostoAdmsMaster = () => {
|
|||||||
return (
|
return (
|
||||||
<ManagePostoAdmsContextProvider>
|
<ManagePostoAdmsContextProvider>
|
||||||
<Container>
|
<Container>
|
||||||
<h1>Postu Administrativo</h1>
|
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">
|
||||||
|
Postu Administrativo
|
||||||
|
</h1>
|
||||||
<div className="grid gap-5 lg:gap-7.5">
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
<DataGridInner />
|
<DataGridInner />
|
||||||
</div>
|
</div>
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog />
|
||||||
|
<DeleteDialog />
|
||||||
<SearchDialog />
|
<SearchDialog />
|
||||||
</Container>
|
</Container>
|
||||||
</ManagePostoAdmsContextProvider>
|
</ManagePostoAdmsContextProvider>
|
||||||
|
|||||||
160
src/pages/master/postoadms/blocks/AddDialog.tsx
Normal file
160
src/pages/master/postoadms/blocks/AddDialog.tsx
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
import { Alert, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth, useAuthContext } from '@/auth';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
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 } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PostData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
municipio_id: 0,
|
||||||
|
created_by: '',
|
||||||
|
created_at: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setFormField(initialState);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const doCreatePostoAdm = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PostData(`${API_URL}/postoadms/create`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
handleAddDialog(false);
|
||||||
|
resetForm();
|
||||||
|
reload();
|
||||||
|
toast.success('Posto Adm created successfully!');
|
||||||
|
} else {
|
||||||
|
toast.error('Failed to create Posto Adm. Please try again.');
|
||||||
|
setAlert({ show: true, message: 'Failed to create Posto Adm. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.municipio_id === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doCreatePostoAdm(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
// const handleReset = () => {
|
||||||
|
// resetForm();
|
||||||
|
// setAlert({ show: false, message: '' });
|
||||||
|
// };
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const created_time = new Date();
|
||||||
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
if (showAddDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
created_by: parsedUser?.username,
|
||||||
|
created_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [showAddDialog]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||||
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Postu Administrativo - 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">
|
||||||
|
Municipio ID<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
value={formField.municipio_id === 0 ? '' : formField.municipio_id}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value, 10);
|
||||||
|
setFormField({ ...formField, municipio_id: 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;
|
||||||
77
src/pages/master/postoadms/blocks/DeleteDialog.tsx
Normal file
77
src/pages/master/postoadms/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ChangeEvent, useCallback, useState } from 'react';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
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, selectedPostoAdms } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [enforce, setEnforce] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const doDeletePostoAdm = useCallback(async () => {
|
||||||
|
const response = await DeleteData(
|
||||||
|
`${API_URL}/postoadms/delete/${selectedPostoAdms}/${enforce}`,
|
||||||
|
{
|
||||||
|
id: selectedPostoAdms
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Posto Adm');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||||
|
}
|
||||||
|
}, [selectedPostoAdms, 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={() => doDeletePostoAdm()}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
149
src/pages/master/postoadms/blocks/EditDialog.tsx
Normal file
149
src/pages/master/postoadms/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
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 parentRef = useRef<any | null>(null);
|
||||||
|
const { showEditDialog, handleEditDialog, selectedPostoAdms } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PutData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
municipio_id: 0,
|
||||||
|
updated_by: '',
|
||||||
|
updated_at: ''
|
||||||
|
};
|
||||||
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setFormField(initialState);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const doUpdatePostoAdm = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PutData(`${API_URL}/postoadms/update/${selectedPostoAdms}`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
resetForm();
|
||||||
|
toast.success('Success Update Posto Adm');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Update Posto Adm');
|
||||||
|
setAlert({ show: true, message: 'Failed to update posto adm. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[selectedPostoAdms, formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.municipio_id === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doUpdatePostoAdm(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const created_time = new Date();
|
||||||
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
|
||||||
|
if (selectedPostoAdms) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
updated_by: parsedUser?.username,
|
||||||
|
updated_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [selectedPostoAdms]);
|
||||||
|
|
||||||
|
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>Municipios - 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</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">
|
||||||
|
Municipio ID
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
value={formField.municipio_id === 0 ? '' : formField.municipio_id}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value, 10);
|
||||||
|
setFormField({ ...formField, municipio_id: 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;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import { DataGridColumnHeader, DataGridProvider } from '@/components';
|
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Toaster } from '@/components/ui/sonner';
|
import { Toaster } from '@/components/ui/sonner';
|
||||||
import { apiConfig } from '@/config/api.config';
|
import { apiConfig } from '@/config/api.config';
|
||||||
@ -106,18 +106,29 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
|
|||||||
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
enableSorting: false,
|
enableSorting: false,
|
||||||
enableHiding: false,
|
enableHiding: false,
|
||||||
meta: {
|
cell: (data) => {
|
||||||
headerClassName: 'w-[100px], text-center',
|
const row = data.row.original;
|
||||||
cellClassName: 'text-center'
|
return (
|
||||||
},
|
<>
|
||||||
cell: (info) => (
|
<button
|
||||||
<Button
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
variant={'outline'}
|
onClick={() => handleEditDialog(true, row.id)}
|
||||||
onClick={() => navigate(`/master-data/municipios/postoadms/${info.row.original.id}`)}
|
|
||||||
>
|
>
|
||||||
Details
|
<KeenIcon icon="notepad-edit" />
|
||||||
</Button>
|
</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'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[handleEditDialog, handleDeleteDialog]
|
[handleEditDialog, handleDeleteDialog]
|
||||||
|
|||||||
Reference in New Issue
Block a user