All files / src/viewmodels/event useMyEventsViewModel.ts

90.9% Statements 50/55
71.42% Branches 5/7
100% Functions 1/1
90.9% Lines 50/55

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 651x 1x   1x 1x       1x 6x 6x 6x 6x 6x 6x 6x 6x   6x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x           3x 3x 3x 6x   6x 3x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x  
import { useState, useEffect, useCallback } from 'react';
import { profileService } from '@/services/profileService';
import type { EventSummary } from '@/models/profile';
import { ApiError } from '@/services/api';
import i18n from '@/i18n';
 
export type MyEventsTab = 'organized' | 'upcoming' | 'active' | 'past' | 'canceled';
 
export function useMyEventsViewModel(token: string | null) {
  const [organized, setOrganized] = useState<EventSummary[]>([]);
  const [upcoming, setUpcoming] = useState<EventSummary[]>([]);
  const [active, setActive] = useState<EventSummary[]>([]);
  const [past, setPast] = useState<EventSummary[]>([]);
  const [canceled, setCanceled] = useState<EventSummary[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [activeTab, setActiveTab] = useState<MyEventsTab>('active');
 
  const fetchEvents = useCallback(async () => {
    if (!token) return;
    setIsLoading(true);
    setError(null);
 
    try {
      const [hosted, upAndActive, completed, canceled_] = await Promise.all([
        profileService.getHostedEvents(token),
        profileService.getUpcomingEvents(token),
        profileService.getCompletedEvents(token),
        profileService.getCanceledEvents(token),
      ]);
 
      setOrganized(hosted);
      setUpcoming(upAndActive.filter((e) => e.status === 'ACTIVE'));
      setActive(upAndActive.filter((e) => e.status === 'IN_PROGRESS'));
      setPast(completed);
      setCanceled(canceled_);
    } catch (err) {
      if (err instanceof ApiError) {
        setError(err.message);
      } else {
        setError(i18n.t('errors.my_events_load_failed'));
      }
    } finally {
      setIsLoading(false);
    }
  }, [token]);
 
  useEffect(() => {
    fetchEvents();
  }, [fetchEvents]);
 
  return {
    organized,
    upcoming,
    active,
    past,
    canceled,
    isLoading,
    error,
    activeTab,
    setActiveTab,
    retry: fetchEvents,
  };
}