167 lines
5.3 KiB
TypeScript
167 lines
5.3 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { Dialog } from '@headlessui/react';
|
|
import { XMarkIcon } from '@heroicons/react/24/outline';
|
|
import { participantService } from '@/lib/services';
|
|
|
|
interface AddParticipantModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
campaignId: string;
|
|
campaignTitle: string;
|
|
}
|
|
|
|
export default function AddParticipantModal({
|
|
isOpen,
|
|
onClose,
|
|
onSuccess,
|
|
campaignId,
|
|
campaignTitle
|
|
}: AddParticipantModalProps) {
|
|
const [formData, setFormData] = useState({
|
|
first_name: '',
|
|
last_name: '',
|
|
email: ''
|
|
});
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
setError('');
|
|
|
|
try {
|
|
await participantService.create({
|
|
campaign_id: campaignId,
|
|
first_name: formData.first_name,
|
|
last_name: formData.last_name,
|
|
email: formData.email
|
|
});
|
|
onSuccess();
|
|
// Reset form
|
|
setFormData({
|
|
first_name: '',
|
|
last_name: '',
|
|
email: ''
|
|
});
|
|
} catch (err) {
|
|
setError('Erreur lors de l\'ajout du participant');
|
|
console.error(err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const { name, value } = e.target;
|
|
setFormData(prev => ({
|
|
...prev,
|
|
[name]: value
|
|
}));
|
|
};
|
|
|
|
return (
|
|
<Dialog open={isOpen} onClose={onClose} className="relative z-50">
|
|
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
|
|
|
|
<div className="fixed inset-0 flex items-center justify-center p-4">
|
|
<Dialog.Panel className="mx-auto max-w-md w-full bg-white rounded-lg shadow-xl">
|
|
<div className="flex items-center justify-between p-6 border-b border-gray-200">
|
|
<Dialog.Title className="text-lg font-medium text-gray-900">
|
|
Ajouter un participant
|
|
</Dialog.Title>
|
|
<button
|
|
onClick={onClose}
|
|
className="text-gray-400 hover:text-gray-600"
|
|
>
|
|
<XMarkIcon className="h-6 w-6" />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="px-6 py-4 bg-green-50 border-b border-gray-200">
|
|
<p className="text-sm text-green-800">
|
|
<span className="font-medium">Campagne:</span> {campaignTitle}
|
|
</p>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} className="p-6 space-y-4">
|
|
{error && (
|
|
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<div>
|
|
<label htmlFor="first_name" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Prénom *
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="first_name"
|
|
name="first_name"
|
|
value={formData.first_name}
|
|
onChange={handleChange}
|
|
required
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
|
|
placeholder="Prénom du participant"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="last_name" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Nom *
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="last_name"
|
|
name="last_name"
|
|
value={formData.last_name}
|
|
onChange={handleChange}
|
|
required
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
|
|
placeholder="Nom du participant"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Email *
|
|
</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
name="email"
|
|
value={formData.email}
|
|
onChange={handleChange}
|
|
required
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
|
|
placeholder="email@exemple.com"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center 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-white border border-gray-300 rounded-md hover:bg-gray-50"
|
|
>
|
|
Annuler
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent rounded-md hover:bg-indigo-700 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
{loading ? 'Ajout...' : 'Ajouter le participant'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</Dialog.Panel>
|
|
</div>
|
|
</Dialog>
|
|
);
|
|
}
|