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 ;
})}
)}
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 && (
async (firebase, firestore, dispatch) => {
try {
dispatch({ type: actions.ADD_COMMENT_START });
+
+ const docref = await firestore
+ .collection("cl_comments")
+ .add(comment);
+
await firestore
.collection("cl_comments")
- .add(comment)
- .then(docref => {
- firestore.collection("cl_comments").doc(docref.id).update({
- comment_id: docref.id
- });
- if (comment.replyTo == comment.tutorial_id) {
- firestore
- .collection("tutorials")
- .doc(comment.tutorial_id)
- .update({
- comments: firebase.firestore.FieldValue.arrayUnion(docref.id)
- });
- }
- })
- .then(() => {
- dispatch({ type: actions.ADD_COMMENT_SUCCESS });
+ .doc(docref.id)
+ .update({
+ comment_id: docref.id
});
+
+ if (comment.replyTo === comment.tutorial_id) {
+ await firestore
+ .collection("tutorials")
+ .doc(comment.tutorial_id)
+ .update({
+ comments: firebase.firestore.FieldValue.arrayUnion(docref.id)
+ });
+ }
+
+ dispatch({ type: actions.ADD_COMMENT_SUCCESS });
} catch (e) {
dispatch({ type: actions.ADD_COMMENT_FAILED, payload: e.message });
}