All files / src/viewmodels/favorites useFavoritesViewModel.ts

96.66% Statements 29/30
71.42% Branches 5/7
100% Functions 1/1
96.66% Lines 29/30

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 371x 1x   1x 1x   1x 6x 6x 6x   6x 3x 3x 3x   3x 3x 2x 3x 1x   1x 1x 1x 3x 3x 3x 6x   6x 3x 6x   6x 6x  
import { useState, useEffect, useCallback } from 'react';
import { getFavoriteEvents } from '@/services/eventService';
import type { FavoriteEventItem } from '@/models/event';
import { ApiError } from '@/services/api';
import i18n from '@/i18n';
 
export function useFavoritesViewModel(token: string | null) {
  const [items, setItems] = useState<FavoriteEventItem[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
 
  const fetchFavorites = useCallback(async () => {
    if (!token) return;
    setIsLoading(true);
    setError(null);
 
    try {
      const data = await getFavoriteEvents(token);
      setItems(data);
    } catch (err) {
      if (err instanceof ApiError) {
        setError(err.message);
      } else {
        setError(i18n.t('errors.unexpected'));
      }
    } finally {
      setIsLoading(false);
    }
  }, [token]);
 
  useEffect(() => {
    fetchFavorites();
  }, [fetchFavorites]);
 
  return { items, isLoading, error, retry: fetchFavorites };
}