-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWrittenPost.js
173 lines (162 loc) · 3.79 KB
/
WrittenPost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useState, useEffect } from "react";
import {
View,
Text,
StyleSheet,
FlatList,
TouchableOpacity,
Image,
Alert,
} from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
const WrittenPost = ({ navigation }) => {
const [userPosts, setUserPosts] = useState([]);
useEffect(() => {
loadUserPosts();
}, []);
const loadUserPosts = async () => {
try {
const jwtToken = await AsyncStorage.getItem("jwtToken");
if (!jwtToken) {
Alert.alert("알림", "로그인이 필요합니다.");
return;
}
const response = await fetch(
"http://192.168.61.45:8080/api/member/posts",
{
method: "GET",
headers: {
Authorization: `Bearer ${jwtToken}`,
"Content-Type": "application/json",
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
setUserPosts(data);
} catch (error) {
console.error("내가 쓴 글 로딩 에러:", error);
Alert.alert(
"오류",
"내가 쓴 글을 불러오는데 실패했습니다. 네트워크 연결을 확인해주세요."
);
}
};
const renderItem = ({ item }) => (
<TouchableOpacity
style={styles.postItem}
onPress={() => navigation.navigate("PostDetail", { postId: item.id })}
>
{item.image && (
<Image source={{ uri: item.image }} style={styles.postImage} />
)}
<View style={styles.postContent}>
<Text style={styles.postTitle}>{item.title}</Text>
<Text style={styles.postDescription} numberOfLines={2}>
{item.content}
</Text>
<View style={styles.postInfo}>
<Text style={styles.postDate}>{item.date}</Text>
<Text style={styles.postLikes}>좋아요 {item.likes}</Text>
</View>
</View>
</TouchableOpacity>
);
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>내가 쓴 글</Text>
<View style={styles.placeholder} />
</View>
{userPosts.length > 0 ? (
<FlatList
data={userPosts}
renderItem={renderItem}
keyExtractor={(item) => String(item?.id || Math.random())}
contentContainerStyle={styles.listContainer}
/>
) : (
<View style={styles.emptyContainer}>
<Text style={styles.emptyText}>작성한 글이 없습니다.</Text>
</View>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
header: {
flexDirection: "row",
alignItems: "center",
padding: 10,
borderBottomWidth: 1,
borderBottomColor: "#ddd",
},
backButton: {
fontSize: 18,
fontWeight: "bold",
},
headerTitle: {
fontSize: 18,
fontWeight: "bold",
marginLeft: 10,
},
placeholder: {
flex: 1,
},
listContainer: {
padding: 10,
},
postItem: {
flexDirection: "row",
marginBottom: 10,
borderWidth: 1,
borderColor: "#ddd",
borderRadius: 5,
},
postImage: {
width: 100,
height: 100,
borderRadius: 5,
},
postContent: {
flex: 1,
padding: 10,
},
postTitle: {
fontSize: 16,
fontWeight: "bold",
},
postDescription: {
fontSize: 14,
color: "#333",
},
postInfo: {
flexDirection: "row",
justifyContent: "space-between",
marginTop: 5,
},
postDate: {
fontSize: 12,
color: "#666",
},
postLikes: {
fontSize: 12,
color: "#666",
},
emptyContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
emptyText: {
fontSize: 16,
color: "#333",
},
});
export default WrittenPost;