-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/backend integration comment (#67)
* feat: 커뮤니티 댓글 생성 및 조회 백엔드 연결 * feat: 게시글/댓글 좋아요 생성 백엔드 연결 * refactor: useEffect 대신 useFocusEffect 사용 * fix: communityPage 댓글 및 게시글의 좋아요 optimistic updates 방식으로 변경
- Loading branch information
Showing
8 changed files
with
746 additions
and
428 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,180 @@ | ||
import React from 'react'; | ||
import { View, Text, Image, StyleSheet, TouchableOpacity } from 'react-native'; | ||
import { useNavigation } from '@react-navigation/native'; | ||
import React, { useState } from 'react'; | ||
import { View, Text, StyleSheet, TouchableOpacity, Alert } from 'react-native'; | ||
import axios from 'axios'; | ||
|
||
import { CustomTheme } from '@styles/CustomTheme'; | ||
import { useOnboarding } from 'src/states/OnboardingContext.js'; | ||
|
||
import IconHeart from '@components/community/IconHeart'; | ||
import IconBookmark from '@components/community/IconBookmark'; | ||
import IconComment from '@components/community/IconComment'; | ||
import IconKebabMenu from './IconKebabMenu'; | ||
import IconKebabMenu from '@components/community/IconKebabMenu'; | ||
|
||
const { fontCaption, fontNavi } = CustomTheme; | ||
|
||
const ItemComment = ({ props }) => { | ||
const navigation = useNavigation(); | ||
const ItemComment = ({ props, id }) => { | ||
const date = (date) => { | ||
const datePart = date.split('T')[0]; | ||
const [year, month, day] = datePart.split('-'); | ||
return `${month}/${day}`; | ||
}; | ||
|
||
const { onboardingData } = useOnboarding(); | ||
const [pressHeart, setPressHeart] = useState({}); | ||
const [heartCounts, setHeartCounts] = useState(props.reduce((acc, post) => { | ||
acc[post.commentId] = post.heart; | ||
return acc; | ||
}, {})); | ||
|
||
|
||
const heartCommentAlert = async (commentId) => { | ||
try { | ||
const response = await axios.post('http://192.168.45.135:8080/api/likes', { | ||
type: 'COMMENT', | ||
postId: id, | ||
commentId: commentId, | ||
}, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': `Bearer ${onboardingData.accessToken}`, | ||
} | ||
}); | ||
|
||
console.log('댓글 좋아요 성공'); | ||
setPressHeart(prevState => ({ ...prevState, [commentId]: true })); | ||
setHeartCounts(prevState => ({ | ||
...prevState, | ||
[commentId]: prevState[commentId] + 1, | ||
})); | ||
} catch (error) { | ||
console.error('댓글 좋아요 실패:', error.response ? error.response.data : error.message); | ||
setPressHeart(prevState => ({ ...prevState, [commentId]: false })); | ||
setHeartCounts(prevState => ({ | ||
...prevState, | ||
[commentId]: prevState[commentId] - 1, | ||
})); | ||
} | ||
}; | ||
|
||
const handleHeart = (commentId) => { | ||
Alert.alert( | ||
"", | ||
"이 댓글에 좋아요를 누르시겠습니까?", | ||
[ | ||
{ | ||
text: "취소", | ||
style: "cancel" | ||
}, | ||
{ | ||
text: "확인", | ||
onPress: () => { | ||
setPressHeart(prevState => ({ ...prevState, [commentId]: true })); | ||
setHeartCounts(prevState => ({ | ||
...prevState, | ||
[commentId]: prevState[commentId] + 1, | ||
})); | ||
heartCommentAlert(commentId); | ||
} | ||
} | ||
], | ||
{ cancelable: false } | ||
); | ||
}; | ||
|
||
|
||
return ( | ||
<> | ||
{props.map((post, index) => ( | ||
<View key={index} style={styles.ItemCommunity}> | ||
<View style={styles.containerRow}> | ||
<View> | ||
<Text style={styles.textPostTitle}>{post.title}</Text> | ||
<Text style={styles.textPostContext}>{post.context}</Text> | ||
|
||
<View style={styles.containerTextRow}> | ||
<View style={styles.containerText}> | ||
<IconHeart /> | ||
<Text style={styles.text}>{post.heart}</Text> | ||
</View> | ||
<View style={styles.containerText}> | ||
<IconBookmark /> | ||
<Text style={styles.text}>{post.bookmark}</Text> | ||
</View> | ||
<View style={styles.containerText}> | ||
<Text style={styles.text}>{post.date}</Text> | ||
{props.map((post, index) => ( | ||
<View key={index} style={styles.ItemCommunity}> | ||
<View style={styles.containerRow}> | ||
<View> | ||
<Text style={styles.textPostTitle}>{post.title}</Text> | ||
<Text style={styles.textPostContext}>{post.context}</Text> | ||
|
||
<View style={styles.containerTextRow}> | ||
<TouchableOpacity style={styles.containerText} onPress={() => handleHeart(post.commentId)}> | ||
<IconHeart active={pressHeart[post.commentId]} /> | ||
<Text style={styles.text}>{heartCounts[post.commentId]}</Text> | ||
</TouchableOpacity> | ||
<View style={styles.containerText}> | ||
<IconBookmark /> | ||
<Text style={styles.text}>{post.bookmark}</Text> | ||
</View> | ||
<View style={styles.containerText}> | ||
<Text style={styles.text}>{date(post.date)}</Text> | ||
</View> | ||
</View> | ||
</View> | ||
</View> | ||
|
||
<IconKebabMenu style={styles.iconKebabMenu}/> | ||
<TouchableOpacity style={styles.textTranslation}> | ||
<Text style={styles.textTranslation}>번역하기</Text> | ||
</TouchableOpacity> | ||
<IconKebabMenu style={styles.iconKebabMenu} /> | ||
<TouchableOpacity style={styles.textTranslation}> | ||
<Text style={styles.textTranslation}>번역하기</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</View> | ||
</View> | ||
))} | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
ItemCommunity: { | ||
width: '100%', | ||
minHeight: 78, | ||
backgroundColor: CustomTheme.bgBasic, | ||
borderRadius: 20, | ||
borderWidth: 2, | ||
borderColor: '#D9EAFF', | ||
paddingHorizontal: 20, | ||
paddingVertical: 11, | ||
justifyContent: 'center', | ||
marginTop: 4, | ||
marginBottom: 4, | ||
}, | ||
containerRow: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
justifyContent: 'space-between', | ||
}, | ||
textPostTitle: { | ||
fontSize: 12, | ||
lineHeight: 16, | ||
fontFamily: 'NotoSansCJKkr-Bold', | ||
width: 272, | ||
height: 17, | ||
}, | ||
textPostContext: { | ||
...fontCaption, | ||
width: 288, | ||
marginTop: 3, | ||
}, | ||
iconKebabMenu: { | ||
position: 'absolute', | ||
top: 0, | ||
right: -11, | ||
}, | ||
textTranslation: { | ||
...fontNavi, | ||
color: CustomTheme.primaryMedium, | ||
textDecorationLine: 'underline', | ||
position: 'absolute', | ||
bottom: 0, | ||
right: -2, | ||
}, | ||
containerTextRow: { | ||
flexDirection: 'row', | ||
marginTop: 8, | ||
}, | ||
containerText: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
marginRight: 8, | ||
}, | ||
text: { | ||
...fontNavi, | ||
color: '#8C8D91', | ||
marginLeft: 1, | ||
}, | ||
|
||
ItemCommunity: { | ||
width: '100%', | ||
minHeight: 78, | ||
backgroundColor: CustomTheme.bgBasic, | ||
borderRadius: 20, | ||
borderWidth: 2, | ||
borderColor: '#D9EAFF', | ||
paddingHorizontal: 20, | ||
paddingVertical: 11, | ||
justifyContent: 'center', | ||
marginTop: 4, | ||
marginBottom: 4, | ||
}, | ||
containerRow: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
justifyContent: 'space-between', | ||
}, | ||
textPostTitle: { | ||
fontSize: 12, | ||
lineHeight: 16, | ||
fontFamily: 'NotoSansCJKkr-Bold', | ||
width: 272, | ||
height: 17, | ||
}, | ||
textPostContext: { | ||
...fontCaption, | ||
width: 288, | ||
marginTop: 3, | ||
}, | ||
iconKebabMenu: { | ||
position: 'absolute', | ||
top: 0, | ||
right: -11, | ||
}, | ||
textTranslation: { | ||
...fontNavi, | ||
color: CustomTheme.primaryMedium, | ||
textDecorationLine: 'underline', | ||
position: 'absolute', | ||
bottom: 0, | ||
right: -2, | ||
}, | ||
containerTextRow: { | ||
flexDirection: 'row', | ||
marginTop: 8, | ||
}, | ||
containerText: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
marginRight: 8, | ||
}, | ||
text: { | ||
...fontNavi, | ||
color: '#8C8D91', | ||
marginLeft: 1, | ||
}, | ||
}); | ||
|
||
export default ItemComment; | ||
export default ItemComment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.