import React, { useEffect, useState } from 'react'; import type { Article, Comment } from '@/types/article'; export default function Show({ articleId }: { articleId: number }) { const [article, setArticle] = useState
(null); const [form, setForm] = useState({ author_name: '', content: '' }); // загрузка статьи useEffect(() => { fetch(`/api/articles/${articleId}`) .then((res) => res.json()) .then((res) => setArticle(res.data)) // <-- берём data .catch(console.error); }, [articleId]); const submit = async () => { if (!form.author_name || !form.content) return; const res = await fetch(`/api/articles/${articleId}/comments`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(form), }); if (!res.ok) return; const result = await res.json(); const newComment: Comment = result.data; // <-- API возвращает CommentResource setArticle((prev) => prev ? { ...prev, comments: [...prev.comments, newComment] } : prev, ); setForm({ author_name: '', content: '' }); }; if (!article) return
Статья не найдена
; return (

{article.title}

{article.content}

Комментарии

{article.comments.map((c) => (
{c.author_name}
{c.content}
))}

Добавить комментарий

setForm({ ...form, author_name: e.target.value }) } />