diff --git a/backend/conduit/articles/models.py b/backend/conduit/articles/models.py index 001b9c2..378ebd3 100644 --- a/backend/conduit/articles/models.py +++ b/backend/conduit/articles/models.py @@ -43,6 +43,7 @@ class Comment(Model, SurrogatePK): comment_id = Column(db.Integer, db.ForeignKey('comment.id'), nullable=True) parentComment = relationship('Comment', backref=db.backref('parent', remote_side=[id]), lazy='dynamic') + def __init__(self, article, author, body, comment_id=None, **kwargs): db.Model.__init__(self, author=author, body=body, article=article, **kwargs) diff --git a/components/article/ArticlePreview.tsx b/components/article/ArticlePreview.tsx index 2668532..2e49e48 100644 --- a/components/article/ArticlePreview.tsx +++ b/components/article/ArticlePreview.tsx @@ -28,7 +28,7 @@ const ArticlePreview = ({ article }) => { if (!isLoggedIn) { Router.push(`/user/login`); return; - } + } setPreview({ ...preview, diff --git a/components/comment/CommentInput.tsx b/components/comment/CommentInput.tsx index bffdf89..0301c96 100644 --- a/components/comment/CommentInput.tsx +++ b/components/comment/CommentInput.tsx @@ -10,6 +10,7 @@ import { SERVER_BASE_URL } from "../../lib/utils/constant"; import storage from "../../lib/utils/storage"; const CommentInput = () => { + const { data: currentUser } = useSWR("user", storage); const isLoggedIn = checkLogin(currentUser); const router = useRouter(); diff --git a/components/comment/CommentList.tsx b/components/comment/CommentList.tsx index d3ccdad..2c8b305 100644 --- a/components/comment/CommentList.tsx +++ b/components/comment/CommentList.tsx @@ -2,6 +2,12 @@ import { useRouter } from "next/router"; import React from "react"; import useSWR from "swr"; +import { useState } from 'react'; +import { message } from 'antd'; +import checkLogin from "../../lib/utils/checkLogin"; +import storage from "../../lib/utils/storage"; +import EditorBox from "./EditorBox"; + import CommentInput from "./CommentInput"; import ErrorMessage from "../common/ErrorMessage"; import LoadingSpinner from "../common/LoadingSpinner"; @@ -11,7 +17,29 @@ import { SERVER_BASE_URL } from "../../lib/utils/constant"; import fetcher from "../../lib/utils/fetcher"; import { Comment, Avatar } from 'antd'; + const CommentList = () => { + // clickedComment is the value of the id of the comment that has clicked the Reply To button + var [clickedComment, setClick] = useState( '' ); + + const { data: currentUser } = useSWR("user", storage); + const isLoggedIn = checkLogin(currentUser) + + // After the editor box is submitted, this sets the clickedComment ID back to default (-1) + const setSubmitData = (handleClick) => { + setClick( handleClick ); + } + + const handleClickReplyTo = (comment) => { + + if (isLoggedIn) { + setClick( comment.id ); + } + else { // Not Logged In + message.info("Please log in to reply", 10) + } + } + const router = useRouter(); const { query: { pid }, @@ -32,13 +60,35 @@ const CommentList = () => { ); const { comments } = data; + const recurseComments = (comments) => { - return ( + + return ( + comments.map((comment: CommentType) => ( + Reply to]} + actions= {[ + /* + If the clickedComment has this id, then there is the Editor Box, so hide the Reply To + Else, the clickedComment does NOT have this id, + so show the Reply To option + */ + + clickedComment == comment.id ? + null + : + + handleClickReplyTo(comment) + } + + >Reply to , + + ]} + author={comment.author.username} avatar={ { /> } content={ -

{comment.body}

+
+

+ { comment.body } +

+

+ { + /* + If the clickedComment has this id, then show the Editor Box + */ + clickedComment == comment.id ? + + : + null + } +

+ +
+ } > - {comment.parentComment.comments.length > 0 ? recurseComments(comment.parentComment.comments) : null} + + { + comment.parentComment.comments.length > 0 ? recurseComments(comment.parentComment.comments) : null + } +
+ ))) } @@ -59,6 +134,7 @@ const CommentList = () => {
{recurseComments(comments)} +
); }; diff --git a/components/comment/EditorBox.tsx b/components/comment/EditorBox.tsx new file mode 100644 index 0000000..df754ad --- /dev/null +++ b/components/comment/EditorBox.tsx @@ -0,0 +1,93 @@ +// Based on CommentInput.tsx + +import axios from "axios"; +import { useRouter } from "next/router"; +import React from "react"; +import useSWR, { trigger } from "swr"; + +import checkLogin from "../../lib/utils/checkLogin"; +import { SERVER_BASE_URL } from "../../lib/utils/constant"; +import storage from "../../lib/utils/storage"; + + +import { Form, Button, Input} from 'antd'; +const { TextArea } = Input; + +var clickedSubmit = false; + +const Editor = ( { onChange, onSubmit, submitting, value } ) => ( + <> + +