All files / components/componentsEvenement/epreuve EpreuveModal.tsx

100% Statements 21/21
100% Branches 20/20
100% Functions 6/6
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 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 1292x                             2x                 68x           68x 68x 68x 68x     68x 29x 6x         23x             68x 2x 2x 2x       68x   66x                                         5x                         5x           128x                                                          
import { useState, useEffect } from 'react';
import { CreateEpreuveRequest } from '@/lib/api/services/evenementSports/epreuveService';
import { Epreuve } from '@/types/sportEvenement/epreuve';
import { Discipline } from '@/types/sportEvenement/discipline';
 
interface Props {
    isOpen: boolean;
    onClose: () => void;
    onSave: (epreuveData: CreateEpreuveRequest) => void;
    loading: boolean;
    error: string | null;
    epreuve?: Epreuve;
    disciplines: Discipline[];
}
 
export default function EpreuveModal({ 
    isOpen, 
    onClose, 
    onSave, 
    loading, 
    error, 
    epreuve,
    disciplines
}: Props) {
    const [formData, setFormData] = useState<CreateEpreuveRequest>({
      libelle: '',
      disciplineId: 0
    });
 
    // Déterminer si c'est une création ou une modification
    const isEditing = Boolean(epreuve);
    const title = isEditing ? 'Modifier l\'épreuve' : 'Créer une nouvelle épreuve';
    const submitLabel = isEditing ? 'Modifier' : 'Créer';
    const loadingLabel = isEditing ? 'Modification...' : 'Création...';
 
    // Initialiser le formulaire avec les données de l'épreuve en mode édition
    useEffect(() => {
      if (epreuve) {
        setFormData({
          libelle: epreuve.libelle,
          disciplineId: epreuve.discipline.id
        });
      } else {
        setFormData({
          libelle: '',
          disciplineId: disciplines.length > 0 ? disciplines[0].id : 0
        });
      }
    }, [epreuve, disciplines]);
 
    const handleSubmit = (e: React.FormEvent) => {
      e.preventDefault();
      if (formData.libelle.trim() && formData.disciplineId > 0) {
        onSave(formData);
      }
    };
    
    if (!isOpen) return null;
 
    return (
        <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
            <div className="bg-white rounded-lg p-6 w-full max-w-md mx-4">
                <div className="flex items-center justify-center mb-4" style={{ minHeight: '3rem' }}>
                    <h3 className="text-lg text-black font-semibold">{title}</h3>
                </div>
 
                {error && (
                    <div className="mb-4 text-sm text-red-600 bg-red-50 p-2 rounded">
                        {error}
                    </div>
                )}
              
                <form onSubmit={handleSubmit} className="space-y-4">
                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">
                            Libellé de l&apos;épreuve
                        </label>
                        <input
                            type="text"
                            value={formData.libelle}
                            onChange={(e) => setFormData({ ...formData, libelle: e.target.value })}
                            className="w-full px-3 py-2 border border-gray-300 text-black rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                            placeholder="Ex: 100m sprint, Saut en hauteur..."
                            required
                        />
                    </div>
                    
                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">
                            Discipline
                        </label>
                        <select
                            value={formData.disciplineId}
                            onChange={(e) => setFormData({ ...formData, disciplineId: Number(e.target.value) })}
                            className="w-full px-3 py-2 border border-gray-300 text-black rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                            required
                        >
                            <option value={0}>Sélectionner une discipline</option>
                            {disciplines.map((discipline) => (
                                <option key={discipline.id} value={discipline.id}>
                                    {discipline.nom}
                                </option>
                            ))}
                        </select>
                    </div>
                    
                    <div className="flex justify-end space-x-3 pt-4">
                        <button
                            type="button"
                            onClick={onClose}
                            className="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-200 rounded-md hover:bg-gray-300"
                            disabled={loading}
                        >
                            Annuler
                        </button>
                        <button
                            type="submit"
                            className="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-md hover:bg-blue-700 disabled:bg-blue-300"
                            disabled={loading || formData.disciplineId === 0}
                        >
                            {loading ? loadingLabel : submitLabel}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    );
}