feat add delete

This commit is contained in:
Wikzyy
2025-03-26 10:55:26 +07:00
parent e44b20635c
commit 43bc6f06f7
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,76 @@
import { useCallApi } from '@/hooks';
import { useManageWalletRuleContext } from '../hooks/useManageWalletRuleContext';
import { Alert, useDataGrid } from '@/components';
import { useCallback, useState } from 'react';
import { toast } from 'sonner';
import { apiConfig } from '@/config/api.config';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
const API_URL_WALLET = apiConfig.service_wallet;
const DeleteDialog = () => {
const { showDeleteDialog, handleDeleteDialog, selectedWalletRule } = useManageWalletRuleContext();
const { DeleteData } = useCallApi();
const { reload } = useDataGrid();
const [alert, setAlert] = useState({ show: false, message: '' });
const doDeleteWalletRule = useCallback(async () => {
if (!selectedWalletRule) {
toast.error('No wallet rule selected');
return;
}
const response = await DeleteData(
`${API_URL_WALLET}/dashboard/wallet_rule/${selectedWalletRule}`,
{ id: selectedWalletRule }
);
if (response?.status) {
toast.success('Wallet rule deleted successfully');
handleDeleteDialog(false, null);
reload();
setAlert({ show: false, message: '' });
} else {
toast.error('Failed to delete wallet rule');
setAlert({ show: true, message: response?.message });
}
}, [selectedWalletRule]);
return (
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-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>
</Alert>
{alert.show && (
<Alert variant="danger">
<h3>{alert.message}</h3>
</Alert>
)}
</DialogHeader>
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
<Button variant="outline" onClick={() => handleDeleteDialog(false, null)}>
Cancel
</Button>
<Button variant="destructive" onClick={doDeleteWalletRule}>
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
export default DeleteDialog;