All files / hook useTickets.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 3/3
100% Lines 20/20

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 402x   2x                   2x 5x 5x 5x   5x 3x 3x 3x 3x 3x 2x 2x   1x 1x 1x   3x       3x     5x    
import {useState, useEffect} from "react";
import {Ticket} from "@/type/achat/ticket";
import {TicketService} from "@/lib/api/service/ticketService";
 
/**
 * Hook personnalisé pour récupérer les tickets du client avec gestion d'état
 * @returns Objet contenant les tickets, l'état de chargement et les erreurs
 * @example
 * const { tickets, loading, error } = useTickets();
 * if (loading) return <Spinner />;
 * if (error) return <div>Erreur de chargement des tickets</div>;
 */
export function useTickets() {
  const [tickets, setTickets] = useState<Ticket[]>([]);
  const [loading, setLoading] = useState<boolean>(true);
  const [error, setError] = useState<Error | null>(null);
 
  useEffect(() => {
    const fetchTickets = async () => {
      setLoading(true);
      setError(null);
      try {
        const data = await TicketService.getAllClientTickets();
        console.log("data", data);
        setTickets(data);
      } catch (e) {
        const err = e as Error;
        console.error("erreur", err.message);
        setTickets([]);
      } finally {
        setLoading(false);
      }
    };
 
    fetchTickets();
  }, []);
 
  return { tickets, loading, error };
}