All files / hooks useOffreManagement.ts

100% Statements 54/54
75% Branches 3/4
100% Functions 11/11
100% Lines 49/49

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 1132x   2x     2x 26x 26x 26x 26x 26x     26x 9x 9x 9x 9x 8x   1x 1x   9x         26x 3x 3x 3x   3x 2x 2x       2x   1x       1x   3x         26x 2x 2x 2x   2x 2x 1x       1x   1x       1x   2x       26x 2x 2x 2x     1x         1x       1x       26x 9x     26x                            
import { useState, useEffect } from 'react';
import { Offre } from '@/types/offre/offre';
import { OffreService } from '@/lib/api/services/offres/offreService';
import { Notification } from '@/types/common/notification';
 
export function useOffresManagement() {
  const [offres, setOffres] = useState<Offre[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [formLoading, setFormLoading] = useState(false);
  const [formNotification, setFormNotification] = useState<Notification | null>(null);
 
  // Charger les offres
  const loadOffres = async () => {
    try {
      setLoading(true);
      setError(null);
      const apiOffres = await OffreService.getAll();
      setOffres(apiOffres);
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : 'Erreur lors du chargement des offres';
      setError(errorMessage);
    } finally {
      setLoading(false);
    }
  };
 
  // Créer une offre
  const createOffre = async (offreData: Offre) => {
    try {
      setFormLoading(true);
      setFormNotification(null);
 
      const newOffre = await OffreService.create(offreData);
      setOffres(prev => [...prev, newOffre]);
      setFormNotification({
        message: 'Offre créée avec succès',
        type: 'success'
      });
      return newOffre;
    } catch (err) {
      setFormNotification({
        message: "Erreur lors de la création de l'offre",
        type: 'error'
      });
      throw err;
    } finally {
      setFormLoading(false);
    }
  };
 
  // Modifier une offre
  const updateOffre = async (id: number, offreData: Offre) => {
    try {
      setFormLoading(true);
      setFormNotification(null);
 
      const updatedOffre = await OffreService.update({ ...offreData, id });
      setOffres(prev => prev.map(offre => offre.id === id ? updatedOffre : offre));
      setFormNotification({
        message: 'Offre modifiée avec succès',
        type: 'success'
      });
      return updatedOffre;
    } catch (err) {
      setFormNotification({
        message: "Erreur lors de la modification de l'offre",
        type: 'error'
      });
      throw err;
    } finally {
      setFormLoading(false);
    }
  };
 
  const deleteOffre = async (id: number) => {
    try {
      await OffreService.delete(id);
      setOffres(prev => prev.filter(offre => offre.id !== id));
 
      // Notification de succès pour suppression
      setFormNotification({
        message: 'Offre supprimée avec succès',
        type: 'success'
      });
    } catch (err) {
      setFormNotification({
        message: "Erreur lors de la suppression de l'offre",
        type: 'error'
      });
      console.log(err)
    }
  };
 
  useEffect(() => {
    loadOffres();
  }, []);
 
  return {
    offres,
    loading,
    error,
    formLoading,
    formNotification,
    createOffre,
    updateOffre,
    deleteOffre,
    setFormLoading,
    setFormNotification,
    loadOffres
  };
}