-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatNewScreen.js
169 lines (156 loc) · 3.86 KB
/
ChatNewScreen.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
import React, { useState } from "react";
import {
View,
Text,
TextInput,
TouchableOpacity,
StyleSheet,
Alert,
} from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
const ChatNewScreen = ({ navigation }) => {
const [title, setTitle] = useState("");
const [tag, setTag] = useState("");
const handleCreateChat = async () => {
if (!title.trim()) {
Alert.alert("알림", "제목을 입력해주세요.");
return;
}
if (!tag) {
Alert.alert("알림", "태그를 선택해주세요.");
return;
}
try {
const newChat = {
id: Date.now().toString(),
title: title.trim(),
tag,
createdAt: new Date().toISOString(),
};
const existingChats = await AsyncStorage.getItem("chats");
const chats = existingChats ? JSON.parse(existingChats) : [];
const updatedChats = [...chats, newChat];
await AsyncStorage.setItem("chats", JSON.stringify(updatedChats));
Alert.alert("성공", "채팅방이 생성되었습니다.", [
{
text: "확인",
onPress: () => navigation.goBack(),
},
]);
} catch (error) {
Alert.alert("오류", "채팅방 생성에 실패했습니다.");
console.error("채팅방 생성 오류:", error);
}
};
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>새 채팅방 만들기</Text>
</View>
<View style={styles.form}>
<Text style={styles.label}>제목</Text>
<TextInput
style={styles.input}
value={title}
onChangeText={setTitle}
placeholder="채팅방 제목을 입력하세요"
/>
<Text style={styles.label}>태그 선택</Text>
<View style={styles.tagContainer}>
{["#돌봄지원", "#돌봄받기", "#나눔받기", "#나눔하기"].map(
(tagOption) => (
<TouchableOpacity
key={tagOption}
style={[
styles.tagButton,
tag === tagOption && styles.selectedTagButton,
]}
onPress={() => setTag(tagOption)}
>
<Text
style={[
styles.tagText,
tag === tagOption && styles.selectedTagText,
]}
>
{tagOption}
</Text>
</TouchableOpacity>
)
)}
</View>
<TouchableOpacity
style={styles.createButton}
onPress={handleCreateChat}
>
<Text style={styles.createButtonText}>채팅방 만들기</Text>
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
header: {
padding: 16,
marginTop: 20,
},
headerTitle: {
fontSize: 24,
fontWeight: "bold",
},
form: {
padding: 16,
},
label: {
fontSize: 16,
fontWeight: "bold",
marginBottom: 8,
},
input: {
borderWidth: 1,
borderColor: "#ddd",
borderRadius: 8,
padding: 12,
marginBottom: 20,
},
tagContainer: {
flexDirection: "row",
flexWrap: "wrap",
gap: 8,
marginBottom: 30,
},
tagButton: {
paddingHorizontal: 16,
paddingVertical: 8,
borderRadius: 20,
backgroundColor: "white",
borderWidth: 1,
borderColor: "#ddd",
},
selectedTagButton: {
backgroundColor: "black",
},
tagText: {
fontSize: 14,
color: "black",
},
selectedTagText: {
color: "white",
},
createButton: {
backgroundColor: "#FFEDAE",
padding: 16,
borderRadius: 8,
alignItems: "center",
},
createButtonText: {
fontSize: 16,
fontWeight: "bold",
color: "black",
},
});
export default ChatNewScreen;