import React, { useEffect, useState, useRef } from 'react'; import { __ } from '@wordpress/i18n'; import { useNavigate } from 'react-router-dom'; // components import { Button } from 'primereact/button'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Card } from 'primereact/card'; import { Tag } from 'primereact/tag'; import { Skeleton } from 'primereact/skeleton'; import { Toast } from 'primereact/toast'; // api import BandoService from '../../../service/bando-service'; import RendicontazioneService from '../service/rendicontazioneService'; const SCHEMA_STATUS_CONFIG = { null: { severity: 'info', label: __('Non creato', 'gepafin'), icon: 'pi pi-circle' }, DRAFT: { severity: 'warning', label: __('Bozza', 'gepafin'), icon: 'pi pi-pencil' }, PUBLISHED: { severity: 'success', label: __('Pubblicato', 'gepafin'), icon: 'pi pi-check-circle' } }; const RendicontazioneHome = () => { const navigate = useNavigate(); const toast = useRef(null); const [rows, setRows] = useState([]); // {bando, schema} const [loading, setLoading] = useState(true); const loadData = () => { setLoading(true); BandoService.getBandiPaginated({ page: 0, size: 100 }, (resp) => { const bandi = resp?.data?.body || []; // per ogni bando, tento di caricare lo schema di rendicontazione const baseRows = bandi.map(b => ({ bando: b, schema: null, schemaLoaded: false })); setRows(baseRows); setLoading(false); // Caricamento schemi in parallelo — update progressivo bandi.forEach((b, idx) => { RendicontazioneService.getSchemaByCallId(b.id, (schemaResp) => { setRows(prev => prev.map((r, i) => i === idx ? { ...r, schema: schemaResp?.data || null, schemaLoaded: true } : r)); }, (err) => { // 404 = schema non ancora creato, tutto ok setRows(prev => prev.map((r, i) => i === idx ? { ...r, schema: null, schemaLoaded: true } : r)); } ); }); }, (err) => { setLoading(false); if (toast.current) { toast.current.show({ severity: 'error', summary: __('Errore', 'gepafin'), detail: err?.message || __('Impossibile caricare i bandi', 'gepafin') }); } } ); }; useEffect(() => { loadData(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const editSchema = (bandoId) => { navigate(`/bandi/${bandoId}/rendicontazione-schema`); }; // --- column templates --- const bandoNameTpl = (row) => (
{row.bando.name || `Bando #${row.bando.id}`} {row.bando.descriptionShort && (
{row.bando.descriptionShort.slice(0, 80)}{row.bando.descriptionShort.length > 80 ? '…' : ''}
)}
); const bandoStatusTpl = (row) => ( ); const schemaStatusTpl = (row) => { if (!row.schemaLoaded) return ; const key = row.schema ? row.schema.status : null; const conf = SCHEMA_STATUS_CONFIG[key] || SCHEMA_STATUS_CONFIG[null]; return ; }; const actionsTpl = (row) => { if (!row.schemaLoaded) return ; const hasSchema = !!row.schema; return (