true message
This commit is contained in:
@@ -1,318 +0,0 @@
|
||||
import { useState } from "react";
|
||||
import DashboardLayout from "@/components/DashboardLayout";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { trpc } from "@/lib/trpc";
|
||||
import { Plus, Pencil, Trash2, Bell, BellOff } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export default function AdminRappels() {
|
||||
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
|
||||
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
|
||||
const [formData, setFormData] = useState({
|
||||
nom: "",
|
||||
joursAvant: 15,
|
||||
emailTemplate: "",
|
||||
actif: true,
|
||||
});
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
|
||||
const { data: rappels = [], isLoading, refetch } = trpc.rappels.list.useQuery();
|
||||
const createMutation = trpc.rappels.create.useMutation();
|
||||
const updateMutation = trpc.rappels.update.useMutation();
|
||||
const deleteMutation = trpc.rappels.delete.useMutation();
|
||||
|
||||
const resetForm = () => {
|
||||
setFormData({
|
||||
nom: "",
|
||||
joursAvant: 15,
|
||||
emailTemplate: "",
|
||||
actif: true,
|
||||
});
|
||||
setEditingId(null);
|
||||
};
|
||||
|
||||
const handleCreate = async () => {
|
||||
try {
|
||||
await createMutation.mutateAsync(formData);
|
||||
toast.success("Rappel créé avec succès");
|
||||
setIsCreateDialogOpen(false);
|
||||
resetForm();
|
||||
refetch();
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || "Erreur lors de la création du rappel");
|
||||
}
|
||||
};
|
||||
|
||||
const handleEdit = (rappel: any) => {
|
||||
setFormData({
|
||||
nom: rappel.nom,
|
||||
joursAvant: rappel.joursAvant,
|
||||
emailTemplate: rappel.emailTemplate,
|
||||
actif: rappel.actif,
|
||||
});
|
||||
setEditingId(rappel.id);
|
||||
setIsEditDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleUpdate = async () => {
|
||||
if (!editingId) return;
|
||||
|
||||
try {
|
||||
await updateMutation.mutateAsync({ id: editingId, ...formData });
|
||||
toast.success("Rappel mis à jour avec succès");
|
||||
setIsEditDialogOpen(false);
|
||||
resetForm();
|
||||
refetch();
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || "Erreur lors de la mise à jour du rappel");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: number) => {
|
||||
if (!confirm("Êtes-vous sûr de vouloir supprimer ce rappel ?")) return;
|
||||
|
||||
try {
|
||||
await deleteMutation.mutateAsync({ id });
|
||||
toast.success("Rappel supprimé avec succès");
|
||||
refetch();
|
||||
} catch (error: any) {
|
||||
toast.error(error.message || "Erreur lors de la suppression du rappel");
|
||||
}
|
||||
};
|
||||
|
||||
const templateExemple = `Bonjour {{prenom}} {{nom}},
|
||||
|
||||
Nous vous rappelons que votre formation "{{formationNom}}" - {{sequenceNom}} aura lieu dans {{joursAvant}} jours.
|
||||
|
||||
📅 Date : {{dateDebut}}
|
||||
📍 Lieu : {{lieu}}
|
||||
👨🏫 Formateur : {{formateur}}
|
||||
|
||||
Cordialement,
|
||||
L'équipe Formation Manager Itinova`;
|
||||
|
||||
return (
|
||||
<DashboardLayout>
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight">Rappels automatiques</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Configurez les rappels envoyés automatiquement avant les formations
|
||||
</p>
|
||||
</div>
|
||||
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button onClick={() => resetForm()}>
|
||||
<Plus className="mr-2 h-4 w-4" />
|
||||
Nouveau rappel
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Créer un rappel</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configurez un nouveau rappel automatique à envoyer avant les formations
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<Label htmlFor="nom">Nom du rappel</Label>
|
||||
<Input
|
||||
id="nom"
|
||||
placeholder="Ex: Rappel J-15"
|
||||
value={formData.nom}
|
||||
onChange={(e) => setFormData({ ...formData, nom: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="joursAvant">Jours avant la formation</Label>
|
||||
<Input
|
||||
id="joursAvant"
|
||||
type="number"
|
||||
min="1"
|
||||
placeholder="15"
|
||||
value={formData.joursAvant}
|
||||
onChange={(e) => setFormData({ ...formData, joursAvant: parseInt(e.target.value) || 0 })}
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground mt-1">
|
||||
Nombre de jours avant la première date de formation
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="emailTemplate">Template d'email</Label>
|
||||
<Textarea
|
||||
id="emailTemplate"
|
||||
placeholder={templateExemple}
|
||||
value={formData.emailTemplate}
|
||||
onChange={(e) => setFormData({ ...formData, emailTemplate: e.target.value })}
|
||||
rows={10}
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground mt-1">
|
||||
Variables disponibles : {"{{"} prenom {"}}"}, {"{{"} nom {"}}"}, {"{{"} formationNom {"}}"}, {"{{"} sequenceNom {"}}"}, {"{{"} dateDebut {"}}"}, {"{{"} lieu {"}}"}, {"{{"} formateur {"}}"}, {"{{"} joursAvant {"}}"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center space-x-2">
|
||||
<Switch
|
||||
id="actif"
|
||||
checked={formData.actif}
|
||||
onCheckedChange={(checked) => setFormData({ ...formData, actif: checked })}
|
||||
/>
|
||||
<Label htmlFor="actif">Activer ce rappel</Label>
|
||||
</div>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="outline" onClick={() => setIsCreateDialogOpen(false)}>
|
||||
Annuler
|
||||
</Button>
|
||||
<Button onClick={handleCreate} disabled={createMutation.isPending}>
|
||||
{createMutation.isPending ? "Création..." : "Créer"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Liste des rappels</CardTitle>
|
||||
<CardDescription>
|
||||
Gérez les rappels automatiques envoyés aux apprenants avant les formations
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="text-center py-8 text-muted-foreground">Chargement...</div>
|
||||
) : rappels.length === 0 ? (
|
||||
<div className="text-center py-8 text-muted-foreground">
|
||||
Aucun rappel configuré. Créez votre premier rappel pour commencer.
|
||||
</div>
|
||||
) : (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Nom</TableHead>
|
||||
<TableHead>Jours avant</TableHead>
|
||||
<TableHead>Statut</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{rappels.map((rappel) => (
|
||||
<TableRow key={rappel.id}>
|
||||
<TableCell className="font-medium">{rappel.nom}</TableCell>
|
||||
<TableCell>J-{rappel.joursAvant}</TableCell>
|
||||
<TableCell>
|
||||
{rappel.actif ? (
|
||||
<span className="inline-flex items-center gap-1 text-green-600">
|
||||
<Bell className="h-4 w-4" />
|
||||
Actif
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-gray-500">
|
||||
<BellOff className="h-4 w-4" />
|
||||
Inactif
|
||||
</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => handleEdit(rappel)}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => handleDelete(rappel.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Dialog de modification */}
|
||||
<Dialog open={isEditDialogOpen} onOpenChange={setIsEditDialogOpen}>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Modifier le rappel</DialogTitle>
|
||||
<DialogDescription>
|
||||
Modifiez les paramètres du rappel automatique
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<Label htmlFor="edit-nom">Nom du rappel</Label>
|
||||
<Input
|
||||
id="edit-nom"
|
||||
placeholder="Ex: Rappel J-15"
|
||||
value={formData.nom}
|
||||
onChange={(e) => setFormData({ ...formData, nom: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="edit-joursAvant">Jours avant la formation</Label>
|
||||
<Input
|
||||
id="edit-joursAvant"
|
||||
type="number"
|
||||
min="1"
|
||||
placeholder="15"
|
||||
value={formData.joursAvant}
|
||||
onChange={(e) => setFormData({ ...formData, joursAvant: parseInt(e.target.value) || 0 })}
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground mt-1">
|
||||
Nombre de jours avant la première date de formation
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="edit-emailTemplate">Template d'email</Label>
|
||||
<Textarea
|
||||
id="edit-emailTemplate"
|
||||
value={formData.emailTemplate}
|
||||
onChange={(e) => setFormData({ ...formData, emailTemplate: e.target.value })}
|
||||
rows={10}
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground mt-1">
|
||||
Variables disponibles : {"{{"} prenom {"}}"}, {"{{"} nom {"}}"}, {"{{"} formationNom {"}}"}, {"{{"} sequenceNom {"}}"}, {"{{"} dateDebut {"}}"}, {"{{"} lieu {"}}"}, {"{{"} formateur {"}}"}, {"{{"} joursAvant {"}}"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center space-x-2">
|
||||
<Switch
|
||||
id="edit-actif"
|
||||
checked={formData.actif}
|
||||
onCheckedChange={(checked) => setFormData({ ...formData, actif: checked })}
|
||||
/>
|
||||
<Label htmlFor="edit-actif">Activer ce rappel</Label>
|
||||
</div>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="outline" onClick={() => setIsEditDialogOpen(false)}>
|
||||
Annuler
|
||||
</Button>
|
||||
<Button onClick={handleUpdate} disabled={updateMutation.isPending}>
|
||||
{updateMutation.isPending ? "Mise à jour..." : "Mettre à jour"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user