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 availableStatuses = [ 'EVALUATION', 'SOCCORSO', 'APPOINTMENT', 'NDG', 'ADMISSIBLE', 'AWAITING_TECHNICAL_EVALUATION', 'TECHNICAL_EVALUATION' ]; const ManageApplStatusSection = () => { const [localAsyncRequest, setLocalAsyncRequest] = useState(false); const [items, setItems] = useState(null); const [totalRecordsNum, setTotalRecordsNum] = useState(0); const [isStatusDialogVisible, setIsStatusDialogVisible] = useState(false); const [selectedAppId, setSelectedAppId] = useState(null); const [selectedStatus, setSelectedStatus] = useState(null); const [lazyState, setLazyState] = useState({ 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 toast = useRef(null); const getPaginationQuery = useCallback(() => getQueryParamsForPaginatedEndpoint(lazyState, allStatuses, 'id'), [lazyState]); const onPage = (event) => { setLazyState(event); }; const onSort = (event) => { event['first'] = 0; event['page'] = 0; setLazyState(event); }; const onFilter = useCallback((event) => { event['first'] = 0; event['page'] = 0; setLazyState(event); }, [lazyState]); const getCallback = (resp) => { if (resp.status === 'SUCCESS') { const { body, totalRecords } = resp.data; setTotalRecordsNum(totalRecords); setItems(body); } setLocalAsyncRequest(false); }; const errGetCallback = () => { setLocalAsyncRequest(false); }; const openStatusDialog = (appId) => { setSelectedAppId(appId); setSelectedStatus(null); setIsStatusDialogVisible(true); }; const hideStatusDialog = () => { setIsStatusDialogVisible(false); setSelectedAppId(null); setSelectedStatus(null); }; const handleChangeStatus = useCallback(() => { setLocalAsyncRequest(true); const body = { application_id: selectedAppId, status: selectedStatus }; AdminService.setApplStatus(body, callback, errCallback); }, [selectedStatus, selectedAppId]); const callback = (resp) => { setItems(prev => prev.map(o => { return o.id === resp.application_id ? { ...o, status: resp.status_set } : o; })); setLocalAsyncRequest(false); hideStatusDialog(); } const errCallback = () => { if (toast.current) { toast.current.show({ severity: 'error', summary: '', detail: resp.detail }); } setLocalAsyncRequest(false); hideStatusDialog(); } const actionsBodyTemplate = (rowData) => { return (