All files / components/componentsEvenement/lieux LieuxManagement.tsx

100% Statements 38/38
100% Branches 8/8
100% Functions 8/8
100% Lines 38/38

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132    2x 2x 2x     2x 2x 2x 2x 2x   2x 42x                             42x   42x 42x 42x         42x 11x 11x   42x 5x 5x 2x 1x           3x 2x         3x 3x             42x 2x 2x 1x         1x             42x       7x                                   4x             3x 3x 3x                       1x              
'use client';
 
import { useState } from 'react';
import { useLieuxManagement } from '@/hooks/useLieuxManagement';
import { useSessionExpiry } from '@/hooks/useSessionExpiry';
import { Lieu } from '@/types/sportEvenement/lieu';
import { CreateLieuRequest } from '@/lib/api/services/evenementSports/lieuService';
import Notification from '@/components/notification';
import LieuxHeader from './LieuxHeader';
import SearchAndFilters from './SearchAndFilters';
import LieuxTable from './LieuxTable';
import LieuModal from './LieuModal';
 
export default function LieuxManagement() {
      useSessionExpiry();
 
      const {
        lieux,
        searchTerm,
        loading,
        error,
        createLoading,
        createError,
        loadLieux,
        createLieu,
        updateLieu,
        deleteLieu,
        handleSearch,
        setCreateError
      } = useLieuxManagement();
 
      const [showModal, setShowModal] = useState(false);
      const [editingLieu, setEditingLieu] = useState<Lieu | null>(null);
      const [notification, setNotification] = useState<{
          message: string;
          type: 'success' | 'error' | 'info';
      } | null>(null);
 
      const handleOpenModal = (lieu?: Lieu) => {
        setEditingLieu(lieu || null);
        setShowModal(true);
      };
    const handleSaveLieu = async (lieuData: CreateLieuRequest) => {
      try {
        if (editingLieu) {
          await updateLieu(editingLieu.id, lieuData);
          setNotification({
            message: 'Lieu modifié avec succès !',
            type: 'success'
          });
        } else {
          // Mode création
          await createLieu(lieuData);
          setNotification({
            message: 'Lieu créé avec succès !',
            type: 'success'
          });
        }
        setShowModal(false);
        setEditingLieu(null);
      } catch {
        // L'erreur est déjà gérée dans le hook
      }
    };
 
    // Fonction pour supprimer un lieu
    const handleDeleteLieu = async (id: number) => {
      try {
        await deleteLieu(id);
        setNotification({
          message: 'Lieu supprimé avec succès !',
          type: 'success'
        });
      } catch {
        setNotification({
          message: 'Erreur lors de la suppression du lieu',
          type: 'error'
        });
      }
    };
 
    return (
      <div className="min-h-screen bg-base-200">
        {/* Header */}
        <LieuxHeader 
          onCreateClick={()=>handleOpenModal()}
        />
 
        {/* Main Content */}
        <main className="max-w-7xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
          {/* Search */}
          <SearchAndFilters
            searchTerm={searchTerm}
            onSearch={handleSearch}
          />
 
          {/* Lieux Table */}
          <LieuxTable
            lieux={lieux}
            loading={loading}
            searchTerm={searchTerm}
            onRefresh={loadLieux}
            onDelete={handleDeleteLieu}
            onEdit={(lieux)=>handleOpenModal(lieux)}
            error={error}
          />
        </main>
        <LieuModal
          isOpen={showModal}
          onClose={() => {
            setShowModal(false);
            setEditingLieu(null);
            setCreateError(null);
          }}
          onSave={handleSaveLieu}
          loading={createLoading}
          error={createError}
          lieu={editingLieu || undefined}
        />
        {notification && (
          <Notification
            message={notification.message}
            type={notification.type}
            onClose={() => {
              setNotification(null);
            }}
          />
        )}
      </div>
    );
}