import { ActionIcon, Alert, Group, Stack, Text } from '@mantine/core'; import { modals } from '@mantine/modals'; import { showNotification } from '@mantine/notifications'; import { IconHandStop, IconTools, IconTrash, IconX } from '@tabler/icons-react'; import { __, sprintf } from '@wordpress/i18n'; import { DataTable } from 'mantine-datatable'; import { type MouseEvent } from 'react'; import useSWR from 'swr'; import { post } from '../../utils/post'; interface Item { id: string; // Add other properties of the item as needed } interface ItemsDataTableProps { item: Item; onMutate: () => void; } export function ItemsDataTable({ item, onMutate }: ItemsDataTableProps) { const { data, isLoading, isValidating, mutate } = useSWR(item.id, post); const handleDelete = async (event: MouseEvent, record: any) => { event.preventDefault(); event.stopPropagation(); modals.openConfirmModal({ title: __('Please confirm your action', 'scotty'), size: 'sm', radius: 'md', withCloseButton: false, centered: true, children: ( } > {__( 'The item selected will be permanently deleted. This action cannot be undone.', 'scotty' )} {__('Are you sure you want to continue?', 'scotty')} ), labels: { confirm: __('Confirm', 'scotty'), cancel: __('Cancel', 'scotty') }, onConfirm: async () => { post(`delete_${item.id}_with_id`, { id: record.id }) .then(() => { showNotification({ title: __('Cleanup', 'scotty'), icon: , color: 'green', message: sprintf( // translators: %s: the deleted record, as shown in the first column. __('The record "%s" has been deleted successfully.', 'scotty'), record[data.columns[0].accessor] ), withBorder: true, }); mutate(); onMutate(); }) .catch((error) => { showNotification({ title: sprintf( // translators: %s: HTTP status code of the failed request. __('Error %s', 'scotty'), error.status ), icon: , color: 'red', message: sprintf( // translators: 1: the record, as shown in the first column, 2: error message returned by the server. __('An error occurred while deleting the record "%1$s". Error: %2$s', 'scotty'), record[data.columns[0].accessor], error.statusText ), withBorder: true, autoClose: 10000, }); }); }, }); }; // Derived on every render: the delete button closes over the current handleDelete, which a // useEffect keyed on `data` alone captured once and kept stale. const columns = data ? [ ...data.columns, { accessor: 'actions', title: , width: 80, textAlign: 'center', render: (record: any) => ( handleDelete(event, record)} > ), }, ] : []; return ( ); }