summaryrefslogtreecommitdiffstats
path: root/src/components/Comment/Comment.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-01-15 23:08:25 +0100
committerArmand Philippot <git@armandphilippot.com>2022-01-15 23:28:24 +0100
commit6f46e54e382bf3f535f27ebead18c2bdf4628e49 (patch)
tree3285800f7c08afe25ebcf985bd5f643bdf7fc603 /src/components/Comment/Comment.tsx
parent4d0b13d22be1297c91316d5e52c8fb30ded5c7dd (diff)
chore(comments): handle comment reply
Diffstat (limited to 'src/components/Comment/Comment.tsx')
-rw-r--r--src/components/Comment/Comment.tsx31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx
index 6a4abce..e0a65f3 100644
--- a/src/components/Comment/Comment.tsx
+++ b/src/components/Comment/Comment.tsx
@@ -1,19 +1,29 @@
import { Button } from '@components/Buttons';
+import CommentForm from '@components/CommentForm/CommentForm';
import { t } from '@lingui/macro';
import { Comment as CommentData } from '@ts/types/comments';
import Image from 'next/image';
import Link from 'next/link';
import { useRouter } from 'next/router';
+import { useEffect, useRef, useState } from 'react';
import styles from './Comment.module.scss';
const Comment = ({
+ articleId,
comment,
isNested = false,
}: {
+ articleId: number;
comment: CommentData;
isNested?: boolean;
}) => {
const router = useRouter();
+ const [isReply, setIsReply] = useState<boolean>(false);
+ const firstFieldRef = useRef<HTMLInputElement>(null);
+
+ useEffect(() => {
+ firstFieldRef.current && firstFieldRef.current.focus();
+ });
const getCommentAuthor = () => {
return comment.author.url ? (
@@ -71,14 +81,31 @@ const Comment = ({
></div>
{!isNested && (
<footer className={styles.footer}>
- <Button clickHandler={() => ''}>{t`Reply`}</Button>
+ <Button
+ clickHandler={() => setIsReply((prev) => !prev)}
+ >{t`Reply`}</Button>
</footer>
)}
</article>
+ {isReply && (
+ <CommentForm
+ ref={firstFieldRef}
+ articleId={articleId}
+ parentId={comment.commentId}
+ isReply={isReply}
+ />
+ )}
{comment.replies.length > 0 && (
<ol className={styles.list}>
{comment.replies.map((reply) => {
- return <Comment key={reply.id} comment={reply} isNested={true} />;
+ return (
+ <Comment
+ articleId={articleId}
+ key={reply.id}
+ comment={reply}
+ isNested={true}
+ />
+ );
})}
</ol>
)}