refactoring majeur (code dupliqué, mort, ...)

- Économie : ~1240 lignes de code dupliqué
- Réduction : ~60% du code modal
- Amélioration : Cohérence et maintenabilité
This commit is contained in:
Yannick Le Duc
2025-08-27 12:45:37 +02:00
parent 6acc7d9d35
commit dc388bf371
25 changed files with 1446 additions and 1821 deletions

View File

@@ -1,11 +1,12 @@
'use client';
import { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { useEffect } from 'react';
import { Input } from '@/components/ui/input';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Label } from '@/components/ui/label';
import { participantService } from '@/lib/services';
import { Participant } from '@/types';
import { useFormState } from '@/hooks/useFormState';
import { FormModal } from './base/FormModal';
import { handleFormError } from '@/lib/form-utils';
interface EditParticipantModalProps {
isOpen: boolean;
@@ -15,13 +16,13 @@ interface EditParticipantModalProps {
}
export default function EditParticipantModal({ isOpen, onClose, onSuccess, participant }: EditParticipantModalProps) {
const [formData, setFormData] = useState({
const initialData = {
first_name: '',
last_name: '',
email: ''
});
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
};
const { formData, setFormData, loading, setLoading, error, setError, handleChange } = useFormState(initialData);
useEffect(() => {
if (participant) {
@@ -31,7 +32,7 @@ export default function EditParticipantModal({ isOpen, onClose, onSuccess, parti
email: participant.email
});
}
}, [participant]);
}, [participant, setFormData]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
@@ -49,88 +50,63 @@ export default function EditParticipantModal({ isOpen, onClose, onSuccess, parti
onSuccess();
} catch (err: any) {
const errorMessage = err?.message || err?.details || 'Erreur lors de la modification du participant';
setError(`Erreur lors de la modification du participant: ${errorMessage}`);
console.error('Erreur lors de la modification du participant:', err);
setError(handleFormError(err, 'la modification du participant'));
} finally {
setLoading(false);
}
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setFormData(prev => ({
...prev,
[e.target.name]: e.target.value
}));
};
if (!participant) return null;
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle>Modifier le participant</DialogTitle>
<DialogDescription>
Modifiez les informations de ce participant.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4">
{error && (
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg">
<p className="text-sm text-red-600 dark:text-red-400">{error}</p>
</div>
)}
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div className="space-y-2">
<Label htmlFor="first_name">Prénom *</Label>
<Input
id="first_name"
name="first_name"
value={formData.first_name}
onChange={handleChange}
placeholder="Prénom"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="last_name">Nom *</Label>
<Input
id="last_name"
name="last_name"
value={formData.last_name}
onChange={handleChange}
placeholder="Nom"
required
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="email">Email *</Label>
<Input
id="email"
name="email"
type="email"
value={formData.email}
onChange={handleChange}
placeholder="email@example.com"
required
/>
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
Annuler
</Button>
<Button type="submit" disabled={loading}>
{loading ? 'Modification...' : 'Modifier le participant'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
<FormModal
isOpen={isOpen}
onClose={onClose}
onSubmit={handleSubmit}
title="Modifier le participant"
description="Modifiez les informations de ce participant."
loading={loading}
error={error}
submitText="Modifier le participant"
loadingText="Modification..."
>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div className="space-y-2">
<Label htmlFor="first_name">Prénom *</Label>
<Input
id="first_name"
name="first_name"
value={formData.first_name}
onChange={handleChange}
placeholder="Prénom"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="last_name">Nom *</Label>
<Input
id="last_name"
name="last_name"
value={formData.last_name}
onChange={handleChange}
placeholder="Nom"
required
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="email">Email *</Label>
<Input
id="email"
name="email"
type="email"
value={formData.email}
onChange={handleChange}
placeholder="email@example.com"
required
/>
</div>
</FormModal>
);
}