@@ -97,6 +97,36 @@ const Community = ({navigation}) => {
97
97
}
98
98
} ;
99
99
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
+ } ;
100
130
101
131
useFocusEffect (
102
132
React . useCallback ( ( ) => {
@@ -172,8 +202,8 @@ const Community = ({navigation}) => {
172
202
</ View >
173
203
174
204
< 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 >
177
207
</ TouchableOpacity >
178
208
< TouchableOpacity
179
209
style = { styles . actionButton }
@@ -222,13 +252,13 @@ const Community = ({navigation}) => {
222
252
/>
223
253
< View style = { styles . modalButtons } >
224
254
< TouchableOpacity style = { styles . submitButton } onPress = { handleAddComment } >
225
- < Text style = { styles . buttonText } > Submit</ Text >
255
+ < Text style = { styles . commentButtonText } > Submit</ Text >
226
256
</ TouchableOpacity >
227
257
< TouchableOpacity
228
258
style = { styles . cancelButton }
229
259
onPress = { ( ) => setShowCommentInput ( false ) }
230
260
>
231
- < Text style = { styles . buttonText } > Cancel</ Text >
261
+ < Text style = { styles . commentButtonText } > Cancel</ Text >
232
262
</ TouchableOpacity >
233
263
</ View >
234
264
</ View >
@@ -320,7 +350,12 @@ const styles = StyleSheet.create({
320
350
alignItems : 'center' ,
321
351
} ,
322
352
actionButton : {
323
- backgroundColor : '#f0f0f0' ,
353
+ backgroundColor : '#e0f7fa' ,
354
+ padding : 10 ,
355
+ borderRadius : 5 ,
356
+ } ,
357
+ likedButton : {
358
+ backgroundColor : '#007BFF' ,
324
359
padding : 10 ,
325
360
borderRadius : 5 ,
326
361
} ,
@@ -373,6 +408,18 @@ const styles = StyleSheet.create({
373
408
marginLeft : 5 ,
374
409
} ,
375
410
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 : {
376
423
color : '#ffffff' ,
377
424
fontSize : 16 ,
378
425
fontWeight : 'bold' ,
0 commit comments