diff --git a/frontend/components/answer/AnswerBox.tsx b/frontend/components/answer/AnswerBox.tsx index 0c1edd6..a767054 100644 --- a/frontend/components/answer/AnswerBox.tsx +++ b/frontend/components/answer/AnswerBox.tsx @@ -28,10 +28,10 @@ interface AnswerBoxProps { const AnswerBox = ({ answer }: AnswerBoxProps) => { const theme = useTheme(); const [isExpanded, setIsExpanded] = useState(false); + const { id, text, createdAt, isAnonymous, comments } = answer; const [hearted, setHearted] = useState(false); const [heartCount, setHeartCount] = useState(answer.hearts); const [heartLoading, setHeartLoading] = useState(false); - const { text, createdAt, isAnonymous, comments } = answer; const handleHeart = useCallback(async () => { if (heartLoading) return; @@ -147,7 +147,7 @@ const AnswerBox = ({ answer }: AnswerBoxProps) => { paddingLeft={majorScale(3)} marginTop={majorScale(3)} > - + ); diff --git a/frontend/components/comment/CommentForm.tsx b/frontend/components/comment/CommentForm.tsx new file mode 100644 index 0000000..53f2450 --- /dev/null +++ b/frontend/components/comment/CommentForm.tsx @@ -0,0 +1,113 @@ +'use client'; + +import React, { useState } from 'react'; + +import { Button, Checkbox, Pane, Textarea, useTheme } from 'evergreen-ui'; + +import { createNewComment } from '@/api/commentService'; + +interface CommentFormProps { + answerId: string; +} + +/** + * Renders an "Add Comment" button that expands into a form + * with a textarea, anonymous toggle, and submit/cancel actions. + */ +export function CommentForm({ answerId }: CommentFormProps) { + const theme = useTheme(); + const [isOpen, setIsOpen] = useState(false); + const [text, setText] = useState(''); + const [isAnonymous, setIsAnonymous] = useState(false); + const [isSubmitting, setIsSubmitting] = useState(false); + + const handleSubmit = async () => { + if (!text.trim() || isSubmitting) return; + + setIsSubmitting(true); + const result = await createNewComment(answerId, { + text: text.trim(), + is_anonymous: isAnonymous, + }); + setIsSubmitting(false); + + if (result) { + setText(''); + setIsAnonymous(false); + setIsOpen(false); + // TODO: refresh comment list in AnswerBox after successful submission + } + // TODO: handle error for failed submissions + }; + + const handleCancel = () => { + setText(''); + setIsAnonymous(false); + setIsOpen(false); + }; + + // If the form is not open, show the "Add Comment" button + if (!isOpen) { + return ( + + ); + } + + // If the form is open, show the comment form + return ( + + {/* Comment text input */} +