All files / hook useOffre.ts

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

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 384x   4x                   4x 5x 5x 5x   5x 3x 3x 3x 3x 3x 2x   1x 1x   3x       3x     5x    
import {useState, useEffect} from "react";
import {Offre} from "@/type/achat/offre";
import { OffreService } from "@/lib/api/service/offreService";
 
/**
 * Hook personnalisé pour récupérer la liste des offres avec gestion d'état
 * @returns Objet contenant les offres, l'état de chargement et les erreurs
 * @example
 * const { offres, loading, error } = useOffres();
 * if (loading) return <Spinner />;
 * if (error) return <div>Erreur: {error.message}</div>;
 */
export function useOffres() {
  const [offres, setOffres] = useState<Offre[]>([]);
  const [loading, setLoading] = useState<boolean>(true);
  const [error, setError] = useState<Error | null>(null);
 
  useEffect(() => {
    const fetchOffres = async () => {
      setLoading(true);
      setError(null);
      try {
        const data = await OffreService.getAllOffre();
        setOffres(data);
      } catch (err) {
        setError(err as Error);
        setOffres([]);
      } finally {
        setLoading(false);
      }
    };
 
    fetchOffres();
  }, []);
 
  return { offres, loading, error };
}