Skip to content

Commit 44ab42f

Browse files
committed
add useAddSocialPoints hook and apply to post create and upvote
1 parent d4420ac commit 44ab42f

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState } from "react";
22
import { Input } from "@/components/shared/dashboard/input";
33
import { Button } from "@/components/ui/button";
4+
import { useAddSocialPoints } from "@/hooks/useAddSocialPoints";
45
import { useUserStore } from "@/store/codev-store";
56
import { useFeedsStore } from "@/store/feeds-store";
67
import { uploadImage } from "@/utils/uploadImage";
@@ -21,6 +22,7 @@ const CreatePostForm = ({
2122
const [isSubmitting, setIsSubmitting] = useState(false);
2223
const [uploadedImageIds, setUploadedImageIds] = useState<string[]>([]);
2324
const fetchPosts = useFeedsStore((state) => state.fetchPosts);
25+
const { addSocialPoints } = useAddSocialPoints();
2426
const { user } = useUserStore();
2527

2628
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
@@ -65,6 +67,9 @@ const CreatePostForm = ({
6567
// Reset form
6668
form.reset();
6769
setImage(null);
70+
71+
//Add social points
72+
await addSocialPoints(user?.id, "1");
6873
} catch (error) {
6974
console.error("Error creating post:", error);
7075
toast.error("Failed to create post.");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { useEffect, useState } from "react";
4+
import { useAddSocialPoints } from "@/hooks/useAddSocialPoints";
45
import { useUserStore } from "@/store/codev-store";
56
import { ArrowBigUp } from "lucide-react";
67

@@ -17,6 +18,7 @@ interface PostUpvoteProps {
1718

1819
export default function PostUpvote({ post }: PostUpvoteProps) {
1920
const { user } = useUserStore();
21+
const { addSocialPoints } = useAddSocialPoints();
2022

2123
const [isUpvoted, setIsUpvoted] = useState(false);
2224
const [upvotes, setUpvotes] = useState(post.upvoters_id?.length || 0);
@@ -39,7 +41,7 @@ export default function PostUpvote({ post }: PostUpvoteProps) {
3941
checkUpvote();
4042
}, [user?.id, post?.id]);
4143

42-
const handleUpvote = (e: React.MouseEvent) => {
44+
const handleUpvote = async (e: React.MouseEvent) => {
4345
if (user) {
4446
e.stopPropagation();
4547
e.preventDefault();
@@ -52,6 +54,9 @@ export default function PostUpvote({ post }: PostUpvoteProps) {
5254

5355
setIsUpvoted((prev) => !prev);
5456
setUpvotes((prev) => prev + (isUpvoted ? -1 : 1));
57+
58+
//Add social points to author
59+
await addSocialPoints(post?.author_id?.id, "2");
5560
}
5661
};
5762

‎apps/codebility/app/home/feeds/_services/query.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const getPosts = async () => {
1111
.select(`
1212
*,
1313
author_id(
14+
id,
1415
first_name,
1516
last_name,
1617
image_url
@@ -32,6 +33,7 @@ export type PostType = {
3233
title: string;
3334
content: string;
3435
author_id?: {
36+
id: string;
3537
first_name: string;
3638
last_name: string;
3739
image_url?: string;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"use client";
2+
3+
import { createClientClientComponent } from "@/utils/supabase/client";
4+
import { useEffect, useState, useCallback } from "react";
5+
6+
export function useAddSocialPoints() {
7+
const [supabase, setSupabase] = useState<any>(null);
8+
const [loading, setLoading] = useState(false);
9+
const [error, setError] = useState<string | null>(null);
10+
11+
useEffect(() => {
12+
const client = createClientClientComponent();
13+
setSupabase(client);
14+
}, []);
15+
16+
const addSocialPoints = useCallback(
17+
async (codevId: string | undefined, categoryId: string) => {
18+
if (!supabase) return null;
19+
20+
try {
21+
setLoading(true);
22+
setError(null);
23+
24+
// Fetch category to get its points
25+
const { data: category, error: categoryError } = await supabase
26+
.from("social_points_categories")
27+
.select("*")
28+
.eq("id", categoryId)
29+
.single();
30+
31+
if (categoryError) throw categoryError;
32+
if (!category) throw new Error("Category not found");
33+
34+
// Insert social points entry
35+
const { data: socialPoints, error: insertError } = await supabase
36+
.from("social_points")
37+
.insert([
38+
{
39+
codev_id: codevId,
40+
category_id: categoryId,
41+
points: category.points,
42+
},
43+
])
44+
.select()
45+
.single();
46+
47+
if (insertError) throw insertError;
48+
49+
return socialPoints;
50+
} catch (err: any) {
51+
console.error("Error adding social points:", err);
52+
setError(err.message || "Unknown error");
53+
return null;
54+
} finally {
55+
setLoading(false);
56+
}
57+
},
58+
[supabase]
59+
);
60+
61+
return { addSocialPoints, loading, error };
62+
}

0 commit comments

Comments
 (0)