"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; import { Plus, X, Link as LinkIcon, Loader2 } from "lucide-react"; import { apiClient } from "@/services/client"; interface CreateSkillFormProps { categoryName: string; onSuccess: (skillId: string) => void; onCancel: () => void; } export function CreateSkillForm({ categoryName, onSuccess, onCancel, }: CreateSkillFormProps) { const [formData, setFormData] = useState({ name: "", description: "", icon: "", links: [""], }); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const addLink = () => { setFormData(prev => ({ ...prev, links: [...prev.links, ""] })); }; const removeLink = (index: number) => { setFormData(prev => ({ ...prev, links: prev.links.filter((_, i) => i !== index) })); }; const updateLink = (index: number, value: string) => { setFormData(prev => ({ ...prev, links: prev.links.map((link, i) => i === index ? value : link) })); }; const generateSkillId = (name: string) => { return name .toLowerCase() .replace(/[^a-z0-9\s]/g, '') .replace(/\s+/g, '-') .trim(); }; const getCategoryId = (categoryName: string) => { // Convertir le nom de catégorie en ID (ex: "Cloud" -> "cloud") return categoryName.toLowerCase(); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); setIsLoading(true); try { // Validation if (!formData.name.trim()) { throw new Error("Le nom de la compétence est requis"); } if (!formData.description.trim()) { throw new Error("La description est requise"); } const skillId = generateSkillId(formData.name); const categoryId = getCategoryId(categoryName); // Filtrer les liens vides const validLinks = formData.links.filter(link => link.trim()); const skillData = { id: skillId, name: formData.name.trim(), description: formData.description.trim(), icon: formData.icon.trim() || "fas-cog", links: validLinks, }; const success = await apiClient.createSkill(categoryId, skillData); if (success) { onSuccess(skillId); } else { throw new Error("Erreur lors de la création de la compétence"); } } catch (err) { setError(err instanceof Error ? err.message : "Une erreur est survenue"); } finally { setIsLoading(false); } }; return (
{error && (
{error}
)}
setFormData(prev => ({ ...prev, name: e.target.value }))} disabled={isLoading} />