import * as React from 'react'; import { __ } from '@wordpress/i18n'; import { BookingApi } from '../booking-ui/api'; import type { Config } from './types'; import { formatDate, formattingLocale } from '../booking-ui/dateTime'; const formatNumber = (v: number) => new Intl.NumberFormat(formattingLocale(), { maximumFractionDigits: 2 }).format(v); interface Report { summary: { booking_count: number; venue_seconds: number; cancelled: number; no_show: number; utilization: number | null; }; resources?: { items: { id: number; name: string; booking_count: number; venue_seconds: number; utilization: number | null; }[]; }; cancellations?: { items: { id: number; reference: string; date: string; resource_name: string; status: string; }[]; }; trend?: { date: string; venue_seconds: number; }[]; } export function BasicStatisticsApp({ config }: { config: Config; }) { const api = React.useMemo(() => new BookingApi(config), [config]); const [section, setSection] = React.useState('overview'), [from, setFrom] = React.useState(''), [to, setTo] = React.useState(''), [report, setReport] = React.useState(null), [error, setError] = React.useState(''), [revision, refresh] = React.useReducer(n => n + 1, 0); React.useEffect(() => { let live = true; api.request<{ defaults: { date_from: string; date_to: string; }; }>('admin/statistics/options').then(d => { if (live) { setFrom(d.defaults.date_from); setTo(d.defaults.date_to); } }).catch(e => { if (live) setError(e.message); }); return () => { live = false; }; }, [api]); React.useEffect(() => { if (!from || !to) return; let live = true; setError(''); api.request(`admin/statistics/${section}`, 'GET', undefined, { date_from: from, date_to: to, per_page: '100' }).then(d => { if (live) setReport(d); }).catch(e => { if (live) setError(e.message); }); return () => { live = false; }; }, [api, section, revision, from, to]); const tabs = [['overview', __('Overview', 'abece-booking')], ['resources', __('Resources', 'abece-booking')], ['cancellations', __('Cancellations and no-show', 'abece-booking')]]; return

{__('Statistics', 'abece-booking')}

{error &&

{error}

}
{ e.preventDefault(); refresh(); }}>
{report && <>
{[[__('Total bookings', 'abece-booking'), report.summary.booking_count], [__('Booked venue hours', 'abece-booking'), formatNumber(report.summary.venue_seconds / 3600)], [__('Cancellations', 'abece-booking'), report.summary.cancelled], [__('No-show', 'abece-booking'), report.summary.no_show], [__('Utilization (%)', 'abece-booking'), report.summary.utilization === null ? '—' : formatNumber(report.summary.utilization)]].map(([label, value]) =>
{label}
{value}
)}
{section === 'resources' ? {report.resources?.items.map(r => )}
{__('Resource', 'abece-booking')}{__('Total bookings', 'abece-booking')}{__('Booked venue hours', 'abece-booking')}{__('Utilization (%)', 'abece-booking')}
{r.name}{r.booking_count}{formatNumber(r.venue_seconds / 3600)}{r.utilization === null ? '—' : formatNumber(r.utilization)}
: section === 'cancellations' ? {report.cancellations?.items.map(r => )}
{formatDate(r.date)}{r.reference}{r.resource_name}{r.status === 'cancelled' ? __('Cancelled', 'abece-booking') : __('No-show', 'abece-booking')}
: {report.trend?.map(r => )}
{__('Date', 'abece-booking')}{__('Booked venue hours', 'abece-booking')}
{formatDate(r.date)}{formatNumber(r.venue_seconds / 3600)}
}
}
; }