-
Notifications
You must be signed in to change notification settings - Fork 3
/
StampList.tsx
355 lines (330 loc) · 10.3 KB
/
StampList.tsx
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React, {useEffect, useState} from 'react';
import { View, Text, Modal, StyleSheet, Image, TouchableOpacity, ScrollView, KeyboardAvoidingView, Platform, TextInput } from 'react-native';
import { RadioButton } from 'react-native-paper';
import realm, { ICustomStamp, createCustomStamp, deleteCustomStamp, getAllCustomStamps } from './src/localDB/document';
import * as amplitude from './AmplitudeAPI';
const StampList = ({visible, closeModal}) => {
// 각 스탬프의 상태를 관리하는 배열, 모두 기본값은 false로 초기화
const [checkedStates, setCheckedStates] = useState(
Array(20).fill(false)
);
const [customStamps, setCustomStamps] = useState<ICustomStamp[]>([]);
useEffect(() => {
const fetchStamps = async () => {
const fetchedCustomStamps = await getAllCustomStamps();
setCustomStamps(fetchedCustomStamps);
};
fetchStamps();
}, []);
const [addStampDataLabel, setAddStampDataLabel] = useState('');
const [addStampDataEmotion, setAddStampDataEmotion] = useState('');
const [isChecked, setIsChecked] = useState(false);
const [stampCount, setStampCount] = useState(0);
const [addStampModalVisible, setAddStampModalVisible] = useState(false);
const [addStampButtonDisabled, setAddStampButtonDisabled] = useState(true);
const handleRadioButtonPress = (index) => {
amplitude.choiceDeleteCustomStampCandidate();
const newCheckedStates = [...checkedStates];
newCheckedStates[index] = !checkedStates[index];
const count = newCheckedStates.filter((state) => state).length;
setCheckedStates(newCheckedStates);
setIsChecked(count > 0);
setStampCount(count);
};
const handleDeleteStamp = () => {
amplitude.deleteCustomStamp();
// 라디오버튼 체크된 것들 삭제
const selectedIndexes = checkedStates.reduce(
(indexes, state, index) => (state ? [...indexes, index] : indexes),
[]
);
// 선택된 스탬프들을 삭제
const newCustomStamps = customStamps.filter(
(stamp, index) => !selectedIndexes.includes(index)
);
// realm에서 선택된 스탬프들을 삭제
selectedIndexes.forEach(index => {
deleteCustomStamp(customStamps[index]);
});
// 선택된 스탬프들의 체크 상태 초기화
const newCheckedStates = checkedStates.map((_, index) =>
selectedIndexes.includes(index) ? false : checkedStates[index]
);
// 변경된 데이터와 상태 적용
setCustomStamps(newCustomStamps);
setCheckedStates(newCheckedStates);
console.log("스탬프 삭제");
};
const handleAddStamp = (label, emotion) => {
amplitude.submitAddCustomStamp(label);
// 새 스탬프 객체의 초기 데이터를 생성
const newStampData = {
stampName: label,
emoji: emotion,
};
let newStamp;
// Realm 데이터베이스에 스탬프 추가
realm.write(() => {
newStamp = createCustomStamp(newStampData);
});
// 상태 업데이트: 새 스탬프를 customStamps에 추가
const updatedCustomStamps = [...customStamps, newStamp];
setCustomStamps(updatedCustomStamps);
// 모달과 버튼 상태 초기화
setAddStampModalVisible(false);
setAddStampButtonDisabled(true);
console.log("스탬프 추가");
};
return (
<Modal visible={visible} animationType='slide' transparent>
<View style={styles.fixModalContainer}>
<View style={styles.fixModalTitleContainer}>
<View style={styles.fixModalTitleContent}>
<TouchableOpacity onPress={closeModal}>
<Image source={require('./assets/arrow-back.png')} />
</TouchableOpacity>
<Text style={styles.fixModalTitle}>스탬프 설정</Text>
</View>
<TouchableOpacity onPress={() => {
amplitude.tryAddCustomStamp();
setAddStampModalVisible(true);
}}>
<Image source={require('./assets/add.png')} />
</TouchableOpacity>
</View>
<View style={styles.fixModalMessageContainer}>
<Text style={styles.fixModalMessage}>감정스탬프를 삭제할 수 있다무🥬</Text>
</View>
<ScrollView style={styles.stampList}>
{customStamps.map((stamp, index) => (
<View key={stamp.id} style={styles.stampListContainer}>
<RadioButton
value="first"
status={checkedStates[index] ? 'checked' : 'unchecked'}
onPress={() => handleRadioButtonPress(index)}
/>
<TouchableOpacity key={stamp.id} style={styles.moodInfo}>
<Text style={styles.moodEmotion}>{stamp.emoji}</Text>
<Text style={styles.moodText}>{stamp.stampName}</Text>
</TouchableOpacity>
</View>
))}
</ScrollView>
{ isChecked &&
<TouchableOpacity style={styles.fixModalButton} onPress={handleDeleteStamp}>
<Text style={styles.fixModalButtonText}>스탬프 삭제</Text>
</TouchableOpacity>
}
</View>
{addStampModalVisible && (
<View style={styles.overlay} />
)}
<Modal visible={addStampModalVisible} animationType='slide' transparent>
<View style={styles.addStampModalContainer}>
<View style={styles.addStampModalTitleContainer}>
<TouchableOpacity onPress={() => {
amplitude.cancelAddCustomStamp();
setAddStampModalVisible(false);
}}>
<Image source={require('./assets/close.png')} />
</TouchableOpacity>
<Text style={styles.addStampModalTitle}>스탬프 추가</Text>
<TouchableOpacity disabled={addStampButtonDisabled} onPress={() => handleAddStamp(addStampDataLabel, addStampDataEmotion)}>
<Image source={require('./assets/add_check.png')}
style={[
styles.checkImage,
addStampButtonDisabled && styles.disabledCheckImage
]}
/>
</TouchableOpacity>
</View>
<View style={styles.addStampModalContent}>
<View style={styles.addStampModalEmotionBox}>
<TextInput
style={styles.addStampModalEmotion}
placeholder='🔥'
maxLength={2}
onChangeText={(text) => {
setAddStampDataEmotion(text);
if(text.length > 0 && addStampDataLabel.length > 0) setAddStampButtonDisabled(false);
else setAddStampButtonDisabled(true);
}}
/>
</View>
<View style={styles.addStampModalLabelBox}>
<TextInput
style={styles.addStampModalLabel}
placeholder='스탬프 이름 입력'
onChangeText={(text) => {
setAddStampDataLabel(text);
if(text.length > 0 && addStampDataEmotion.length > 0) setAddStampButtonDisabled(false);
else setAddStampButtonDisabled(true);
}}
/>
</View>
</View>
</View>
</Modal>
</Modal>
);
};
const styles = StyleSheet.create({
fixModalContainer: {
backgroundColor: 'white',
width: '100%',
height: '100%',
},
fixModalTitleContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginHorizontal: 20,
marginVertical: 10,
},
fixModalTitleContent: {
flexDirection: 'row',
alignItems: 'center',
gap: 10,
},
fixModalTitle: {
color: '#212429',
fontFamily: 'Pretendard',
fontWeight: '400',
fontSize: 16,
fontStyle: 'normal',
},
fixModalMessageContainer: {
paddingHorizontal: 20,
paddingVertical: 10,
},
fixModalMessage: {
color: '#A8A8A8',
textAlign: 'left',
fontFamily: 'Pretendard',
fontWeight: '500',
fontSize: 12,
fontStyle: 'normal',
},
stampList: {
width: 393,
// paddingHorizontal: 20,
// marginTop: 132,
// marginBottom: 60,
},
stampListContainer: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 20,
width: 393,
height: 60,
gap: 10,
},
moodInfo: {
flexDirection: 'row',
gap: 10,
alignItems: 'center',
},
moodEmotion: {
color: '#212429',
fontSize: 24,
},
moodText: {
color: '#212429',
fontFamily: 'Pretendard',
fontWeight: '400',
fontSize: 14,
fontStyle: 'normal',
lineHeight: 20,
},
fixModalButton: {
// position: 'absolute',
// bottom: 0,
width: '100%',
height: 60,
marginBottom: 30,
backgroundColor: '#FAFAFA',
alignItems: 'center',
justifyContent: 'center',
},
fixModalButtonText: {
color: '#000000',
fontFamily: 'Pretendard',
fontWeight: '600',
fontSize: 14,
fontStyle: 'normal',
},
keyboardAvoidingContainer: {
flex: 1,
},
overlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.3)', // 반투명한 검정색 배경
},
addStampModalContainer: {
// flex: 1,
backgroundColor: 'white',
width: '80%',
height: 260,
alignSelf: 'center',
marginTop: 'auto',
marginBottom: 'auto',
borderRadius: 16,
},
addStampModalTitleContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 15,
marginHorizontal: 16,
marginBottom: 40,
},
addStampModalTitle: {
color: '#212429',
fontFamily: 'Pretendard',
fontWeight: '400',
fontSize: 16,
},
checkImage: {
// 기본 이미지 스타일
},
disabledCheckImage: {
opacity: 0.2, // 비활성 시에 투명도 조절
},
addStampModalContent: {
// 가운데에 위치하도록
flex: 1,
justifyContent: 'space-between',
flexDirection: 'column',
alignItems: 'center',
// gap: 15,
marginBottom: 55,
},
addStampModalEmotionBox: {
width: 50,
height: 50,
borderRadius: 8,
borderWidth: 1,
borderColor: '#F0F0F0',
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
addStampModalEmotion: {
fontSize: 24,
},
addStampModalLabelBox: {
width: 296,
height: 50,
borderRadius: 8,
borderWidth: 1,
borderColor: '#F0F0F0',
backgroundColor: 'white',
paddingHorizontal: 10,
},
addStampModalLabel: {
fontSize: 16,
},
});
export default StampList;