Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Comment Display Issue and Improve Comment Submission Handling #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/TutorialPage/components/Commnets/Comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const Comment = ({ id }) => {
const firestore = useFirestore();
const firebase = useFirebase();
const dispatch = useDispatch();
useState(() => {
useEffect(() => {
getCommentData(id)(firebase, firestore, dispatch);
}, [id]);

Expand Down Expand Up @@ -165,7 +165,7 @@ const Comment = ({ id }) => {
<div style={{ margin: "10px 0 0 10px" }}>
<Textbox type="reply" handleSubmit={handleSubmit} />
{replies?.replies.map((id, index) => {
return <Comment id={id} />;
return <Comment key={index} id={id} />;
})}
</div>
)}
Expand Down
29 changes: 7 additions & 22 deletions src/components/TutorialPage/components/Commnets/CommentBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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));
Expand All @@ -65,17 +50,17 @@ const CommentBox = ({ commentsArray, tutorialId }) => {
<Typography variant="h5" sx={{ fontWeight: "600" }}>
Comments({commentsArray?.length || 0})
</Typography>
<Textbox handleSubmit={handleSubmit} />
<Textbox handleSubmit={onAddComment} />
<Grid container rowSpacing={2}>
{comments?.map((id, index) => {
return (
<Grid item xs={12}>
<Comment id={id} key={index} />
<Grid item xs={12} key={index}>
<Comment id={id} />
</Grid>
);
})}
<Grid item container justifyContent="center">
{comments?.length != commentsArray?.length && (
{comments?.length < commentsArray?.length && (
<Button
sx={{ textTransform: "none", fontSize: "14px" }}
onClick={increaseCommentCount}
Expand Down
14 changes: 12 additions & 2 deletions src/components/TutorialPage/components/Commnets/Textbox.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import React, { useState } from "react";
import {
Box,
TextField,
Expand Down Expand Up @@ -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: (
<InputAdornment position="end" sx={{ position: "relative" }}>
Expand Down Expand Up @@ -70,7 +77,10 @@ const Textbox = ({ type, handleSubmit }) => {
<Button
variant="contained"
disableElevation
onClick={() => handleSubmit(commentText)}
onClick={() => {
handleSubmit(commentText);
setCommentText("");
}}
>
<Send />
</Button>
Expand Down
35 changes: 33 additions & 2 deletions src/components/TutorialPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import StepsBar from "./StepBar";
import useWindowSize from "../../helpers/customHooks/useWindowSize";
import {
getTutorialData,
getTutorialSteps
getTutorialSteps,
addComment
} from "../../store/actions/tutorialPageActions";
import { getUserProfileData } from "../../store/actions";
import { useDispatch, useSelector } from "react-redux";
Expand All @@ -24,6 +25,7 @@ function TutorialPage({ background = "white", textColor = "black" }) {
const history = useHistory();
const windowSize = useWindowSize();
const [openMenu, setOpen] = useState(false);
const [commentsArray, setCommentsArray] = useState([]);
const toggleSlider = () => {
setOpen(!openMenu);
};
Expand All @@ -42,6 +44,11 @@ function TutorialPage({ background = "white", textColor = "black" }) {
}
}) => data
);

useEffect(() => {
setCommentsArray(tutorial?.comments);
}, [tutorial?.comments]);

const loading = useSelector(
({
tutorialPage: {
Expand Down Expand Up @@ -72,6 +79,27 @@ function TutorialPage({ background = "white", textColor = "black" }) {
history.push("/not-found");
}

const handleAddComment = async comment => {
const commentData = {
content: comment,
replyTo: id,
tutorial_id: id,
createdAt: firestore.FieldValue.serverTimestamp(),
userId: "codelabzuser"
};
const commentId = await addComment(commentData)(
firebase,
firestore,
dispatch
);

if (!commentsArray || commentsArray?.length === 0) {
setCommentsArray([commentId]);
} else {
setCommentsArray(prevComments => [commentId, ...prevComments]);
}
};

return (
<Box
className={classes.wrapper}
Expand Down Expand Up @@ -112,7 +140,10 @@ function TutorialPage({ background = "white", textColor = "black" }) {
>
<PostDetails details={postDetails} />
<Tutorial steps={steps} />
<CommentBox commentsArray={tutorial?.comments} tutorialId={id} />
<CommentBox
commentsArray={commentsArray}
onAddComment={handleAddComment}
/>
</Grid>

<Grid
Expand Down
38 changes: 22 additions & 16 deletions src/store/actions/tutorialPageActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export const getTutorialData =
.doc(tutorialID)
.get();
const tutorial = data.data();
if (tutorial.comments && Array.isArray(tutorial.comments)) {
tutorial.comments.reverse();
}
dispatch({ type: actions.GET_POST_DATA_SUCCESS, payload: tutorial });
} catch (e) {
dispatch({ type: actions.GET_POST_DATA_FAIL });
Expand Down Expand Up @@ -195,25 +198,28 @@ export const getCommentReply =
export const addComment = comment => 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 });
}
Expand Down
Loading