applying soft delete on masterdata, except walletrule and conversion

This commit is contained in:
bagusajisaputroo
2025-03-25 23:10:41 +07:00
parent 8a956ea950
commit 89dc164891
7 changed files with 123 additions and 202 deletions

View File

@ -1,69 +1,56 @@
import { Alert, useDataGrid } from '@/components';
import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext';
import { ChangeEvent, useCallback, useState } from 'react';
import axios from 'axios';
import { useCallback, useState } from 'react';
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 { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { DialogDescription } from '@radix-ui/react-dialog';
const API_URL = apiConfig.service_master_data;
const DeleteDialog = () => {
const { showDeleteDialog, handleDeleteDialog, selectedMunicipios, municipios } =
useManageMunicipiosContext();
const { showDeleteDialog, handleDeleteDialog, selectedMunicipios } = useManageMunicipiosContext();
const { reload } = useDataGrid();
const { DeleteData } = useCallApi();
const [enforce, setEnforce] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
});
const doDeleteMunicipio = useCallback(async () => {
if (!selectedMunicipios) {
toast.error('No Municipio selected');
return;
}
const response = await DeleteData(
`${API_URL}/municipios/delete/${selectedMunicipios}/${enforce}`,
{
id: selectedMunicipios
}
`${API_URL}/municipios/delete/${selectedMunicipios}/false`,
{ id: selectedMunicipios }
);
if (response?.status) {
setAlert((prev) => ({ ...prev, show: false, message: '' }));
setAlert({ 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 }));
setAlert({ show: true, message: response?.message });
toast.error('Failed Delete Municipio');
}
}, [selectedMunicipios, enforce]);
}, [selectedMunicipios, DeleteData, handleDeleteDialog, reload]);
return (
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
<DialogHeader className="p-0 border-0 block">
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
<Alert variant="warning">
<h3 className="text-lg">Are you sure?</h3>
<span className="text-sm">you will delete this data!</span>
<div className="mt-2 flex items-center gap-x-2">
<label className="form-label max-w-56">Hard Delete</label>
<EnforceSwitch
enforce={enforce}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
setEnforce(e.target.checked);
}}
/>
</div>
<span className="text-sm">You will delete this data!</span>
</Alert>
{alert.show && (
<Alert variant="danger">
@ -72,10 +59,10 @@ const DeleteDialog = () => {
)}
</DialogHeader>
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
<Button variant="outline" onClick={() => handleDeleteDialog(false, null)}>
Cancel
</Button>
<Button variant={'destructive'} onClick={() => doDeleteMunicipio()}>
<Button variant="destructive" onClick={doDeleteMunicipio}>
Delete
</Button>
</DialogFooter>