add crud municipio

This commit is contained in:
Wikzyy
2025-03-03 11:59:32 +07:00
parent f0eb8803b2
commit a8a270ba7f
7 changed files with 338 additions and 22 deletions

View File

@ -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<any | null>(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<HTMLFormElement>) => {
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<HTMLFormElement>) => {
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 (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-96 flex flex-col p-5 overflow-hidden [&>button]:hidden">
@ -96,7 +154,7 @@ const AddDialog = () => {
Reset
</Button>
<Button variant={'default'} type="submit">
Save Changes
Create
</Button>
</div>
</div>