import { ActionIcon, Alert, Button, Center, Group, Select, Stack, Switch, Text, } from '@mantine/core'; import { useMediaQuery } from '@mantine/hooks'; import { modals } from '@mantine/modals'; import { showNotification } from '@mantine/notifications'; import { IconChevronDown, IconChevronRight, IconHandStop, IconRefresh, IconTools, IconTrash, IconX, } from '@tabler/icons-react'; import { __, _n, sprintf } from '@wordpress/i18n'; import { DataTable } from 'mantine-datatable'; import { type MouseEvent, useEffect, useState } from 'react'; import useSWR from 'swr'; import { post } from '../../utils/post'; import classes from './Deletable.module.css'; import { ItemsDataTable } from './ItemsDataTable'; export function Deletable({ deletableType = 'trash' }) { const { data: originalData, isLoading, isValidating, mutate, } = useSWR<{ id: string; label: string; count: number }[]>(deletableType, post); // Whole sentences per view: a translated word spliced into an English frame would fix the // word order of every language to English. const emptyMessage = deletableType === 'duplicates' ? __('No duplicate records found.', 'scotty') : __('No trash records found.', 'scotty'); const onlyDeletableLabel = deletableType === 'duplicates' ? __('Show only items with duplicates', 'scotty') : __('Show only items with trash', 'scotty'); const [data, setData] = useState([]); const [selectedRecords, setSelectedRecords] = useState([]); const isTouch = useMediaQuery('(pointer: coarse)'); const [bulkAction, setBulkAction] = useState(''); const [showDeletable, setShowDeletable] = useState(true); const [expandedCompanyIds, setExpandedCompanyIds] = useState([]); useEffect(() => { if (originalData && originalData.length > 0) { const newData = originalData.filter(({ count }) => (showDeletable ? +count > 0 : true)); setData(newData); } }, [originalData, showDeletable]); /** * Delete every record of one item group, after a confirmation modal. * * @param event Click on the row's delete button. * @param record The item group: `id` is the AJAX action suffix, `label` is shown to the user. * @param record.id * @param record.label */ const handleDelete = async ( event: MouseEvent, record: { id: string; label: string } ) => { event.preventDefault(); event.stopPropagation(); modals.openConfirmModal({ title: __('Please confirm your action', 'scotty'), size: 'sm', radius: 'md', withCloseButton: false, centered: true, children: ( } > {sprintf( // translators: %s: name of the item group, e.g. "Auto Draft". __('All %s will be permanently deleted.', 'scotty'), record.label )}{' '} {__('This action cannot be undone.', 'scotty')} {__('Are you sure you want to continue?', 'scotty')} ), labels: { confirm: __('Confirm', 'scotty'), cancel: __('Cancel', 'scotty'), }, onConfirm: () => { post(`delete_${record.id}`) .then(async () => { showNotification({ title: __('Cleanup', 'scotty'), icon: , color: 'green', message: sprintf( // translators: %s: name of the item group, e.g. "Auto Draft". __('All "%s" items have been deleted successfully.', 'scotty'), record.label ), withBorder: true, }); setExpandedCompanyIds([]); await mutate(undefined, { revalidate: true }); }) .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: name of the item group, 2: error message returned by the server. __('An error occurred while deleting the "%1$s" records. Error: %2$s', 'scotty'), record.label, error.statusText ), withBorder: true, autoClose: 10000, }); }); }, }); }; const handleBulkAction = () => { if (bulkAction !== 'delete' || selectedRecords.length === 0) { return; } const records = [...selectedRecords] as { id: string; label: string }[]; modals.openConfirmModal({ title: __('Please confirm your action', 'scotty'), size: 'sm', radius: 'md', withCloseButton: false, centered: true, children: ( } > {sprintf( // translators: %d: number of selected item groups. __('All selected items (%d) will be permanently deleted.', 'scotty'), records.length )}{' '} {__('This action cannot be undone.', 'scotty')} {__('Are you sure you want to continue?', 'scotty')} ), labels: { confirm: __('Confirm', 'scotty'), cancel: __('Cancel', 'scotty'), }, onConfirm: async () => { const failed: { id: string; label: string; status?: number; statusText?: string }[] = []; for (const record of records) { try { await post(`delete_${record.id}`); } catch (error: any) { failed.push({ ...record, status: error?.status, statusText: error?.statusText }); } } setExpandedCompanyIds([]); setSelectedRecords([]); setBulkAction(''); await mutate(undefined, { revalidate: true }); if (failed.length === 0) { showNotification({ title: __('Cleanup', 'scotty'), icon: , color: 'green', message: sprintf( // translators: %d: number of item groups deleted. _n( '%d item group has been deleted successfully.', '%d item groups have been deleted successfully.', records.length, 'scotty' ), records.length ), withBorder: true, }); } else { showNotification({ title: __('Error', 'scotty'), icon: , color: 'red', message: sprintf( // translators: %s: comma-separated names of the item groups that could not be deleted. __('Failed to delete: %s', 'scotty'), failed.map((f) => f.label).join(', ') ), withBorder: true, autoClose: 10000, }); } }, }); }; if (!isLoading && !isValidating && data.length === 0) { return ( 👍 {emptyMessage}
); } return (