add form handling add municipio

This commit is contained in:
Wikzyy
2025-02-27 16:33:06 +07:00
parent 7e511baece
commit 3d9ec24d5f
5 changed files with 135 additions and 30 deletions

View File

@ -1,12 +1,16 @@
import { Container } from '@/components'; import { Container, DataGridInner } from '@/components';
import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext'; import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext';
import MunicipiosContent from './MunicipiosContent'; import AddDialog from './blocks/AddDialog';
const Municipios = () => { const Municipios = () => {
return ( return (
<ManageMunicipiosProvider> <ManageMunicipiosProvider>
<Container> <Container className="mb-7">
<MunicipiosContent /> <h1 className="text-xl font-medium leading-none text-gray-900 mb-5">MUNICIPIOS</h1>
<div className="grid gap-5 lg:gap-7.5">
<DataGridInner />
</div>
<AddDialog />
</Container> </Container>
</ManageMunicipiosProvider> </ManageMunicipiosProvider>
); );

View File

@ -1,16 +0,0 @@
import { DataGridInner } from '@/components';
import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext';
import { useManageMunicipiosContext } from './hooks/useManageMunicipiosContext';
const MunicipiosContent = () => {
return (
<div>
<h1>Municipios</h1>
<div className="grid gap-5 lg:gap-7.5">
<DataGridInner />
</div>
</div>
);
};
export default MunicipiosContent;

View File

@ -0,0 +1,111 @@
import React, { useRef, useState } from 'react';
import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext';
import {
Dialog,
DialogBody,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog';
import { Alert, KeenIcon } from '@/components';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
const AddDialog = () => {
const parentRef = useRef<any | null>(null);
const { showAddDialog, handleAddDialog } = useManageMunicipiosContext();
const [alert, setAlert] = useState({
show: false,
message: ''
});
const initialState = {
name: ''
};
const [formField, setFormField] = useState(initialState);
const resetForm = () => {
setFormField(initialState);
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (formField.name === '') {
setAlert({ show: true, message: 'Please fill name field.' });
return;
}
console.log(formField);
setAlert({ show: false, message: '' });
};
const handleReset = () => {
setFormField(initialState);
};
return (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-96 flex flex-col p-5 overflow-hidden [&>button]:hidden">
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
<DialogHeader className="p-2 border-0">
<div className="flex items-center justify-between flex-wrap grow">
<div className="flex flex-col justify-center">
<h1 className="text-xl font-semibold leading-none text-gray-900">Add Municipios</h1>
<div className="flex items-center gap-2 text-sm font-normal text-gray-700"></div>
</div>
<div
className="cursor-pointer hover:opacity-100 opacity-50"
onClick={() => {
handleAddDialog(false);
resetForm();
}}
>
<KeenIcon icon="cross" className="text-1.5xl" />
</div>
</div>
</DialogHeader>
<DialogBody className="scrollable-y px-0 pb-0" ref={parentRef}>
<div className="flex flex-col px-0">
{alert.show && (
<Alert variant="danger" className="mb-5">
{alert.message}
</Alert>
)}
<form action="" onSubmit={handleSubmit}>
<div className="card-body grid-cols-6 gap-5 p-0">
<div className="grid grid-cols-8 gap-2 w-full items-center">
<label className="form-label flex items-center gap-1 col-span-2">
Name<span className="text-red-500">*</span>
</label>
<Input
className="input col-span-6"
type="text"
autoComplete="off"
value={formField.name}
onChange={({ target }) =>
setFormField((prev) => ({ ...prev, name: target.value }))
}
/>
</div>
<div className="flex justify-end pt-2.5 gap-5 col-span-6">
<Button variant={'outline'} type="reset" onClick={handleReset}>
Reset
</Button>
<Button variant={'default'} type="submit">
Save Changes
</Button>
</div>
</div>
</form>
</div>
</DialogBody>
</DialogContent>
</Dialog>
);
};
export default AddDialog;

View File

@ -15,7 +15,7 @@ const ListToolbar = () => {
<KeenIcon icon="magnifier" /> <KeenIcon icon="magnifier" />
<input <input
type="text" type="text"
placeholder="Search users" placeholder="Search Municipios"
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''} value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
onChange={(event) => onChange={(event) =>
table.getColumn('name')?.setFilterValue(event.target.value) table.getColumn('name')?.setFilterValue(event.target.value)

View File

@ -96,12 +96,22 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) =
{ {
accessorFn: (row) => row.name, accessorFn: (row) => row.name,
id: 'name', id: 'name',
header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />, header: ({ column }) => <DataGridColumnHeader title="Municipios Name" column={column} />,
enableSorting: true, enableSorting: true,
enableHiding: false, enableHiding: false,
meta: { meta: {
headerClassName: 'w-[250px]' headerClassName: 'w-[250px]'
} }
},
{
accessorFn: (row) => row.postoadms,
id: 'postoadms',
header: ({ column }) => <DataGridColumnHeader title="Postoadms" column={column} />,
enableSorting: true,
enableHiding: false,
meta: {
headerClassName: 'w-[100px]'
}
} }
], ],
[handleEditDialog, handleDeleteDialog] [handleEditDialog, handleDeleteDialog]
@ -113,21 +123,21 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) =
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() }; filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
const response = await axios.get(`${API_URL}/municipios/list`, { const response = await axios.get(`${API_URL}/municipios/list`, {
params: { params: {
limit, limit: limit,
page: 1, page: page + 1,
with_deleted: false, with_deleted: false,
order_field: sorting[0].id, order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
} }
}); });
console.log(response.data); console.log(response.data);
return { data: response?.data.data.list, totalCount: response?.data.total_count }; return { data: response?.data.data.list, totalCount: response?.data.data.total_count };
} catch (error) { } catch (error) {
console.error('Error fetching municipios', error); console.error('Error fetching municipios', error);
} }
}; };
const getMunicipiosByName = async (name: string) => { const getPostoadmsByMunicipio = async (name: string) => {
try { try {
const response = await axios.get(`${API_URL}/municipios/postoadms/${name}`); const response = await axios.get(`${API_URL}/municipios/postoadms/${name}`);
const data = response.data; const data = response.data;
@ -173,10 +183,6 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) =
} }
}; };
// useEffect(() => {
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
// }, []);
console.log(municipios);
return ( return (
<ManageMunicipiosContext.Provider <ManageMunicipiosContext.Provider
value={{ value={{