Skip to content

Commit 0add6a6

Browse files
committed
3- comment on post mutation added
1 parent 7b90099 commit 0add6a6

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

frontend/src/components/common/Post.jsx

+37-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
99
import { toast } from "react-hot-toast";
1010

1111
import LoadingSpinner from "./LoadingSpinner";
12+
import { formatPostDate } from "../../utils/date";
1213

1314
const Post = ({ post }) => {
1415
const [comment, setComment] = useState("");
1516
const { data: authUser } = useQuery({ queryKey: ["authUser"] });
1617
const queryClient = useQueryClient();
18+
const postOwner = post.user;
19+
const isLiked = post.likes.includes(authUser._id);
20+
21+
const isMyPost = authUser._id === post.user._id;
22+
23+
const formattedDate = formatPostDate(post.createdAt);
1724

1825
const { mutate: deletePost, isPending: isDeleting } = useMutation({
1926
mutationFn: async () => {
@@ -71,21 +78,44 @@ const Post = ({ post }) => {
7178
},
7279
});
7380

74-
const postOwner = post.user;
75-
const isLiked = post.likes.includes(authUser._id);
76-
77-
const isMyPost = authUser._id === post.user._id;
78-
79-
const formattedDate = "1h";
81+
const { mutate: commentPost, isPending: isCommenting } = useMutation({
82+
mutationFn: async () => {
83+
try {
84+
const res = await fetch(`/api/posts/comment/${post._id}`, {
85+
method: "POST",
86+
headers: {
87+
"Content-Type": "application/json",
88+
},
89+
body: JSON.stringify({ text: comment }),
90+
});
91+
const data = await res.json();
8092

81-
const isCommenting = false;
93+
if (!res.ok) {
94+
throw new Error(data.error || "Something went wrong");
95+
}
96+
return data;
97+
} catch (error) {
98+
throw new Error(error);
99+
}
100+
},
101+
onSuccess: () => {
102+
toast.success("Comment posted successfully");
103+
setComment("");
104+
queryClient.invalidateQueries({ queryKey: ["posts"] });
105+
},
106+
onError: (error) => {
107+
toast.error(error.message);
108+
},
109+
});
82110

83111
const handleDeletePost = () => {
84112
deletePost();
85113
};
86114

87115
const handlePostComment = (e) => {
88116
e.preventDefault();
117+
if (isCommenting) return;
118+
commentPost();
89119
};
90120

91121
const handleLikePost = () => {

frontend/src/utils/date/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export const formatPostDate = (createdAt) => {
2+
const currentDate = new Date();
3+
const createdAtDate = new Date(createdAt);
4+
5+
const timeDifferenceInSeconds = Math.floor((currentDate - createdAtDate) / 1000);
6+
const timeDifferenceInMinutes = Math.floor(timeDifferenceInSeconds / 60);
7+
const timeDifferenceInHours = Math.floor(timeDifferenceInMinutes / 60);
8+
const timeDifferenceInDays = Math.floor(timeDifferenceInHours / 24);
9+
10+
if (timeDifferenceInDays > 1) {
11+
return createdAtDate.toLocaleDateString("en-US", { month: "short", day: "numeric" });
12+
} else if (timeDifferenceInDays === 1) {
13+
return "1d";
14+
} else if (timeDifferenceInHours >= 1) {
15+
return `${timeDifferenceInHours}h`;
16+
} else if (timeDifferenceInMinutes >= 1) {
17+
return `${timeDifferenceInMinutes}m`;
18+
} else {
19+
return "Just now";
20+
}
21+
};
22+
23+
export const formatMemberSinceDate = (createdAt) => {
24+
const date = new Date(createdAt);
25+
const months = [
26+
"January",
27+
"February",
28+
"March",
29+
"April",
30+
"May",
31+
"June",
32+
"July",
33+
"August",
34+
"September",
35+
"October",
36+
"November",
37+
"December",
38+
];
39+
const month = months[date.getMonth()];
40+
const year = date.getFullYear();
41+
return `Joined ${month} ${year}`;
42+
};

0 commit comments

Comments
 (0)