diff --git a/components/community-suggestion-form.tsx b/components/community-suggestion-form.tsx new file mode 100644 index 0000000..3c01adb --- /dev/null +++ b/components/community-suggestion-form.tsx @@ -0,0 +1,161 @@ +"use client"; + +import { useState } from "react"; +import { MessageSquarePlus, Send, X } from "lucide-react"; +import { cn } from "@/lib/utils"; + +interface CommunitySuggestionFormProps { + gameId: string; + gameTitle: string; + editableFields: Array<{ + name: string; + label: string; + currentValue: string; + }>; + className?: string; +} + +export function CommunitySuggestionForm({ + gameId, + gameTitle, + editableFields, + className, +}: CommunitySuggestionFormProps) { + const [isOpen, setIsOpen] = useState(false); + const [selectedField, setSelectedField] = useState(""); + const [proposedValue, setProposedValue] = useState(""); + const [reason, setReason] = useState(""); + const [submitting, setSubmitting] = useState(false); + const [success, setSuccess] = useState(false); + const [error, setError] = useState(null); + + const handleSubmit = async () => { + if (!selectedField || !proposedValue.trim()) return; + + setSubmitting(true); + setError(null); + + try { + const res = await fetch("/api/community-suggestions", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + gameId, + fieldName: selectedField, + proposedValue: proposedValue.trim(), + reason: reason.trim() || undefined, + }), + }); + + if (!res.ok) { + const data = await res.json(); + throw new Error(data.error || "Failed to submit suggestion"); + } + + setSuccess(true); + setTimeout(() => { + setIsOpen(false); + setSuccess(false); + setSelectedField(""); + setProposedValue(""); + setReason(""); + }, 2000); + } catch (err) { + setError(err instanceof Error ? err.message : "Unknown error"); + } finally { + setSubmitting(false); + } + }; + + if (!isOpen) { + return ( + + ); + } + + return ( +
+
+

Suggest an Edit for {gameTitle}

+ +
+ + {success ? ( +
+ Suggestion submitted! A moderator will review it. +
+ ) : ( +
+
+ + +
+ + {selectedField && ( + <> +
+ +