This commit is contained in:
Raja Oktafrianto
2025-03-18 11:27:23 +07:00
parent 8026d6910a
commit 1445fc8ecf
28 changed files with 446 additions and 316 deletions

View File

@ -42,6 +42,7 @@ const AddDialog = () => {
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
const doCreateMunicipio = useCallback(
@ -72,7 +73,7 @@ const AddDialog = () => {
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (formField.name === '') {
if (formField.name.trim() === '') {
setAlert({ show: true, message: 'Please fill name field.' });
return;
}

View File

@ -25,7 +25,7 @@ const EditDialog = () => {
const { showEditDialog, handleEditDialog, selectedMunicipios, municipios } =
useManageMunicipiosContext();
const { reload } = useDataGrid();
const { PutData } = useCallApi();
const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [alert, setAlert] = useState({
show: false,
@ -44,6 +44,7 @@ const EditDialog = () => {
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
const doUpdateMunicipios = useCallback(
@ -74,28 +75,46 @@ const EditDialog = () => {
[selectedMunicipios, formField]
);
const doFetchData = useCallback(async (id: string) => {
const response = await GetData(`${API_URL}/municipios/getdata/${id}`, { id });
if (response?.status) {
setFormField((prev) => ({
...prev,
name: response.data.name
}));
} else {
setFormField((prev) => ({
...prev,
name: ''
}));
}
}, []);
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (formField.name === '') {
if (formField.name.trim() === '') {
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(() => {
if (selectedMunicipios) {
doFetchData(selectedMunicipios);
}
}, [selectedMunicipios]);
useEffect(() => {
if (showEditDialog === false) {
resetForm();
}
}, [showEditDialog]);
useEffect(() => {
if (selectedMunicipios) {
@ -127,7 +146,9 @@ const EditDialog = () => {
<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>
<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"

View File

@ -29,8 +29,8 @@ const ListToolbar = () => {
<input
type="text"
placeholder="Search Municipios"
value={searchName}
onChange={(event) => setSearchName(event.target.value)}
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
/>
</label>
<DefaultTooltip title={'Filter'} placement={'top'}>

View File

@ -113,8 +113,9 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) =
}
},
{
accessorFn: (row) => row.name,
id: 'name',
// accessorFn: (row) => row.name,
// id: 'name',
accessorKey: 'name',
header: ({ column }) => <DataGridColumnHeader title="Municipios Name" column={column} />,
enableSorting: true,
enableHiding: false,
@ -159,73 +160,27 @@ const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) =
try {
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
const response = await axios.get(`${API_URL}/municipios/list`, {
params: {
limit: limit,
page: page + 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
}
const response = await GetData(`${API_URL}/municipios/list`, {
limit,
page: page + 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
filter: JSON.stringify(filter)
});
// console.log(response.data);
// 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 };
setMunicipios(response?.data.list);
return { data: response?.data.list, totalCount: response?.data.total_count };
} catch (error) {
console.error('Error fetching municipios', error);
}
};
const getPostoadmsByMunicipio = async (name: string) => {
try {
const response = await axios.get(`${API_URL}/municipios/postoadms/${name}`);
const data = response.data;
console.log(data);
} catch (error) {
console.error(`Error fetching municipios by ${name}`, error);
}
};
const createMunicipios = async (data: Partial<MunicipiosProps>) => {
try {
await axios.post(`${API_URL}/municipios/create`, data);
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
} catch (error) {
console.error('Error creating municipios', error);
}
};
const updateMunicipios = async (id: number, data: Partial<MunicipiosProps>) => {
try {
await axios.put(`${API_URL}/update/${id}`, data);
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
} catch (error) {
console.error('Error updating municipios', error);
}
};
const deleteMunicipios = async (id: number, hardDelete?: boolean) => {
try {
await axios.delete(`${API_URL}/delete/${id}/${hardDelete}`);
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
} catch (error) {
console.error('Error deleting municipios', error);
}
};
const restoreMunicipios = async (id: number) => {
try {
await axios.put(`${API_URL}/restore/${id}`);
// getMunicipiosLists(10, 1, false, 'name', 'ASC');
} catch (error) {
console.error('Error restoring municipios', error);
}
};
return (
<ManageMunicipiosContext.Provider
value={{