From 46ceb9641928853ae640fb0d187e3677d48005ba Mon Sep 17 00:00:00 2001 From: Rykkel <220993G@mymail.nyp.edu.sg> Date: Thu, 1 Aug 2024 03:02:09 +0800 Subject: [PATCH] Retrieve post comments --- client/src/components/CommentInputModule.tsx | 86 +++++++++ client/src/components/CommentsModule.tsx | 181 +++++++++++-------- client/src/icons.tsx | 27 ++- client/src/pages/CommunityPage.tsx | 1 - client/src/pages/PostPage.tsx | 15 +- server/routes/post.js | 68 ++++--- 6 files changed, 258 insertions(+), 120 deletions(-) create mode 100644 client/src/components/CommentInputModule.tsx diff --git a/client/src/components/CommentInputModule.tsx b/client/src/components/CommentInputModule.tsx new file mode 100644 index 0000000..14235af --- /dev/null +++ b/client/src/components/CommentInputModule.tsx @@ -0,0 +1,86 @@ +import { Formik, Form, FormikHelpers } from "formik"; +import { useEffect, useState } from "react"; +import config from '../config'; +import * as Yup from "yup"; +import NextUIFormikTextarea from "./NextUIFormikTextarea"; +import instance from '../security/http'; +import { useNavigate, useParams } from "react-router-dom"; +import { retrieveUserInformation } from "../security/users"; +import { Button } from "@nextui-org/react"; +import { PaperAirplaneIcon } from "../icons"; + + +const validationSchema = Yup.object({ + content: Yup.string() + .trim() + .min(3, "Content must be at least 3 characters") + .max(500, "Content must be at most 500 characters") +}); + +export default function CommentInputModule() { + const navigate = useNavigate(); + const [userId, setUserId] = useState(null); + const { id } = useParams(); // Retrieve 'id' from the route + + const initialValues = { + content: "", + }; + + const postId = id; + + useEffect(() => { + const getUserInformation = async () => { + try { + const user = await retrieveUserInformation(); // Get the user ID + setUserId(user.id); // Set the user ID in the state + } catch (error) { + console.error(error); + } + }; + getUserInformation(); + }, []); + + const submitComment = async (values: any, { resetForm }: FormikHelpers<{ content: string }>) => { + const response = await instance.post(config.serverAddress + `/post/${postId}/comments`, + { ...values, userId, postId } + ); + console.log("Comment created succesfully", response.data); + resetForm(); // Reset the form after successful submission + }; + + return ( +