Skip to content

Commit aa0cece

Browse files
committed
revalidate social points on add/delete post/comment
1 parent 6408916 commit aa0cece

5 files changed

Lines changed: 68 additions & 45 deletions

File tree

‎apps/codebility/app/home/feeds/_components/Feed.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect, useState } from "react";
44
import DefaultPagination from "@/components/ui/pagination";
5-
import { useFeedsStore } from "@/store/feeds-store"; // Adjust path as needed
5+
import { useFeedsStore } from "@/store/feeds-store";
66

77
import Post from "./PostCard";
88

@@ -24,7 +24,7 @@ export default function Feed({ isAdmin }: FeedProp) {
2424

2525
useEffect(() => {
2626
fetchPosts();
27-
}, [fetchPosts]);
27+
}, []);
2828

2929
const handleDeletePost = (deletedPostId: string | number) => {
3030
useFeedsStore.setState((state) => ({

‎apps/codebility/app/home/feeds/_components/PostViewCommentItem.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useState } from "react";
44
import Image from "next/image";
5+
import { useFeedsStore } from "@/store/feeds-store";
56
import { X } from "lucide-react";
67

78
import { deletePostComment } from "../_services/action";
@@ -21,15 +22,16 @@ export default function PostViewCommentItem({
2122
content,
2223
userCanDelete,
2324
}: PostViewCommentItemProps) {
25+
const fetchPosts = useFeedsStore((state) => state.fetchPosts);
2426
const [isVisible, setIsVisible] = useState(true);
2527

2628
const handleDelete = async () => {
2729
setIsVisible(false);
2830
try {
2931
await deletePostComment(commenntId);
32+
fetchPosts();
3033
} catch (error) {
3134
setIsVisible(true);
32-
// Optionally, you could log the error or show a message to the user
3335
}
3436
};
3537

‎apps/codebility/app/home/feeds/_components/PostViewCreateComment.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useState } from "react";
44
import Image from "next/image";
55
import { defaultAvatar } from "@/public/assets/images";
66
import { useUserStore } from "@/store/codev-store";
7+
import { useFeedsStore } from "@/store/feeds-store";
78

89
import { createComment } from "../_services/action";
910

@@ -18,6 +19,7 @@ export default function PostViewCreateComment({
1819
}: CreateCommentProps) {
1920
const [comment, setComment] = useState("");
2021
const [isSubmitting, setIsSubmitting] = useState(false);
22+
const fetchPosts = useFeedsStore((state) => state.fetchPosts);
2123
const { user } = useUserStore();
2224

2325
const handleKeyDown = async (e: React.KeyboardEvent<HTMLInputElement>) => {
@@ -28,6 +30,7 @@ export default function PostViewCreateComment({
2830
await createComment(postId, user.id, comment);
2931
setComment("");
3032
onCommentCreated?.();
33+
fetchPosts();
3134
} finally {
3235
setIsSubmitting(false);
3336
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
import { useUserStore } from "@/store/codev-store";
5+
import { useFeedsStore } from "@/store/feeds-store";
6+
import { MessageSquareText } from "lucide-react";
7+
import { shallow } from "zustand/shallow";
8+
9+
import { getSocialPoints } from "../_services/action";
10+
11+
export default function SocialPointsCard() {
12+
const posts = useFeedsStore((state) => state.posts, shallow);
13+
const { user } = useUserStore();
14+
const [points, setPoints] = useState(0);
15+
16+
useEffect(() => {
17+
const fetchSocialPoints = async () => {
18+
if (!user) return;
19+
const fetchedPoints = await getSocialPoints(user.id);
20+
setPoints(fetchedPoints || 0);
21+
};
22+
23+
if (user) fetchSocialPoints();
24+
}, [user, posts]);
25+
26+
return (
27+
<div className="dark:bg-white/3 relative mt-4 w-full rounded-xl border border-white/10 bg-gray-800 p-4 text-white shadow-lg backdrop-blur-sm transition-all duration-300 hover:scale-[1.02] hover:bg-white/10 hover:shadow-lg dark:border-white/5 xl:fixed xl:right-6 xl:top-20 xl:z-50 xl:mt-0 xl:w-64">
28+
<div className="relative">
29+
<div className="mb-3 flex items-center justify-between">
30+
<div className="flex items-center gap-2">
31+
<div className="rounded-lg bg-gradient-to-br from-blue-500 to-cyan-500 p-2 text-white">
32+
<MessageSquareText className="h-6 w-6" />
33+
</div>
34+
<div>
35+
<p className="text-sm font-medium text-gray-300">Social Points</p>
36+
<p className="text-xs text-gray-500">Feed Engagement</p>
37+
</div>
38+
</div>
39+
<div className="text-right">
40+
<p className="bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-2xl font-bold text-transparent">
41+
{points}
42+
</p>
43+
<p className="text-xs text-gray-500">points</p>
44+
</div>
45+
</div>
46+
<div className="space-y-1">
47+
<p className="text-xs text-gray-500 dark:text-gray-400">
48+
Earn more points by posting and getting likes and comments!
49+
</p>
50+
</div>
51+
</div>
52+
</div>
53+
);
54+
}

‎apps/codebility/app/home/feeds/page.tsx‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { useEffect, useState } from "react";
44
import CreatePostModal from "@/components/modals/CreatePostModal";
55
import { Button } from "@/components/ui/button";
66
import { useUserStore } from "@/store/codev-store";
7-
import { MessageSquareText } from "lucide-react";
87

98
import Feed from "./_components/Feed";
10-
import { getSocialPoints, getUserRole } from "./_services/action";
9+
import SocialPointsCard from "./_components/SocialPointsCard";
10+
import { getUserRole } from "./_services/action";
1111

1212
export default function FeedsPage() {
1313
const [isAdmin, setIsAdmin] = useState(true);
1414
const [isModalOpen, setIsModalOpen] = useState(false);
15-
const [socialPoints, setSocialPoints] = useState(0);
1615
const { user } = useUserStore();
1716

1817
useEffect(() => {
@@ -22,16 +21,7 @@ export default function FeedsPage() {
2221
setIsAdmin(role === "Admin");
2322
};
2423

25-
const fetchSocialPoints = async () => {
26-
if (!user) return;
27-
const socialPoints = await getSocialPoints(user.id);
28-
setSocialPoints(socialPoints || 0);
29-
};
30-
31-
if (user) {
32-
fetchRole();
33-
fetchSocialPoints();
34-
}
24+
if (user) fetchRole();
3525
}, [user]);
3626

3727
const openModal = () => setIsModalOpen(true);
@@ -47,35 +37,9 @@ export default function FeedsPage() {
4737
Codebility community.
4838
</p>
4939
</div>
50-
{/* Social Points Card*/}
51-
<div className="dark:bg-white/3 relative mt-4 w-full rounded-xl border border-white/10 bg-gray-800 p-4 text-white shadow-lg backdrop-blur-sm transition-all duration-300 hover:scale-[1.02] hover:bg-white/10 hover:shadow-lg dark:border-white/5 xl:fixed xl:right-6 xl:top-20 xl:z-50 xl:mt-0 xl:w-64">
52-
<div className="relative">
53-
<div className="mb-3 flex items-center justify-between">
54-
<div className="flex items-center gap-2">
55-
<div className="rounded-lg bg-gradient-to-br from-blue-500 to-cyan-500 p-2 text-white">
56-
<MessageSquareText className="h-6 w-6" />
57-
</div>
58-
<div>
59-
<p className="text-sm font-medium text-gray-300">
60-
Social Points
61-
</p>
62-
<p className="text-xs text-gray-500">Feed Engagement</p>
63-
</div>
64-
</div>
65-
<div className="text-right">
66-
<p className="bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-2xl font-bold text-transparent">
67-
{socialPoints}
68-
</p>
69-
<p className="text-xs text-gray-500">points</p>
70-
</div>
71-
</div>
72-
<div className="space-y-1">
73-
<p className="text-xs text-gray-500 dark:text-gray-400">
74-
Earn more points by posting and getting likes and comments!
75-
</p>
76-
</div>
77-
</div>
78-
</div>
40+
41+
{/* Social Points Card */}
42+
<SocialPointsCard />
7943

8044
<Button className="mb-4 mt-4 w-auto" onClick={openModal}>
8145
Create Post

0 commit comments

Comments
 (0)