Skip to content

Commit 31f9b67

Browse files
committed
feat: frontend implementation
1 parent 9267846 commit 31f9b67

2 files changed

Lines changed: 74 additions & 33 deletions

File tree

backend/projects/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def toggle_like(request, project_id):
7878
Like.objects.create(project=project, user=user)
7979
project.likes_count += 1
8080
project.save()
81-
return Response({'message': 'Project liked successfully'}, status=status.HTTP_200_OK)
81+
return Response({'message': 'Project liked successfully','like_count': project.likes_count}, status=status.HTTP_200_OK)
8282
else:
8383
return Response({'message': 'You already liked this project.'}, status=status.HTTP_400_BAD_REQUEST)
8484

@@ -88,7 +88,7 @@ def toggle_like(request, project_id):
8888
like.delete()
8989
project.likes_count -= 1
9090
project.save()
91-
return Response({'message': 'Project unliked successfully'}, status=status.HTTP_200_OK)
91+
return Response({'message': 'Project unliked successfully','like_count': project.likes_count}, status=status.HTTP_200_OK)
9292
else:
9393
return Response({'message': 'You already unliked this project.'}, status=status.HTTP_400_BAD_REQUEST)
9494

@@ -109,7 +109,7 @@ def toggle_follow(request, project_id):
109109
Follow.objects.create(project=project, user=user)
110110
project.followers_count += 1
111111
project.save()
112-
return Response({'message': 'Project followed successfully'}, status=status.HTTP_200_OK)
112+
return Response({'message': 'Project followed successfully', 'follow_count': project.followers_count}, status=status.HTTP_200_OK)
113113
else:
114114
return Response({'message': 'You already followed this project.'}, status=status.HTTP_400_BAD_REQUEST)
115115

@@ -119,7 +119,7 @@ def toggle_follow(request, project_id):
119119
follow.delete()
120120
project.followers_count -= 1
121121
project.save()
122-
return Response({'message': 'Project unfollowed successfully'}, status=status.HTTP_200_OK)
122+
return Response({'message': 'Project unfollowed successfully', 'follow_count': project.followers_count}, status=status.HTTP_200_OK)
123123
else:
124124
return Response({'message': 'You already unfollowed this project.'}, status=status.HTTP_400_BAD_REQUEST)
125125

frontend/bitmatch/src/views/IndividualProjectPage.jsx

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ const ProjectDetailPage = () => {
103103
const ownerData = await fetchUserByUUID(projectData.owner);
104104
setOwnerData(ownerData);
105105

106+
// Check initial like status
107+
const likeStatusResponse = await fetch(
108+
`${SERVER_HOST}/projects/likes/check/${userData.id}/${id}`
109+
);
110+
if (likeStatusResponse.ok) {
111+
const likeData = await likeStatusResponse.json();
112+
setLiked(likeData.liked);
113+
} else {
114+
console.error("Error checking like status");
115+
}
116+
117+
// Check initial follow status
118+
const followStatusResponse = await fetch(
119+
`${SERVER_HOST}/projects/follows/check/${userData.id}/${id}/`
120+
);
121+
if (followStatusResponse.ok) {
122+
const followData = await followStatusResponse.json();
123+
setFollowing(followData.followed);
124+
} else {
125+
console.error("Error checking follow status");
126+
}
127+
106128
setIsReady(true);
107129
} catch (error) {
108130
setError("Error loading data");
@@ -131,22 +153,49 @@ const ProjectDetailPage = () => {
131153

132154
const handleFollow = async () => {
133155
setFollowing(!following);
134-
console.log("current following status:", following);
135156
const fdata = {
136157
action: following ? "unfollow" : "follow",
137-
user_id: 1,
158+
user_id: userUuid,
138159
};
139-
console.log("id is ", id);
140160

141161
try {
142162
const response = await axios.post(
143-
`${SERVER_HOST}/projects/follow/${id}`,
163+
`${SERVER_HOST}/projects/follow/${id}/`,
144164
fdata
145165
);
146-
console.log("response: ", response.data);
147-
window.location.reload();
166+
if (response.data && response.data.follow_count !== undefined) {
167+
setProject((prevProject) => ({
168+
...prevProject,
169+
followers_count: response.data.follow_count,
170+
}));
171+
}
148172
} catch (error) {
149173
console.error("Error updating follows: ", error);
174+
setFollowing(!following);
175+
}
176+
};
177+
178+
const handleLike = async () => {
179+
setLiked(!likeStatus);
180+
const fdata = {
181+
action: likeStatus ? "unlike" : "like",
182+
user_id: userUuid,
183+
};
184+
185+
try {
186+
const response = await axios.post(
187+
`${SERVER_HOST}/projects/like/${id}/`,
188+
fdata
189+
);
190+
if (response.data && response.data.like_count !== undefined) {
191+
setProject((prevProject) => ({
192+
...prevProject,
193+
likes_count: response.data.like_count,
194+
}));
195+
}
196+
} catch (error) {
197+
console.error("Error updating like status: ", error);
198+
setLiked(!likeStatus);
150199
}
151200
};
152201

@@ -365,25 +414,6 @@ const ProjectDetailPage = () => {
365414
return () => clearTimeout(timer);
366415
}, [cooldown]);
367416

368-
const handleLike = async () => {
369-
setLiked(!likeStatus);
370-
const fdata = {
371-
action: likeStatus ? "unlike" : "like",
372-
user_id: 1,
373-
};
374-
375-
try {
376-
const response = await axios.post(
377-
`${SERVER_HOST}/projects/like/${id}`,
378-
fdata
379-
);
380-
console.log("response: ", response.data);
381-
window.location.reload();
382-
} catch (error) {
383-
console.error("Error updating likes: ", error);
384-
}
385-
};
386-
387417
const handleSave = async (data) => {
388418
const formData = new FormData();
389419

@@ -563,14 +593,25 @@ const ProjectDetailPage = () => {
563593

564594
<div className="flex gap-4 mb-2">
565595
<div className="flex items-center gap-2">
566-
<Button variant="ghost" size="icon" onClick={handleLike}>
567-
<ThumbsUp className="h-6 w-6"></ThumbsUp>
596+
<Button
597+
variant="ghost"
598+
size="icon"
599+
onClick={handleLike}
600+
className={likeStatus ? "text-blue-500" : ""}
601+
>
602+
<ThumbsUp className="h-6 w-6" />
568603
</Button>
569604
<span>{project.likes_count} Likes</span>
570605
</div>
606+
571607
<div className="flex items-center gap-2">
572-
<Button variant="ghost" size="icon" onClick={handleFollow}>
573-
<UserRound className="h-6 w-6"></UserRound>
608+
<Button
609+
variant="ghost"
610+
size="icon"
611+
onClick={handleFollow}
612+
className={following ? "text-blue-500" : ""}
613+
>
614+
<UserRound className="h-6 w-6" />
574615
</Button>
575616
<span>{project.followers_count} Followers</span>
576617
</div>

0 commit comments

Comments
 (0)