2023-10-08 01:55:14 +00:00
|
|
|
import { Button, Icon, Icons, Modal, ModalTrigger, Text } from 'react-basics';
|
|
|
|
|
import { useApi, useMessages } from 'components/hooks';
|
2024-02-05 03:53:06 +00:00
|
|
|
import { touch } from 'store/modified';
|
2024-01-29 09:32:05 +00:00
|
|
|
import ConfirmationForm from 'components/common/ConfirmationForm';
|
2023-10-08 01:55:14 +00:00
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
export function ReportDeleteButton({
|
|
|
|
|
reportId,
|
|
|
|
|
reportName,
|
|
|
|
|
onDelete,
|
|
|
|
|
}: {
|
|
|
|
|
reportId: string;
|
|
|
|
|
reportName: string;
|
|
|
|
|
onDelete?: () => void;
|
|
|
|
|
}) {
|
2024-01-29 09:32:05 +00:00
|
|
|
const { formatMessage, labels, messages, FormattedMessage } = useMessages();
|
2023-10-08 01:55:14 +00:00
|
|
|
const { del, useMutation } = useApi();
|
2024-01-29 09:32:05 +00:00
|
|
|
const { mutate, isPending, error } = useMutation({
|
|
|
|
|
mutationFn: reportId => del(`/reports/${reportId}`),
|
|
|
|
|
});
|
2023-10-08 01:55:14 +00:00
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
const handleConfirm = (close: () => void) => {
|
|
|
|
|
mutate(reportId as any, {
|
2023-10-08 01:55:14 +00:00
|
|
|
onSuccess: () => {
|
2024-01-29 09:32:05 +00:00
|
|
|
touch('reports');
|
2023-10-08 01:55:14 +00:00
|
|
|
onDelete?.();
|
|
|
|
|
close();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ModalTrigger>
|
2024-01-29 09:32:05 +00:00
|
|
|
<Button variant="quiet">
|
2023-10-08 01:55:14 +00:00
|
|
|
<Icon>
|
|
|
|
|
<Icons.Trash />
|
|
|
|
|
</Icon>
|
|
|
|
|
<Text>{formatMessage(labels.delete)}</Text>
|
|
|
|
|
</Button>
|
2024-01-29 09:32:05 +00:00
|
|
|
<Modal title={formatMessage(labels.deleteReport)}>
|
|
|
|
|
{(close: () => void) => (
|
|
|
|
|
<ConfirmationForm
|
|
|
|
|
message={
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
{...messages.confirmDelete}
|
|
|
|
|
values={{ target: <b>{reportName}</b> }}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
isLoading={isPending}
|
|
|
|
|
error={error}
|
2023-10-08 01:55:14 +00:00
|
|
|
onConfirm={handleConfirm.bind(null, close)}
|
|
|
|
|
onClose={close}
|
2024-02-07 07:04:22 +00:00
|
|
|
buttonLabel={formatMessage(labels.delete)}
|
2023-10-08 01:55:14 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Modal>
|
|
|
|
|
</ModalTrigger>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ReportDeleteButton;
|