Skip to content

Commit 6f49b78

Browse files
authored
Merge pull request #530 from bounswe/feature/MB-like-functionality
Feature/mb like functionality
2 parents d506472 + e7a2c02 commit 6f49b78

File tree

2 files changed

+235
-79
lines changed

2 files changed

+235
-79
lines changed

mobile/src/pages/Community.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ const Community = ({navigation}) => {
9797
}
9898
};
9999

100+
const postLike = async (postId) => {
101+
const likeURL = `${baseURL}/like`;
102+
try {
103+
const response = await fetch(likeURL, {
104+
method: 'POST',
105+
headers: {
106+
'Content-Type': 'application/json',
107+
Authorization: `Bearer ${accessToken}`,
108+
},
109+
body: JSON.stringify({ post_id: postId }),
110+
});
111+
if (response.ok) {
112+
fetchPosts();
113+
Alert.alert('Liked successfully!');
114+
} else {
115+
console.error(`Failed to like the post: ${response.status}`);
116+
}
117+
} catch (error) {
118+
console.error('Error liking the post:', error);
119+
}
120+
};
121+
122+
const handleLike = (postId) => {
123+
if (!accessToken) {
124+
navigation.navigate('Login&Register');
125+
Alert.alert('Please login to like the post');
126+
return;
127+
}
128+
postLike(postId);
129+
};
100130

101131
useFocusEffect(
102132
React.useCallback(() => {
@@ -172,8 +202,8 @@ const Community = ({navigation}) => {
172202
</View>
173203

174204
<View style={styles.actions}>
175-
<TouchableOpacity style={styles.actionButton}>
176-
<Text>👍 {post.liked_by.length}</Text>
205+
<TouchableOpacity style={post.liked_by.includes(userId) ? styles.likedButton : styles.actionButton} onPress={() => handleLike(post.id)}>
206+
<Text style={post.liked_by.includes(userId) ? styles.likedButtonText : styles.buttonText}>👍 {post.liked_by.length}</Text>
177207
</TouchableOpacity>
178208
<TouchableOpacity
179209
style={styles.actionButton}
@@ -222,13 +252,13 @@ const Community = ({navigation}) => {
222252
/>
223253
<View style={styles.modalButtons}>
224254
<TouchableOpacity style={styles.submitButton} onPress={handleAddComment}>
225-
<Text style={styles.buttonText}>Submit</Text>
255+
<Text style={styles.commentButtonText}>Submit</Text>
226256
</TouchableOpacity>
227257
<TouchableOpacity
228258
style={styles.cancelButton}
229259
onPress={() => setShowCommentInput(false)}
230260
>
231-
<Text style={styles.buttonText}>Cancel</Text>
261+
<Text style={styles.commentButtonText}>Cancel</Text>
232262
</TouchableOpacity>
233263
</View>
234264
</View>
@@ -320,7 +350,12 @@ const styles = StyleSheet.create({
320350
alignItems: 'center',
321351
},
322352
actionButton: {
323-
backgroundColor: '#f0f0f0',
353+
backgroundColor: '#e0f7fa',
354+
padding: 10,
355+
borderRadius: 5,
356+
},
357+
likedButton: {
358+
backgroundColor: '#007BFF',
324359
padding: 10,
325360
borderRadius: 5,
326361
},
@@ -373,6 +408,18 @@ const styles = StyleSheet.create({
373408
marginLeft: 5,
374409
},
375410
buttonText: {
411+
color: '#007BFF',
412+
fontSize: 16,
413+
fontWeight: 'bold',
414+
textAlign: 'center',
415+
},
416+
likedButtonText: {
417+
color: '#ffffff',
418+
fontSize: 16,
419+
fontWeight: 'bold',
420+
textAlign: 'center',
421+
},
422+
commentButtonText: {
376423
color: '#ffffff',
377424
fontSize: 16,
378425
fontWeight: 'bold',

0 commit comments

Comments
 (0)