import { useEffect, useState } from 'react' import { api } from '../lib/api.js' export default function Feed() { const [posts, setPosts] = useState([]) const [openId, setOpenId] = useState(null) const [comments, setComments] = useState([]) const [draft, setDraft] = useState('') const [error, setError] = useState(null) const load = () => api.get('/posts/feed').then(setPosts).catch(e => setError(e.message)) useEffect(() => { load() }, []) const open = async (id) => { setOpenId(id) setComments(await api.get(`/posts/${id}/comments`)) } const send = async () => { if (!draft.trim()) return try { await api.post(`/posts/${openId}/comments`, { description: draft }) setDraft('') setComments(await api.get(`/posts/${openId}/comments`)) load() } catch (e) { setError(e.message) } } return (
{error &&
{error}
} {posts.length === 0 &&

Nothing published yet.

} {posts.map(p => (

{p.recipeName || '(photo post)'}

{p.kcalPerServing} kcal / portion
by {p.authorUsername} · {new Date(p.createdAt).toLocaleString()} · {p.commentCount} comments {p.isFavourite && ' · favourite'}
{openId === p.id && (
{comments.map(c => (
{c.authorUsername} {new Date(c.createdAt).toLocaleString()}
{c.description}
))}
setDraft(e.target.value)} />
)}
))}
) }