diff --git a/src/components/CommentSection.tsx b/src/components/CommentSection.tsx index b1d055d..35857a8 100644 --- a/src/components/CommentSection.tsx +++ b/src/components/CommentSection.tsx @@ -1,9 +1,15 @@ -import React, { useState } from 'react'; +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(''); @@ -11,11 +17,29 @@ const CommentSection: React.FC = ({ postId }) => { 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(); - // Basic validation if (!name.trim() || !email.trim() || !comment.trim()) { setError('All fields are required'); return; @@ -38,7 +62,12 @@ const CommentSection: React.FC = ({ postId }) => { setIsSubmitted(true); setIsSubmitting(false); - // Reset success message after 5 seconds + // 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(); @@ -54,19 +83,19 @@ const CommentSection: React.FC = ({ postId }) => { return (

Leave a Comment

- + {isSubmitted && (
- Your comment has been submitted and is awaiting moderation. Thank you! + Your comment has been submitted. Thank you!
)} - + {error && (
{error}
)} - +
@@ -96,7 +125,7 @@ const CommentSection: React.FC = ({ postId }) => { />
- +
- +
= ({ postId }) => { Save my name and email for the next time I comment
- +
- - {/* Sample comments - in a real implementation, these would be fetched from an API */} +
-

Comments (2)

- +

Comments ({comments.length})

+
-
-
-
-

John Calvin

-

March 10, 2025

+ {comments.map((comment) => ( +
+
+
+

{comment.name}

+

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

+
+

{comment.comment}

-

- A profound meditation on the doctrine of grace. I particularly appreciate your emphasis on how this truth transforms our daily lives, not just our theological understanding. -

-
- -
-
-
-

Martin Luther

-

March 5, 2025

-
-
-

- This post eloquently articulates what I've been trying to explain to my congregation. The way you connected Scripture with practical application was masterful. I'll be sharing this widely. -

-
+ ))}