diff --git a/src/app/admin/articles/[id]/page.tsx b/src/app/admin/articles/[id]/page.tsx new file mode 100644 index 0000000..4a8620b --- /dev/null +++ b/src/app/admin/articles/[id]/page.tsx @@ -0,0 +1,167 @@ +'use client'; + +import { useEffect, useState } from 'react'; +import { useParams, useRouter } from 'next/navigation'; +import { ArticleStatus, Bias } from '@prisma/client'; + +type Article = { + id: string; + title: string; + summary: string | null; + url: string; + primaryBias: Bias; + politicalTags: string; + rankScore: number; + credibilityScore: number; + status: ArticleStatus; + topics: string; + aiExplanation: string | null; +}; + +export default function EditArticlePage() { + const { id } = useParams<{ id: string }>(); + const router = useRouter(); + const [article, setArticle] = useState
(null); + const [saving, setSaving] = useState(false); + + useEffect(() => { + fetch(`/api/admin/articles/${id}`) + .then((res) => res.json()) + .then((data) => { + setArticle(data); + }); + }, [id]); + + async function handleSave(e: React.FormEvent) { + e.preventDefault(); + if (!article) return; + setSaving(true); + + const res = await fetch(`/api/admin/articles/${id}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + title: article.title, + summary: article.summary, + primaryBias: article.primaryBias, + politicalTags: JSON.parse(article.politicalTags || '[]'), + rankScore: article.rankScore, + credibilityScore: article.credibilityScore, + status: article.status, + topics: JSON.parse(article.topics || '[]'), + }), + }); + + setSaving(false); + if (res.ok) { + router.push('/admin/articles'); + } else { + alert('Failed to save'); + } + } + + if (!article) return

Loading...

; + + return ( +
+

Edit Article

+
+
+ + setArticle({ ...article, title: e.target.value })} + className="w-full px-3 py-2 border rounded-lg dark:bg-neutral-800 dark:border-neutral-700" + /> +
+
+ +