All files / src/components/partner PartnerAttendanceDashboard.tsx

77.77% Statements 42/54
50% Branches 16/32
73.68% Functions 14/19
81.25% Lines 39/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317                                                                                                    6x 6x     6x         8x               6x 16x 16x   16x   9x           16x 9x                             7x 1x                     6x   16x 16x 16x   16x                                                                             1x                                                                   6x 4x 4x   4x 4x 8x                 4x                                                                                                                       6x 4x 4x 4x 4x 8x 8x 8x 8x   4x 4x 8x               4x                                                                      
/**
 * PartnerAttendanceDashboard
 * Story 8.1: AC1, AC2, AC3, AC7, AC8
 *
 * Two charts replacing the raw attendance table:
 *
 * Chart 1 — Attendance per event (ComposedChart):
 *   Bars = this company's attendees per event  |  Line = total event attendees
 *   Shows: "are we growing in step with BATbern?"
 *
 * Chart 2 — Year-over-year trend (ComposedChart):
 *   Bars = company headcount per year  |  Line (right axis) = attendance share %
 *   Shows: "are we becoming a larger or smaller slice over time?"
 *
 * Both charts computed from the existing /analytics/dashboard API payload.
 * No new backend endpoints required.
 */
import React, { useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import {
  Bar,
  CartesianGrid,
  ComposedChart,
  Line,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from 'recharts';
import {
  Alert,
  Box,
  Button,
  ButtonGroup,
  Card,
  CardContent,
  Container,
  Paper,
  Skeleton,
  Typography,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import { getAttendanceDashboard } from '@/services/api/partnerAnalyticsApi';
import { AttendanceExportButton } from './AttendanceExportButton';
import { CHART_COLORS } from '@/components/organizer/Analytics/CHART_COLORS';
 
interface Props {
  companyName: string;
}
 
const CURRENT_YEAR = new Date().getFullYear();
const FROM_YEAR_5 = CURRENT_YEAR - 5;
 
// Reusable chart card wrapper
const ChartSection: React.FC<{ title: string; children: React.ReactNode; testId?: string }> = ({
  title,
  children,
  testId,
}) => (
  <Paper variant="outlined" sx={{ p: 3, mb: 3 }} data-testid={testId}>
    <Typography variant="subtitle1" fontWeight={600} mb={2}>
      {title}
    </Typography>
    {children}
  </Paper>
);
 
export const PartnerAttendanceDashboard: React.FC<Props> = ({ companyName }) => {
  const { t } = useTranslation('partners');
  const [fromYear, setFromYear] = useState<number>(FROM_YEAR_5);
 
  const { data, isLoading, isError } = useQuery({
    queryKey: ['partnerAttendanceDashboard', companyName, fromYear],
    queryFn: () => getAttendanceDashboard(companyName, fromYear),
    staleTime: 15 * 60 * 1000,
  });
 
  // ─── Loading state ─────────────────────────────────────────────────────────
 
  if (isLoading) {
    return (
      <Container maxWidth="xl" sx={{ py: 4 }} data-testid="analytics-loading">
        <Skeleton variant="text" width={300} height={40} sx={{ mb: 2 }} />
        <Box sx={{ display: 'flex', gap: 2, mb: 3 }}>
          <Skeleton variant="rectangular" width={200} height={100} />
          <Skeleton variant="rectangular" width={200} height={100} />
        </Box>
        <Skeleton variant="rectangular" height={300} sx={{ mb: 3 }} />
        <Skeleton variant="rectangular" height={250} />
      </Container>
    );
  }
 
  // ─── Error state ───────────────────────────────────────────────────────────
 
  if (isError) {
    return (
      <Container maxWidth="xl" sx={{ py: 4 }}>
        <Alert severity="error" data-testid="analytics-error">
          {t('portal.analytics.error')}
        </Alert>
      </Container>
    );
  }
 
  // ─── Data preparation ──────────────────────────────────────────────────────
 
  const summaries = data?.attendanceSummary ?? [];
 
  const totalCompanyAttendees = summaries.reduce((sum, s) => sum + s.companyAttendees, 0);
  const totalAttendees = summaries.reduce((sum, s) => sum + s.totalAttendees, 0);
  const avgAttendanceRate = totalAttendees > 0 ? totalCompanyAttendees / totalAttendees : 0;
 
  return (
    <Container maxWidth="xl" sx={{ py: 4 }} data-testid="attendance-dashboard">
      {/* KPI cards */}
      <Box sx={{ display: 'flex', gap: 2, mb: 3 }}>
        <Card sx={{ minWidth: 200 }} data-testid="kpi-attendance-rate">
          <CardContent>
            <Typography variant="subtitle2" color="text.secondary">
              {t('portal.analytics.kpi.attendanceRate')}
            </Typography>
            <Typography variant="h4">{(avgAttendanceRate * 100).toFixed(1)}%</Typography>
          </CardContent>
        </Card>
 
        <Card sx={{ minWidth: 200 }} data-testid="kpi-cost-per-attendee">
          <CardContent>
            <Typography variant="subtitle2" color="text.secondary">
              {t('portal.analytics.kpi.costPerAttendee')}
            </Typography>
            <Typography variant="h4">
              {data?.costPerAttendee != null
                ? `${t('portal.analytics.kpi.currency')} ${data.costPerAttendee.toFixed(2)}`
                : 'N/A'}
            </Typography>
          </CardContent>
        </Card>
      </Box>
 
      {/* Range toggle + export */}
      <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 3 }}>
        <ButtonGroup variant="outlined" size="small" data-testid="range-toggle">
          <Button
            variant={fromYear === FROM_YEAR_5 ? 'contained' : 'outlined'}
            onClick={() => setFromYear(FROM_YEAR_5)}
            data-testid="range-5years"
          >
            {t('portal.analytics.range.last5years')}
          </Button>
          <Button
            variant={fromYear === CURRENT_YEAR - 20 ? 'contained' : 'outlined'}
            onClick={() => setFromYear(CURRENT_YEAR - 20)}
            data-testid="range-allhistory"
          >
            {t('portal.analytics.range.allHistory')}
          </Button>
        </ButtonGroup>
 
        <AttendanceExportButton companyName={companyName} fromYear={fromYear} />
      </Box>
 
      {summaries.length === 0 ? (
        <Typography color="text.secondary" data-testid="no-data-message">
          {t('portal.analytics.noData')}
        </Typography>
      ) : (
        <>
          <Chart1PerEvent summaries={summaries} />
          <Chart2YearOverYear summaries={summaries} />
        </>
      )}
    </Container>
  );
};
 
// ─── Chart 1: Attendance per event ────────────────────────────────────────────
 
interface SummaryRow {
  eventCode: string;
  eventTitle: string;
  eventDate: string;
  companyAttendees: number;
  totalAttendees: number;
}
 
const Chart1PerEvent: React.FC<{ summaries: SummaryRow[] }> = ({ summaries }) => {
  const { t } = useTranslation('partners');
  const rows = useMemo(
    () =>
      [...summaries]
        .sort((a, b) => new Date(a.eventDate).getTime() - new Date(b.eventDate).getTime())
        .map((s) => ({
          eventCode: s.eventCode,
          title: s.eventTitle || s.eventCode,
          company: s.companyAttendees,
          total: s.totalAttendees,
        })),
    [summaries]
  );
 
  return (
    <ChartSection
      title={t('portal.analytics.charts.companyVsTotal')}
      testId="chart-attendance-per-event"
    >
      <ResponsiveContainer width="100%" height={320}>
        <ComposedChart data={rows} margin={{ top: 4, right: 40, left: 0, bottom: 56 }}>
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis
            dataKey="title"
            angle={-45}
            textAnchor="end"
            interval={0}
            tick={{ fontSize: 10 }}
          />
          <YAxis allowDecimals={false} />
          <Tooltip
            content={({ active, payload }) => {
              if (!active || !payload?.length) return null;
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
              const row = payload[0].payload as any;
              return (
                <div
                  style={{
                    background: '#fff',
                    border: '1px solid #ccc',
                    borderRadius: 4,
                    padding: '8px 12px',
                    fontSize: 12,
                  }}
                >
                  <div style={{ fontWeight: 600 }}>{row.eventCode}</div>
                  <div style={{ marginBottom: 4, color: '#555' }}>{row.title}</div>
                  {payload.map((p) => (
                    <div key={p.dataKey as string}>
                      {p.dataKey === 'company' ? 'Your attendees' : 'Total attendees'}: {p.value}
                    </div>
                  ))}
                </div>
              );
            }}
          />
          <Bar dataKey="company" name="company" fill={CHART_COLORS.partner} />
          <Line
            type="monotone"
            dataKey="total"
            name="total"
            stroke={CHART_COLORS.primary}
            dot={false}
            strokeWidth={2}
            strokeDasharray="5 3"
          />
        </ComposedChart>
      </ResponsiveContainer>
    </ChartSection>
  );
};
 
// ─── Chart 2: Year-over-year trend ────────────────────────────────────────────
 
const Chart2YearOverYear: React.FC<{ summaries: SummaryRow[] }> = ({ summaries }) => {
  const { t } = useTranslation('partners');
  const rows = useMemo(() => {
    const byYear: Record<string, { company: number; total: number }> = {};
    for (const s of summaries) {
      const year = new Date(s.eventDate).getFullYear().toString();
      Eif (!byYear[year]) byYear[year] = { company: 0, total: 0 };
      byYear[year].company += s.companyAttendees;
      byYear[year].total += s.totalAttendees;
    }
    return Object.entries(byYear)
      .sort(([a], [b]) => Number(a) - Number(b))
      .map(([year, { company, total }]) => ({
        year,
        company,
        total,
        sharePct: total > 0 ? Math.round((company / total) * 100) : 0,
      }));
  }, [summaries]);
 
  return (
    <ChartSection title={t('portal.analytics.charts.yoyHeadcount')} testId="chart-yoy-headcount">
      <ResponsiveContainer width="100%" height={280}>
        <ComposedChart data={rows} margin={{ top: 4, right: 48, left: 0, bottom: 4 }}>
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="year" />
          <YAxis yAxisId="count" allowDecimals={false} />
          <YAxis
            yAxisId="pct"
            orientation="right"
            tickFormatter={(v) => `${v}%`}
            domain={[0, 100]}
          />
          <Tooltip
            formatter={(value, name) => {
              if (name === 'sharePct') return [`${value}%`, 'Share of total'];
              if (name === 'company') return [value, 'Your attendees'];
              return [value, name];
            }}
          />
          <Bar yAxisId="count" dataKey="company" name="company" fill={CHART_COLORS.partner} />
          <Line
            yAxisId="pct"
            type="monotone"
            dataKey="sharePct"
            name="sharePct"
            stroke={CHART_COLORS.primary}
            dot={{ r: 4 }}
            strokeWidth={2}
          />
        </ComposedChart>
      </ResponsiveContainer>
    </ChartSection>
  );
};