umami/src/app/(main)/reports/ReportDeleteButton.tsx

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-10-08 01:55:14 +00:00
import { Button, Icon, Icons, Modal, ModalTrigger, Text } from 'react-basics';
2024-02-08 07:48:51 +00:00
import { useApi, useMessages, useModified } from 'components/hooks';
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;
}) {
const { formatMessage, labels, messages, FormattedMessage } = useMessages();
2023-10-08 01:55:14 +00:00
const { del, useMutation } = useApi();
const { mutate, isPending, error } = useMutation({
mutationFn: reportId => del(`/reports/${reportId}`),
});
2024-02-08 07:48:51 +00:00
const { touch } = useModified();
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: () => {
touch('reports');
2023-10-08 01:55:14 +00:00
onDelete?.();
close();
},
});
};
return (
<ModalTrigger>
2024-02-08 22:05:35 +00:00
<Button>
2023-10-08 01:55:14 +00:00
<Icon>
<Icons.Trash />
</Icon>
<Text>{formatMessage(labels.delete)}</Text>
</Button>
<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}
buttonLabel={formatMessage(labels.delete)}
2023-10-08 01:55:14 +00:00
/>
)}
</Modal>
</ModalTrigger>
);
}
export default ReportDeleteButton;