diff --git a/src/pages/master/aldeias/blocks/ListToolbar.tsx b/src/pages/master/aldeias/blocks/ListToolbar.tsx new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/master/municipios/Municipios.tsx b/src/pages/master/municipios/Municipios.tsx index 0538fbd..820900c 100644 --- a/src/pages/master/municipios/Municipios.tsx +++ b/src/pages/master/municipios/Municipios.tsx @@ -2,6 +2,8 @@ import { Container, DataGridInner } from '@/components'; import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext'; import AddDialog from './blocks/AddDialog'; import SearchDialog from './blocks/SearchDialog'; +import EditDialog from './blocks/EditDialog'; +import DeleteDialog from './blocks/DeleteDialog'; const Municipios = () => { return ( @@ -12,6 +14,8 @@ const Municipios = () => { + + diff --git a/src/pages/master/municipios/blocks/AddDialog.tsx b/src/pages/master/municipios/blocks/AddDialog.tsx index 2916f94..9a9a342 100644 --- a/src/pages/master/municipios/blocks/AddDialog.tsx +++ b/src/pages/master/municipios/blocks/AddDialog.tsx @@ -1,4 +1,4 @@ -import React, { useRef, useState } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext'; import { Dialog, @@ -8,19 +8,32 @@ import { DialogHeader, DialogTitle } from '@/components/ui/dialog'; -import { Alert, KeenIcon } from '@/components'; +import { Alert, KeenIcon, useDataGrid } from '@/components'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; +import axios from 'axios'; +import { apiConfig } from '@/config/api.config'; +import { toast } from 'sonner'; +import { getAuth, useAuthContext } from '@/auth'; +import { useCallApi } from '@/hooks'; +import { doSaveLogActivity } from '@/actions/GlobalActions'; + +const API_URL = apiConfig.service_master_data; const AddDialog = () => { const parentRef = useRef(null); - const { showAddDialog, handleAddDialog } = useManageMunicipiosContext(); + const { reload } = useDataGrid(); + const { PostData } = useCallApi(); + const parsedUser = getAuth()?.user; + const { showAddDialog, handleAddDialog, selectedMunicipios } = useManageMunicipiosContext(); const [alert, setAlert] = useState({ show: false, message: '' }); const initialState = { - name: '' + name: '', + created_by: '', + created_at: '' }; const [formField, setFormField] = useState(initialState); @@ -28,6 +41,31 @@ const AddDialog = () => { setFormField(initialState); }; + const doCreateMunicipio = useCallback( + async (e: React.FormEvent) => { + e.preventDefault(); + const response = await PostData(`${API_URL}/municipios/create`, formField); + + if (response?.status) { + handleAddDialog(false); + resetForm(); + toast.success('Municipio created successfully!'); + reload(); + // const createActivity = { + // module: 'Manage Municipio', + // description: `Create Municipio => ${selectedMunicipios}`, + // action: 'C' + // }; + + // doSaveLogActivity(createActivity); + } else { + toast.error('Failed to create municipio.'); + setAlert({ show: true, message: 'Failed to create municipio. Please try again.' }); + } + }, + [formField] + ); + const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -36,6 +74,14 @@ const AddDialog = () => { return; } + // setFormField({ + // name: formField.name, + // created_by: parsedUser.email, + // created_at: formattedTime + // }); + + // doCreateMunicipio(e); + console.log(parsedUser.email); console.log(formField); setAlert({ show: false, message: '' }); }; @@ -44,6 +90,18 @@ const AddDialog = () => { setFormField(initialState); }; + useEffect(() => { + const created_time = new Date(); + const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' '); + if (showAddDialog) { + setFormField({ + name: formField.name, + created_by: parsedUser.email, + created_at: formattedTime + }); + } + }, [showAddDialog]); + return ( handleAddDialog(open)}> @@ -96,7 +154,7 @@ const AddDialog = () => { Reset diff --git a/src/pages/master/municipios/blocks/DeleteDialog.tsx b/src/pages/master/municipios/blocks/DeleteDialog.tsx new file mode 100644 index 0000000..0605ab5 --- /dev/null +++ b/src/pages/master/municipios/blocks/DeleteDialog.tsx @@ -0,0 +1,87 @@ +import { Alert, useDataGrid } from '@/components'; +import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext'; +import { ChangeEvent, useCallback, useState } from 'react'; +import axios from 'axios'; +import { apiConfig } from '@/config/api.config'; +import { useCallApi } from '@/hooks'; +import { toast } from 'sonner'; +import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@/components/ui/dialog'; +import { EnforceSwitch } from '@/components/switch'; +import { Button } from '@/components/ui/button'; +import { doSaveLogActivity } from '@/actions/GlobalActions'; + +const API_URL = apiConfig.service_master_data; + +const DeleteDialog = () => { + const { showDeleteDialog, handleDeleteDialog, selectedMunicipios, municipios } = + useManageMunicipiosContext(); + const { reload } = useDataGrid(); + const { DeleteData } = useCallApi(); + const [enforce, setEnforce] = useState(false); + const [alert, setAlert] = useState({ + show: false, + message: '' + }); + + const doDeleteMunicipio = useCallback(async () => { + const response = await DeleteData( + `${API_URL}/municipios/delete/${selectedMunicipios}/${enforce}`, + { + id: selectedMunicipios + } + ); + + if (response?.status) { + setAlert((prev) => ({ ...prev, show: false, message: '' })); + handleDeleteDialog(false, null); + toast.success('Success Delete Municipio'); + reload(); + // const createActivity = { + // module: 'Manage Municipio', + // description: `Delete Municipio => ${selectedMunicipios}`, + // action: 'D' + // }; + + // doSaveLogActivity(createActivity); + } else { + setAlert((prev) => ({ ...prev, show: true, message: response?.message })); + } + }, [selectedMunicipios, 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/municipios/blocks/EditDialog.tsx b/src/pages/master/municipios/blocks/EditDialog.tsx new file mode 100644 index 0000000..afb9c3c --- /dev/null +++ b/src/pages/master/municipios/blocks/EditDialog.tsx @@ -0,0 +1,152 @@ +import React, { useCallback, useEffect, useRef, useState } from 'react'; +import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext'; +import { Alert, useDataGrid } from '@/components'; +import axios from 'axios'; +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'; +import { getAuth, useAuthContext } from '@/auth'; +import { useCallApi } from '@/hooks'; +import { doSaveLogActivity } from '@/actions/GlobalActions'; + +const API_URL = apiConfig.service_master_data; + +const EditDialog = () => { + const parentRef = useRef(null); + const { showEditDialog, handleEditDialog, selectedMunicipios, municipios } = + useManageMunicipiosContext(); + const { reload } = useDataGrid(); + const { PutData } = useCallApi(); + const parsedUser = getAuth()?.user; + const [alert, setAlert] = useState({ + show: false, + message: '' + }); + + const initialState = { + name: '', + updated_by: '', + updated_at: '' + }; + + const [formField, setFormField] = useState(initialState); + + const resetForm = () => { + setFormField(initialState); + }; + + const doUpdateMunicipios = useCallback( + async (e: React.FormEvent) => { + e.preventDefault(); + const response = await PutData( + `${API_URL}/municipios/update/${selectedMunicipios}`, + formField + ); + + if (response?.status) { + handleEditDialog(false, null); + resetForm(); + toast.success('Success update municipio'); + reload(); + // const createActivity = { + // module: 'Manage Municipio', + // description: `Edit Municipio => ${selectedMunicipios}`, + // action: 'U' + // }; + + // doSaveLogActivity(createActivity); + } else { + toast.error('Failed update user'); + setAlert({ show: true, message: 'Failed to update municipio. Please try again.' }); + } + }, + [selectedMunicipios, formField] + ); + + const handleUpdate = (e: React.FormEvent) => { + e.preventDefault(); + + if (formField.name === '') { + setAlert({ show: true, message: 'Please fill name field.' }); + return; + } + + // setFormField({ + // name: formField.name, + // created_by: parsedUser.email, + // created_at: formattedTime + // }); + + doUpdateMunicipios(e); + console.log(formField); + setAlert({ show: false, message: '' }); + }; + + // const doFetchMunicipios = useCallback(async (id: string) => { + // const response = await axios.get(`${API_URL}/municipios/${id}`); + // }, []); + + useEffect(() => { + const created_time = new Date(); + const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' '); + if (selectedMunicipios) { + setFormField({ + name: formField.name, + updated_by: parsedUser.email, + updated_at: formattedTime + }); + } + }, [selectedMunicipios]); + + // console.log(selectedMunicipios); + return ( + handleEditDialog(open, null)}> + + + Municipios - Update + + + +
+ {alert.show && ( + +

{alert.message}

+
+ )} + +
+
+
+
+ + setFormField({ ...formField, name: e.target.value })} + /> +
+
+ +
+ +
+
+
+
+
+
+
+ ); +}; + +export default EditDialog; diff --git a/src/pages/master/municipios/blocks/SearchDialog.tsx b/src/pages/master/municipios/blocks/SearchDialog.tsx index 4ba1a62..42c1ddf 100644 --- a/src/pages/master/municipios/blocks/SearchDialog.tsx +++ b/src/pages/master/municipios/blocks/SearchDialog.tsx @@ -21,7 +21,7 @@ import { SelectValue } from '@/components/ui/select'; -interface PostoAdms { +interface PostoAdmsProps { id: number; name: string; } @@ -44,7 +44,7 @@ const SearchDialog = () => { const resetForm = () => { setFormField(initialState); }; - const [postoadms, setPostoadms] = useState([]); + const [postoadms, setPostoadms] = useState([]); const [isFound, setIsFound] = useState(false); const handleSubmit = async (e: React.FormEvent) => { @@ -62,7 +62,7 @@ const SearchDialog = () => { if (response.data.status) { setPostoadms(response.data.data); setIsFound(true); - console.log('Found postoadms: ', response.data.data); + // console.log('Found postoadms: ', response.data.data); } else { setPostoadms([]); setIsFound(false); @@ -147,7 +147,7 @@ const SearchDialog = () => { {isFound && postoadms.length > 0 && (
-

Postu Administravo:

+

Sucos:


diff --git a/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx b/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx index 808f57e..4d95cf4 100644 --- a/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx +++ b/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx @@ -1,4 +1,4 @@ -import { DataGridColumnHeader, DataGridProvider } from '@/components'; +import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components'; import { Toaster } from '@/components/ui/sonner'; import { apiConfig } from '@/config/api.config'; import { ColumnDef } from '@tanstack/react-table'; @@ -127,18 +127,29 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) = 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] @@ -158,6 +169,11 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) = } }); console.log(response.data); + // const sortedList = response.data.data.list.sort((a: MunicipiosProps, b: MunicipiosProps) => { + // if (a.name < b.name) return -1; + // if (a.name > b.name) return 1; + // return 0; + // }); setMunicipios(response.data.data.list); return { data: response?.data.data.list, totalCount: response?.data.data.total_count }; } catch (error) { @@ -210,7 +226,6 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) = console.error('Error restoring municipios', error); } }; - console.log(municipios); return (