import React, { useEffect, useState, useCallback, useRef } from 'react'; import { __ } from '@wordpress/i18n'; import translationStrings from '../../../../translationStringsForComponents'; // api import ApplicationService from '../../../../service/application-service'; import AdminService from '../../../../service/admin-service'; // helpers import getQueryParamsForPaginatedEndpoint from '../../../../helpers/getQueryParamsForPaginatedEndpoint'; import getBandoLabel from '../../../../helpers/getBandoLabel'; import getBandoSeverity from '../../../../helpers/getBandoSeverity'; // components import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Button } from 'primereact/button'; import { Dialog } from 'primereact/dialog'; import { Tag } from 'primereact/tag'; import { Dropdown } from 'primereact/dropdown'; import ProperBandoLabel from '../../../../components/ProperBandoLabel'; import { Toast } from 'primereact/toast'; const allStatuses = [ 'SUBMIT', 'EVALUATION', 'SOCCORSO', 'APPOINTMENT', 'NDG', 'ADMISSIBLE', 'AWAITING_TECHNICAL_EVALUATION', 'TECHNICAL_EVALUATION' ]; const initialDeletedLazyState = { first: 0, rows: 10, page: 0 }; const initialPreDeleteLazyState = { first: 0, rows: 5, page: 0, sortField: null, sortOrder: null, filters: { id: { value: null, matchMode: 'equals' }, callTitle: { value: null, matchMode: 'contains' }, companyName: { value: null, matchMode: 'contains' }, status: { value: null, matchMode: 'equals' } } }; const ManageApplDeleteSection = ({ canViewDeleted, canDeleteConfirm, canDelete }) => { const toast = useRef(null); // --- Table 1: Deleted applications --- const [deletedLoading, setDeletedLoading] = useState(false); const [deletedItems, setDeletedItems] = useState(null); const [deletedTotal, setDeletedTotal] = useState(0); const [deletedLazyState, setDeletedLazyState] = useState(initialDeletedLazyState); const [deletedRefreshKey, setDeletedRefreshKey] = useState(0); const [finalDeleteDialogVisible, setFinalDeleteDialogVisible] = useState(false); const [finalDeleteAppId, setFinalDeleteAppId] = useState(null); const [finalDeleteLoading, setFinalDeleteLoading] = useState(false); // --- Table 2: Pre-delete applications --- const [preDeleteLoading, setPreDeleteLoading] = useState(false); const [preDeleteItems, setPreDeleteItems] = useState(null); const [preDeleteTotal, setPreDeleteTotal] = useState(0); const [preDeleteLazyState, setPreDeleteLazyState] = useState(initialPreDeleteLazyState); const [preDeleteRefreshKey, setPreDeleteRefreshKey] = useState(0); const [preDeleteDialogVisible, setPreDeleteDialogVisible] = useState(false); const [preDeleteAppId, setPreDeleteAppId] = useState(null); const [preDeleteActionLoading, setPreDeleteActionLoading] = useState(false); // ---- Table 1 handlers ---- const getDeletedCallback = (resp) => { if (resp.status === 'success') { const { body, totalRecords } = resp.data; setDeletedTotal(totalRecords); setDeletedItems(body); } setDeletedLoading(false); }; const errDeletedCallback = () => { setDeletedLoading(false); }; const openFinalDeleteDialog = (appId) => { setFinalDeleteAppId(appId); setFinalDeleteDialogVisible(true); }; const hideFinalDeleteDialog = () => { setFinalDeleteDialogVisible(false); setFinalDeleteAppId(null); }; const handleFinalDelete = useCallback(() => { setFinalDeleteLoading(true); AdminService.doFinalDelete( { application_id: finalDeleteAppId }, () => { setDeletedRefreshKey(k => k + 1); setFinalDeleteLoading(false); hideFinalDeleteDialog(); }, (resp) => { if (toast.current) { toast.current.show({ severity: 'error', summary: '', detail: resp.detail }); } setFinalDeleteLoading(false); hideFinalDeleteDialog(); } ); }, [finalDeleteAppId]); const deletedActionsBodyTemplate = (rowData) => (