All files / src/views/favorites FavoritesPage.tsx

9.72% Statements 14/144
100% Branches 0/0
0% Functions 0/5
9.72% Lines 14/144

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 1741x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                              
import { useState } from 'react';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import i18n from '@/i18n';
import { useAuth } from '@/contexts/AuthContext';
import { useFavoritesViewModel } from '@/viewmodels/favorites/useFavoritesViewModel';
import FavoriteLocationsTab from './FavoriteLocationsTab';
import type { FavoriteEventItem } from '@/models/event';
import { EventCoverImage } from '@/components/EventCoverImage';
import { RatingWithCount } from '@/components/RatingWithCount';
import { getEventCardBadgePresentation } from '@/utils/eventStatus';
import { getEventCategoryPresentation } from '@/utils/eventCategoryPresentation';
import '@/styles/my-events.css';
import '@/styles/discover.css';
import '@/styles/favorites.css';
 
type FavoritesTab = 'events' | 'locations';
 
function formatDate(iso: string): string {
  const d = new Date(iso);
  return d.toLocaleDateString(i18n.resolvedLanguage, {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
  });
}
 
function formatTime(iso: string): string {
  const d = new Date(iso);
  return d.toLocaleTimeString(i18n.resolvedLanguage, {
    hour: '2-digit',
    minute: '2-digit',
    hour12: false,
  });
}
 
function FavoriteCard({ item }: { item: FavoriteEventItem }) {
  const { t } = useTranslation();
  const badge = getEventCardBadgePresentation(item.status);
  const category = getEventCategoryPresentation(item.category_name ?? item.category ?? 'Event', false).label;
 
  return (
    <Link to={`/events/${item.id}`} className="dc-card">
      <div className="dc-card-image-wrapper">
        <EventCoverImage
          src={item.image_url}
          alt={item.title}
          imgClassName="dc-card-image"
          variant="card"
        />
        {badge && (
          <span
            className={`dc-lifecycle-badge ${
              badge.variant === 'upcoming'
                ? 'dc-lifecycle-upcoming'
                : badge.variant === 'in_progress'
                  ? 'dc-lifecycle-in-progress'
                  : badge.variant === 'canceled'
                    ? 'dc-lifecycle-canceled'
                    : 'dc-lifecycle-completed'
            }`}
          >
            {badge.label}
          </span>
        )}
        {item.privacy_level && item.privacy_level !== 'PRIVATE' && (
          <span className={`dc-privacy-badge dc-privacy-${item.privacy_level.toLowerCase()}`}>
            {t(`events.privacy.${item.privacy_level}`)}
          </span>
        )}
      </div>
      <div className="dc-card-body">
        <div className="dc-card-meta">
          <span className="dc-card-category">{category}</span>
          <span className="dc-card-date">
            {formatDate(item.start_time)} &middot; {formatTime(item.start_time)}
          </span>
        </div>
        <h3 className="dc-card-title">{item.title}</h3>
        {item.location_address && (
          <p className="dc-card-location">{item.location_address}</p>
        )}
        <div className="dc-card-footer">
          <span className="dc-card-participants">
            {t('favorites.participants', { count: item.approved_participant_count ?? 0 })}
          </span>
          {item.host_score && (
            <RatingWithCount
              score={item.host_score.final_score}
              count={item.host_score.hosted_event_rating_count}
              className="dc-card-score"
            />
          )}
        </div>
      </div>
    </Link>
  );
}
 
function FavoriteEventsContent({ token }: { token: string | null }) {
  const { t } = useTranslation();
  const vm = useFavoritesViewModel(token);
 
  if (vm.isLoading) {
    return (
      <div className="me-loading">
        <span className="spinner" />
        <p>{t('favorites.loading_events')}</p>
      </div>
    );
  }
 
  if (vm.error) {
    return (
      <div className="me-error">
        <p>{vm.error}</p>
        <button type="button" className="me-retry-btn" onClick={vm.retry}>
          {t('common.retry')}
        </button>
      </div>
    );
  }
 
  if (vm.items.length === 0) {
    return (
      <div className="me-empty">
        <p>{t('favorites.empty_events')}</p>
      </div>
    );
  }
 
  return (
    <div className="me-grid">
      {vm.items.map((item) => (
        <FavoriteCard key={item.id} item={item} />
      ))}
    </div>
  );
}
 
export default function FavoritesPage() {
  const { t } = useTranslation();
  const { token } = useAuth();
  const [activeTab, setActiveTab] = useState<FavoritesTab>('events');
  const tabs: { key: FavoritesTab; label: string }[] = [
    { key: 'events', label: t('favorites.events_tab') },
    { key: 'locations', label: t('favorites.locations_tab') },
  ];
 
  return (
    <div className="me-page">
      <h1 className="me-title">{t('favorites.title')}</h1>
      <p className="me-subtitle">{t('favorites.subtitle')}</p>
 
      {/* Tabs */}
      <div className="me-tabs">
        {tabs.map((tab) => (
          <button
            key={tab.key}
            type="button"
            className={`me-tab ${activeTab === tab.key ? 'active' : ''}`}
            onClick={() => setActiveTab(tab.key)}
          >
            {tab.label}
          </button>
        ))}
      </div>
 
      {activeTab === 'events' && <FavoriteEventsContent token={token} />}
      {activeTab === 'locations' && <FavoriteLocationsTab />}
    </div>
  );
}