import { type Component, createSignal, For, Show } from "solid-js"; import { formatDateTime } from "@/utils"; import { useAuth } from "@/context/AuthContext"; import type { Comment } from "@/api/blog"; interface CommentSectionProps { comments: Comment[]; commentsCount: number; onAddComment: (content: string) => Promise; onUpdateComment?: (commentId: number, content: string) => Promise; onDeleteComment?: (commentId: number) => Promise; } const CommentSection: Component = (props) => { const { user } = useAuth(); const [newComment, setNewComment] = createSignal(""); const [editingCommentId, setEditingCommentId] = createSignal( null, ); const [editContent, setEditContent] = createSignal(""); const handleSubmit = async (e: Event) => { e.preventDefault(); if (!newComment().trim()) return; await props.onAddComment(newComment()); setNewComment(""); }; const startEdit = (comment: Comment) => { setEditingCommentId(comment.idComment); setEditContent(comment.content); }; const cancelEdit = () => { setEditingCommentId(null); setEditContent(""); }; const saveEdit = async (commentId: number) => { if (props.onUpdateComment && editContent().trim()) { await props.onUpdateComment(commentId, editContent()); setEditingCommentId(null); setEditContent(""); } }; const handleDelete = async (commentId: number) => { if ( props.onDeleteComment && confirm("Are you sure you want to delete this comment?") ) { await props.onDeleteComment(commentId); } }; const isCommentOwner = (comment: Comment) => user()?.userId === comment.patientId; const sortedComments = () => { if (!props.comments) return []; return [...props.comments].sort( (a, b) => new Date(b.dateOfComment).getTime() - new Date(a.dateOfComment).getTime(), ); }; return (

Comments