From 02d612e02608aa9339738723ff856fe4dd946380 Mon Sep 17 00:00:00 2001 From: Mathis HERRIOT <197931332+0x485254@users.noreply.github.com> Date: Wed, 21 Jan 2026 13:44:10 +0100 Subject: [PATCH] feat(contents): add `UserContentEditDialog` component for editing user content - Introduced `UserContentEditDialog` to enable inline editing of user-generated content. - Integrated form handling with `react-hook-form` for validation and form state management. - Added category selection support and updated content saving functionality. - Included success and error feedback with loading states for better user experience. --- .../components/user-content-edit-dialog.tsx | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 frontend/src/components/user-content-edit-dialog.tsx diff --git a/frontend/src/components/user-content-edit-dialog.tsx b/frontend/src/components/user-content-edit-dialog.tsx new file mode 100644 index 0000000..86d7d00 --- /dev/null +++ b/frontend/src/components/user-content-edit-dialog.tsx @@ -0,0 +1,158 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { useForm } from "react-hook-form"; +import { toast } from "sonner"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { CategoryService } from "@/services/category.service"; +import { ContentService } from "@/services/content.service"; +import type { Category, Content } from "@/types/content"; + +interface UserContentEditDialogProps { + content: Content | null; + open: boolean; + onOpenChange: (open: boolean) => void; + onSuccess: () => void; +} + +export function UserContentEditDialog({ + content, + open, + onOpenChange, + onSuccess, +}: UserContentEditDialogProps) { + const [loading, setLoading] = useState(false); + const [categories, setCategories] = useState([]); + + const form = useForm<{ title: string; categoryId: string }>({ + defaultValues: { + title: "", + categoryId: "", + }, + }); + + useEffect(() => { + CategoryService.getAll().then(setCategories).catch(console.error); + }, []); + + useEffect(() => { + if (content) { + form.reset({ + title: content.title, + categoryId: content.categoryId || "none", + }); + } + }, [content, form]); + + const onSubmit = async (values: { title: string; categoryId: string }) => { + if (!content) return; + setLoading(true); + try { + const data = { + ...values, + categoryId: values.categoryId === "none" ? null : values.categoryId, + }; + await ContentService.update(content.id, data); + toast.success("Mème mis à jour !"); + onSuccess(); + onOpenChange(false); + } catch (error) { + console.error(error); + toast.error("Erreur lors de la mise à jour."); + } finally { + setLoading(false); + } + }; + + return ( + + + + Modifier mon mème + +
+ + ( + + Titre + + + + + + )} + /> + ( + + Catégorie + + + + )} + /> + + + + + + +
+
+ ); +}