From b7f3f31fb9f00b5445c3f5b98c454934927c1dc7 Mon Sep 17 00:00:00 2001 From: Mallepally Lokeshwar Reddy Date: Sat, 15 Jun 2024 13:56:44 +0530 Subject: [PATCH 1/3] chore: Replace useState with useEffect in Comment component --- src/components/TutorialPage/components/Commnets/Comment.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/TutorialPage/components/Commnets/Comment.jsx b/src/components/TutorialPage/components/Commnets/Comment.jsx index 09e486fc..c742ae16 100644 --- a/src/components/TutorialPage/components/Commnets/Comment.jsx +++ b/src/components/TutorialPage/components/Commnets/Comment.jsx @@ -60,7 +60,7 @@ const Comment = ({ id }) => { const firestore = useFirestore(); const firebase = useFirebase(); const dispatch = useDispatch(); - useState(() => { + useEffect(() => { getCommentData(id)(firebase, firestore, dispatch); }, [id]); @@ -165,7 +165,7 @@ const Comment = ({ id }) => {
{replies?.replies.map((id, index) => { - return ; + return ; })}
)} From 50ecf92ab8a8e0fe8a904929aa9bf5676d876cbd Mon Sep 17 00:00:00 2001 From: Mallepally Lokeshwar Reddy Date: Sat, 15 Jun 2024 14:00:18 +0530 Subject: [PATCH 2/3] chore: Refactor Textbox component to handle comment submission on Enter key press --- .../TutorialPage/components/Commnets/Textbox.jsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/components/TutorialPage/components/Commnets/Textbox.jsx b/src/components/TutorialPage/components/Commnets/Textbox.jsx index 891d9c00..998011da 100644 --- a/src/components/TutorialPage/components/Commnets/Textbox.jsx +++ b/src/components/TutorialPage/components/Commnets/Textbox.jsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import React, { useState } from "react"; import { Box, TextField, @@ -40,6 +40,13 @@ const Textbox = ({ type, handleSubmit }) => { fullWidth value={commentText} onChange={e => setCommentText(e.target.value)} + onKeyDown={e => { + if (e.key === "Enter") { + e.preventDefault(); + handleSubmit(commentText); + setCommentText(""); + } + }} InputProps={{ endAdornment: ( @@ -70,7 +77,10 @@ const Textbox = ({ type, handleSubmit }) => { From 0336425a289d8ee27251ef9c2a93d2a8772e2681 Mon Sep 17 00:00:00 2001 From: Mallepally Lokeshwar Reddy Date: Sat, 15 Jun 2024 14:17:29 +0530 Subject: [PATCH 3/3] refactor(CommentBox): Improvements in handling comment submission --- .../components/Commnets/CommentBox.jsx | 29 ++++---------- src/components/TutorialPage/index.jsx | 35 ++++++++++++++++- src/store/actions/tutorialPageActions.js | 38 +++++++++++-------- 3 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/components/TutorialPage/components/Commnets/CommentBox.jsx b/src/components/TutorialPage/components/Commnets/CommentBox.jsx index 02f462f9..87b35387 100644 --- a/src/components/TutorialPage/components/Commnets/CommentBox.jsx +++ b/src/components/TutorialPage/components/Commnets/CommentBox.jsx @@ -3,9 +3,7 @@ import { makeStyles } from "@mui/styles"; import React, { useEffect, useState } from "react"; import Textbox from "./Textbox"; import Comment from "./Comment"; -import { addComment } from "../../../../store/actions/tutorialPageActions"; -import { useDispatch, useSelector } from "react-redux"; -import { useFirebase, useFirestore } from "react-redux-firebase"; + const useStyles = makeStyles(() => ({ container: { margin: "10px 0", @@ -28,23 +26,10 @@ const useStyles = makeStyles(() => ({ } })); -const CommentBox = ({ commentsArray, tutorialId }) => { +const CommentBox = ({ commentsArray, onAddComment }) => { const classes = useStyles(); - const firestore = useFirestore(); - const firebase = useFirebase(); - const dispatch = useDispatch(); - const [comments, setComments] = useState([]); + const [comments, setComments] = useState(commentsArray); const [currCommentCount, setCurrCommentCount] = useState(3); - const handleSubmit = comment => { - const commentData = { - content: comment, - replyTo: tutorialId, - tutorial_id: tutorialId, - createdAt: firestore.FieldValue.serverTimestamp(), - userId: "codelabzuser" - }; - addComment(commentData)(firebase, firestore, dispatch); - }; useEffect(() => { setComments(commentsArray?.slice(0, currCommentCount)); @@ -65,17 +50,17 @@ const CommentBox = ({ commentsArray, tutorialId }) => { Comments({commentsArray?.length || 0}) - + {comments?.map((id, index) => { return ( - - + + ); })} - {comments?.length != commentsArray?.length && ( + {comments?.length < commentsArray?.length && (