All files / src/views/tickets TicketDetailPage.tsx

91.13% Statements 144/158
73.91% Branches 17/23
100% Functions 3/3
91.13% Lines 144/158

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 1821x 1x 1x 1x 1x 1x 1x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x   2x   5x 5x 5x 5x   5x                     5x 1x 1x   5x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   3x 3x 3x 3x 5x 1x 2x   5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x               5x 5x 5x 3x 3x 3x 3x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x   5x   5x 2x 2x   2x 2x 2x 2x 2x 2x 2x 1x 1x 1x   2x 2x   5x 5x   5x  
import { Link, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useTicketDetailViewModel } from '@/viewmodels/tickets/useTicketDetailViewModel';
import { getTicketStatusPresentation } from '@/utils/ticketStatus';
import NotFoundView from '../fallback/NotFoundView';
import i18n from '@/i18n';
import '@/styles/tickets.css';
 
function formatDateTime(iso: string | undefined | null): string {
  if (!iso) return '—';
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return iso;
  return d.toLocaleString(i18n.resolvedLanguage, {
    weekday: 'long',
    month: 'long',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false,
  });
}
 
function MobileQRIcon() {
  return (
    <svg
      width={28}
      height={28}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={2}
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden
    >
      <rect x="3" y="3" width="7" height="7" rx="1" />
      <rect x="14" y="3" width="7" height="7" rx="1" />
      <rect x="3" y="14" width="7" height="7" rx="1" />
      <path d="M14 14h3M14 17h3M14 21h7M17 14v7M21 14v3" />
    </svg>
  );
}
 
export default function TicketDetailPage() {
  const { t } = useTranslation();
  const { ticketId } = useParams<{ ticketId: string }>();
  const vm = useTicketDetailViewModel(ticketId);
 
  if (vm.status === 'loading') {
    return (
      <div className="tk-page">
        <div className="tk-loading">
          <span className="spinner" />
          <p>{t('tickets.loading_ticket')}</p>
        </div>
      </div>
    );
  }
 
  if (vm.status === 'not-found') {
    return <NotFoundView />;
  }
 
  if (vm.status === 'error' || !vm.ticket) {
    return (
      <div className="tk-page">
        <div className="tk-error" role="alert">
          <span>{vm.errorMessage ?? t('tickets.load_failed')}</span>
          <button type="button" className="tk-retry-btn" onClick={vm.refresh}>
            {t('common.retry')}
          </button>
        </div>
      </div>
    );
  }
 
  const { ticket, participation, event, location, qr_access } = vm.ticket;
  const presentation = getTicketStatusPresentation(ticket.status);
  const canShowMobileHint = ticket.status === 'ACTIVE';
  const eligibilityNotice =
    ticket.status === 'ACTIVE' && qr_access.eligible_now === false
      ? qr_access.reason ?? t('tickets.qr_not_eligible')
      : null;
 
  return (
    <div className="tk-page">
      <Link to="/tickets" className="tk-back-link">
        &larr; {t('tickets.back_to_tickets')}
      </Link>
 
      <article className="tk-detail" data-testid="ticket-detail">
        <header className="tk-detail-header">
          <span className={`tk-status-badge tk-status-${presentation.tone}`}>
            {presentation.label}
          </span>
          <h1 className="tk-detail-title">{event.title}</h1>
          <p className="tk-detail-status-desc">{presentation.description}</p>
        </header>
 
        <section className="tk-detail-section">
          <h2 className="tk-detail-section-title">{t('tickets.event_section')}</h2>
          <div className="tk-detail-row">
            <span className="tk-detail-label">{t('tickets.when')}</span>
            <span className="tk-detail-value">
              {formatDateTime(event.start_time)}
              {event.end_time && (
                <>
                  <br />
                  <span className="tk-detail-secondary">
                    {t('tickets.until', { date: formatDateTime(event.end_time) })}
                  </span>
                </>
              )}
            </span>
          </div>
          {event.address && (
            <div className="tk-detail-row">
              <span className="tk-detail-label">{t('tickets.where')}</span>
              <span className="tk-detail-value">{event.address}</span>
            </div>
          )}
          <div className="tk-detail-row">
            <span className="tk-detail-label">{t('tickets.coordinates')}</span>
            <span className="tk-detail-value tk-detail-mono">
              {location.anchor_lat.toFixed(5)}, {location.anchor_lon.toFixed(5)}
            </span>
          </div>
          <Link to={`/events/${event.id}`} className="tk-detail-link">
            {t('tickets.view_event_details')}
          </Link>
        </section>
 
        <section className="tk-detail-section">
          <h2 className="tk-detail-section-title">{t('tickets.ticket_section')}</h2>
          <div className="tk-detail-row">
            <span className="tk-detail-label">{t('tickets.ticket_id')}</span>
            <span className="tk-detail-value tk-detail-mono">{ticket.id}</span>
          </div>
          <div className="tk-detail-row">
            <span className="tk-detail-label">{t('tickets.participation')}</span>
            <span className="tk-detail-value">
              {t(`tickets.participation_status.${participation.status}`, {
                defaultValue: participation.status,
              })}
            </span>
          </div>
          <div className="tk-detail-row">
            <span className="tk-detail-label">{t('tickets.expires_label')}</span>
            <span className="tk-detail-value">{formatDateTime(ticket.expires_at)}</span>
          </div>
          {ticket.used_at && (
            <div className="tk-detail-row">
              <span className="tk-detail-label">{t('tickets.used')}</span>
              <span className="tk-detail-value">{formatDateTime(ticket.used_at)}</span>
            </div>
          )}
        </section>
 
        {canShowMobileHint && (
          <section
            className={`tk-mobile-hint ${eligibilityNotice ? 'tk-mobile-hint-warn' : ''}`}
          >
            <span className="tk-mobile-hint-icon">
              <MobileQRIcon />
            </span>
            <div className="tk-mobile-hint-body">
              <p className="tk-mobile-hint-title">{t('tickets.show_qr_title')}</p>
              <p className="tk-mobile-hint-text">{t('tickets.show_qr_body')}</p>
              {eligibilityNotice && (
                <p className="tk-mobile-hint-notice">
                  {t('tickets.eligibility_note', { reason: eligibilityNotice })}
                </p>
              )}
            </div>
          </section>
        )}
      </article>
    </div>
  );
}