From 8758472b99ff281f2f463ddc9199295ccbeec1dc Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Tue, 4 Mar 2025 08:44:49 +0700 Subject: [PATCH] add crud postoadm --- .../master/postoadms/PostoAdmsMaster.tsx | 10 +- .../master/postoadms/blocks/AddDialog.tsx | 160 ++++++++++++++++++ .../master/postoadms/blocks/DeleteDialog.tsx | 77 +++++++++ .../master/postoadms/blocks/EditDialog.tsx | 149 ++++++++++++++++ .../hooks/ManagePostoAdmsContext.tsx | 35 ++-- 5 files changed, 418 insertions(+), 13 deletions(-) create mode 100644 src/pages/master/postoadms/blocks/AddDialog.tsx create mode 100644 src/pages/master/postoadms/blocks/DeleteDialog.tsx create mode 100644 src/pages/master/postoadms/blocks/EditDialog.tsx diff --git a/src/pages/master/postoadms/PostoAdmsMaster.tsx b/src/pages/master/postoadms/PostoAdmsMaster.tsx index 829f5dd..3ff1138 100644 --- a/src/pages/master/postoadms/PostoAdmsMaster.tsx +++ b/src/pages/master/postoadms/PostoAdmsMaster.tsx @@ -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 { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext'; import { Container, DataGridInner } from '@/components'; @@ -6,10 +9,15 @@ const PostoAdmsMaster = () => { return ( -

Postu Administrativo

+

+ Postu Administrativo +

+ + +
diff --git a/src/pages/master/postoadms/blocks/AddDialog.tsx b/src/pages/master/postoadms/blocks/AddDialog.tsx new file mode 100644 index 0000000..3849e1c --- /dev/null +++ b/src/pages/master/postoadms/blocks/AddDialog.tsx @@ -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(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) => { + 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) => { + 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 ( + handleAddDialog(open)}> + + + Postu Administrativo - Create + + + +
+ {alert.show && ( + +

{alert.message}

+
+ )} + +
+
+
+
+ + setFormField({ ...formField, name: e.target.value })} + /> +
+
+ +
+
+ + { + const value = parseInt(e.target.value, 10); + setFormField({ ...formField, municipio_id: isNaN(value) ? 0 : value }); + }} + /> +
+
+ +
+ + +
+
+
+
+
+
+
+ ); +}; + +export default AddDialog; diff --git a/src/pages/master/postoadms/blocks/DeleteDialog.tsx b/src/pages/master/postoadms/blocks/DeleteDialog.tsx new file mode 100644 index 0000000..f969485 --- /dev/null +++ b/src/pages/master/postoadms/blocks/DeleteDialog.tsx @@ -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 ( + handleDeleteDialog(open, null)}> + + + +

Are you sure?

+ you will delete this data! +
+ + ) => { + setEnforce(e.target.checked); + }} + /> +
+
+ {alert.show && ( + +

{alert.message}

+
+ )} +
+ + + + +
+
+ ); +}; + +export default DeleteDialog; diff --git a/src/pages/master/postoadms/blocks/EditDialog.tsx b/src/pages/master/postoadms/blocks/EditDialog.tsx new file mode 100644 index 0000000..05dc645 --- /dev/null +++ b/src/pages/master/postoadms/blocks/EditDialog.tsx @@ -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(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) => { + 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) => { + 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 ( + handleEditDialog(open, null)}> + + + Municipios - Update + + + +
+ {alert.show && ( + +

{alert.message}

+
+ )} + +
+
+
+
+ + setFormField({ ...formField, name: e.target.value })} + /> +
+
+ +
+
+ + { + const value = parseInt(e.target.value, 10); + setFormField({ ...formField, municipio_id: isNaN(value) ? 0 : value }); + }} + /> +
+
+ +
+ +
+
+
+
+
+
+
+ ); +}; + +export default EditDialog; diff --git a/src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx b/src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx index 973817c..971470b 100644 --- a/src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx +++ b/src/pages/master/postoadms/hooks/ManagePostoAdmsContext.tsx @@ -1,4 +1,4 @@ -import { DataGridColumnHeader, DataGridProvider } from '@/components'; +import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components'; import { Button } from '@/components/ui/button'; import { Toaster } from '@/components/ui/sonner'; import { apiConfig } from '@/config/api.config'; @@ -106,18 +106,29 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod header: ({ column }) => , enableSorting: false, enableHiding: false, - meta: { - headerClassName: 'w-[100px], text-center', - cellClassName: 'text-center' + cell: (data) => { + const row = data.row.original; + return ( + <> + + + + ); }, - cell: (info) => ( - - ) + meta: { + headerClassName: 'w-[100px]', + cellClassName: 'text-center' + } } ], [handleEditDialog, handleDeleteDialog]