import React, { useState, useEffect } from 'react'; interface CommentFormProps { postId: string; } interface Comment { name: string; comment: string; createdAt: string; } const CommentSection: React.FC = ({ postId }) => { const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [comment, setComment] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitted, setIsSubmitted] = useState(false); const [error, setError] = useState(null); const [comments, setComments] = useState([]); useEffect(() => { const fetchComments = async () => { try { const response = await fetch(`/api/comments?postId=${postId}`); if (response.ok) { const data = await response.json(); setComments(data); } else { console.error('Failed to fetch comments'); } } catch (err) { console.error('Error fetching comments:', err); } }; fetchComments(); }, [postId]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!name.trim() || !email.trim() || !comment.trim()) { setError('All fields are required'); return; } setIsSubmitting(true); setError(null); try { const response = await fetch('/api/comments', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, email, comment, postId }), }); if (response.ok) { setName(''); setEmail(''); setComment(''); setIsSubmitted(true); setIsSubmitting(false); // Fetch updated comments const updatedComments = await fetch(`/api/comments?postId=${postId}`).then((res) => res.json() ); setComments(updatedComments); setTimeout(() => setIsSubmitted(false), 5000); } else { const data = await response.json(); setError(data.message || 'Something went wrong. Please try again later.'); setIsSubmitting(false); } } catch (err) { setError('Something went wrong. Please try again later.'); setIsSubmitting(false); } }; return (

Leave a Comment

{isSubmitted && (
Your comment has been submitted. Thank you!
)} {error && (
{error}
)}
setName(e.target.value)} className="w-full px-4 py-2 border border-primary-300 rounded-md focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" required onFocus={() => { if (typeof window !== 'undefined') { const savedName = localStorage.getItem('name'); if (savedName) setName(savedName); } }} />
setEmail(e.target.value)} className="w-full px-4 py-2 border border-primary-300 rounded-md focus:outline-none focus:ring-2 focus:ring-accent focus:border-transparent" required onFocus={() => { if (typeof window !== 'undefined') { const savedEmail = localStorage.getItem('email'); if (savedEmail) setEmail(savedEmail); } }} />
{/*
{ if (typeof window !== 'undefined') { const saveInfo = e.target.checked; localStorage.setItem('saveInfo', saveInfo.toString()); if (saveInfo) { localStorage.setItem('name', name); localStorage.setItem('email', email); } else { localStorage.removeItem('name'); localStorage.removeItem('email'); } } }} />
*/}

Comments ({comments.length})

{comments.map((comment) => (

{comment.name}

{new Date(comment.createdAt).toLocaleDateString()}

{comment.comment}

))}
); }; export default CommentSection;