Skip to content

Commit

Permalink
Fix the issue while deleting the comments. (#1276)
Browse files Browse the repository at this point in the history
* Fix the issue while deleting the comments.

* fix: style and error handling

---------

Co-authored-by: Narayan soni <[email protected]>
  • Loading branch information
Jai-Marothiya and narayan954 authored Oct 16, 2023
1 parent a75c045 commit 1fb368f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
41 changes: 28 additions & 13 deletions src/components/Post/CommentDialogBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import DeleteTwoToneIcon from "@mui/icons-material/DeleteTwoTone";
import { Link } from "react-router-dom";
import ReadMore from "../ReadMore";
import { deleteComment } from "../../js/postFn";
import { useSnackbar } from "notistack";

const CommentDialogBox = ({ postId, comments, user, fullScreen }) => {
const { enqueueSnackbar } = useSnackbar();

const { isAnonymous } = user;
const [username, setUsername] = useState("");
const [openToDeleteComment, setOpenToDeleteComment] = useState(false);
const [selectedCommentId, setSelectedCommentId] = useState(null);

useEffect(() => {
async function getUsername() {
Expand All @@ -42,6 +46,22 @@ const CommentDialogBox = ({ postId, comments, user, fullScreen }) => {
}
}, []);

const handleDelete = (postId, commentid) => {
deleteComment(postId, commentid, enqueueSnackbar);
};

/****************************************** */
const handleDeleteIconClick = (commentId) => {
setSelectedCommentId(commentId);
setOpenToDeleteComment(true);
};

// Function to clear the selected comment ID when the delete confirmation dialog is closed
const handleCloseDeleteDialog = () => {
setSelectedCommentId(null);
setOpenToDeleteComment(false);
};

return (
<Box
sx={{
Expand Down Expand Up @@ -87,15 +107,12 @@ const CommentDialogBox = ({ postId, comments, user, fullScreen }) => {
</ReadMore>
</p>
</div>
<div
onClick={() => {
setOpenToDeleteComment(!openToDeleteComment);
}}
>
<div>
{user && userComment?.content?.username == username && (
<DeleteTwoToneIcon
fontSize="small"
className="comment-delete-icon"
onClick={() => handleDeleteIconClick(userComment.id)}
/>
)}
{
Expand All @@ -106,21 +123,19 @@ const CommentDialogBox = ({ postId, comments, user, fullScreen }) => {
aria-labelledby="responsive-dialog-title"
>
<DialogTitle id="responsive-dialog-title">
{"Delete Comment?"}
Delete Comment?
</DialogTitle>
<DialogContent>
<DialogContentText>
Are you sure you want to delete this Comment?
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setOpenToDeleteComment(false)}>
Cancel
</Button>
<DialogActions onClick={handleCloseDeleteDialog}>
<Button>Cancel</Button>
<Button
onClick={(event) =>
deleteComment(event, postId, userComment.id)
}
onClick={() => {
handleDelete(postId, selectedCommentId);
}}
>
Delete
</Button>
Expand Down
31 changes: 20 additions & 11 deletions src/js/postFn.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import firebase from "firebase/compat/app";

import { db, storage } from "../lib/firebase";

import { playErrorSound, playSuccessSound } from "./sounds";

import firebase from "firebase/compat/app";

export const deletePost = async (
uid,
postId,
Expand Down Expand Up @@ -59,12 +58,22 @@ export const savePost = async (postId) => {
return JSON.parse(localStorage.getItem("posts"));
};

export const deleteComment = async (event, postId, commentId) => {
event.preventDefault();
await db
.collection("posts")
.doc(postId)
.collection("comments")
.doc(commentId)
.delete();
export const deleteComment = async (postId, commentId, enqueueSnackbar) => {
try {
await db
.collection("posts")
.doc(postId)
.collection("comments")
.doc(commentId)
.delete()
.then(() => {
playSuccessSound();
enqueueSnackbar("Comment deleted successfully!", {
variant: "success",
});
});
} catch (error) {
playErrorSound();
enqueueSnackbar(`Error deleting comment: ${error}`, { variant: "error" });
}
};

0 comments on commit 1fb368f

Please sign in to comment.