-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChat.tsx
353 lines (323 loc) · 9.64 KB
/
Chat.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
import React, { useEffect, useState } from "react";
import { SafeAreaView, StyleSheet, Text, View, Image, TextInput, Pressable, FlatList, TouchableOpacity } from "react-native";
import Icon from 'react-native-vector-icons/dist/Ionicons';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Modal from "react-native-modal";
export function Chat({ route, navigation }) {
const [message, setMessage] = useState([]);
const [reply, setReply] = useState("");
const [isModalVisible, setModalVisible] = useState(false);
const [userLocation, setUserLocation] = useState("");
async function loadChat() {
var userJson = await AsyncStorage.getItem("user");
var userId = JSON.parse(userJson).id;
var form = new FormData();
form.append("from", userId);
form.append("to", route.params.id);
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
const chatResponseJSONText = request.responseText;
if (chatResponseJSONText != "") {
const chatResponseArray = JSON.parse(chatResponseJSONText);
setMessage(chatResponseArray);
}
}
};
request.open("POST", "http://10.0.2.2/manthra/loadChat.php", true);
request.send(form);
}
function chatRequestSender() {
setInterval(loadChat, 5000);
}
useEffect(chatRequestSender, []);
async function sendReply() {
var userJson = await AsyncStorage.getItem("user");
var userId = JSON.parse(userJson).id;
var form = new FormData();
form.append("message", reply);
form.append("from", userId);
form.append("to", route.params.id);
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
setReply("");
}
};
request.open("POST", "http://10.0.2.2/manthra/saveChat.php", true);
request.send(form);
}
function goToHome() {
navigation.navigate("Home");
}
function loadUserProfile() {
loadUserLocation();
setModalVisible(true);
}
function loadUserLocation() {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var text = request.responseText;
setUserLocation(text);
}
};
request.open("GET", "http://10.0.2.2/manthra/loadCountries.php?id=" + route.params.id, true);
request.send();
}
function closeUserProfile() {
setModalVisible(false);
}
const ui = (
<SafeAreaView style={styles4.main}>
<View style={styles4.headreView}>
<TouchableOpacity style={styles4.backBtn} onPress={goToHome}>
<Icon name="chevron-back-outline" size={28} color="black" />
</TouchableOpacity>
<View style={styles4.profileView}>
<Pressable onPress={loadUserProfile}>
<Image source={{ uri: route.params.profileImageStatus == 1 ? "http://10.0.2.2/manthra/resources/images/profile_images/" + route.params.mobile + ".png" : "http://10.0.2.2/manthra/resources/images/user.png" }} style={styles4.profileImg} />
</Pressable>
</View>
<View style={styles4.profileName}>
<Text style={styles4.headerText}>{route.params.name}</Text>
</View>
</View>
<FlatList data={message} renderItem={chatItem} />
<View style={styles4.messageSendArea}>
<TextInput style={styles4.messageSendInput} placeholder="Type message..." autoCorrect={false} maxLength={500} onChangeText={setReply} value={reply} />
<Pressable onPress={sendReply}>
<Icon name="send" style={styles4.sendIcon} size={35} color="black" />
</Pressable>
</View>
<View>
<Modal isVisible={isModalVisible} backdropOpacity={0.4} animationOutTiming={400}>
<View style={styles4.modal}>
<Text style={styles4.modalHeaderText}>{route.params.name}</Text>
<View style={styles4.modalContent}>
<Image source={{ uri: "http://10.0.2.2/manthra/resources/images/profile_images/0717414745.png" }} />
<Image source={{ uri: route.params.profileImageStatus == 1 ? "http://10.0.2.2/manthra/resources/images/profile_images/" + route.params.mobile + ".png" : "http://10.0.2.2/manthra/resources/images/user.png" }} style={styles4.modalImg} />
<View style={styles4.detailBox}>
<Icon name="phone-portrait" style={styles4.detailIcon}>{route.params.mobile}</Icon>
<Icon name="location" style={styles4.detailIcon}>{userLocation}</Icon>
</View>
<View style={styles4.btnBox}>
<TouchableOpacity onPress={closeUserProfile}>
<Text style={styles4.closeBtn}>Close</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
</View>
</SafeAreaView>
);
function chatItem({ item }) {
const ui = (
<View style={item.side == "right" ? styles4.chatViewRight : styles4.chatView}>
<View style={item.side == "right" ? styles4.chatBoxRight : styles4.chatBox}>
<View style={styles4.msgTextArea}>
<Text style={item.side == "right" ? styles4.msgTextRight : styles4.msgText}>{item.content}</Text>
</View>
<View style={styles4.timeArea}>
<Icon name="checkmark-outline" size={18} color="yellow" style={item.side == "left" || item.deliverStatus == "seen" ? styles4.dNone : styles4.chatIcon} />
<Icon name="checkmark-done-outline" size={18} color="yellow" style={item.side == "left" || item.deliverStatus == "unseen" ? styles4.dNone : styles4.chatIcon} />
<Text style={item.side == "right" ? styles4.timeRight : styles4.time}>{item.time}</Text>
</View>
</View>
</View>
);
return ui;
}
return ui;
}
const styles4 = StyleSheet.create(
{
main: {
display: "flex",
flexDirection: "column",
justifyContent: "flex-end",
width: "100%",
flexGrow: 1,
backgroundColor: "rgb(251, 255, 201)",
},
headreView: {
flexDirection: "row",
alignItems: "center",
width: "100%",
backgroundColor: "yellow",
borderBottomEndRadius: 35,
borderBottomStartRadius: 35,
},
headerText: {
color: "#080808",
fontSize: 23,
fontWeight: "bold",
fontFamily: "Poppins",
marginVertical: 15,
},
profileView: {
width: 90,
padding: 15,
},
profileImg: {
width: 60,
height: 60,
borderRadius: 30,
},
profileName: {
flexGrow: 1,
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
},
chatView: {
paddingStart: 15,
paddingEnd: 15,
width: "100%",
flexDirection: "row",
justifyContent: "flex-start",
},
chatViewRight: {
paddingStart: 15,
paddingEnd: 15,
width: "100%",
flexDirection: "row",
justifyContent: "flex-end",
},
chatBox: {
flexDirection: "column",
backgroundColor: "yellow",
paddingVertical: 6,
paddingHorizontal: 6,
borderStyle: "solid",
borderWidth: 1,
borderColor: "#0101003a",
borderRadius: 15,
marginBottom: 12,
maxWidth: "50%",
},
chatBoxRight: {
flexDirection: "column",
backgroundColor: "black",
paddingVertical: 6,
paddingHorizontal: 6,
borderStyle: "solid",
borderWidth: 1,
borderColor: "#0101003a",
borderRadius: 15,
marginBottom: 12,
maxWidth: "50%",
},
msgTextArea: {
flexDirection: "column",
paddingStart: 10,
paddingTop: 10,
paddingEnd: 10,
paddingBottom: 6,
},
timeArea: {
flexDirection: "row",
justifyContent: "flex-end",
},
msgText: {
fontSize: 17,
color: "black",
},
msgTextRight: {
fontSize: 17,
color: "white",
},
time: {
top: 2,
end: 1,
color: "black",
opacity: 0.5,
},
timeRight: {
top: 2,
end: 5,
color: "white",
opacity: 0.6,
},
chatIcon: {
marginLeft: 8,
paddingRight: 10,
},
dNone: {
display: "none",
},
messageSendArea: {
flexDirection: "row",
width: "100%",
paddingHorizontal: 15,
paddingBottom: 15,
},
messageSendInput: {
width: "100%",
borderStyle: "solid",
backgroundColor: "white",
borderWidth: 2,
borderColor: "#0101003a",
borderRadius: 25,
paddingStart: 20,
paddingEnd: 55,
fontSize: 22,
},
sendIcon: {
top: 8,
end: 45,
},
backBtn: {
paddingLeft: 5,
},
modal: {
backgroundColor: "yellow",
borderRadius: 25,
margin: 0,
padding: 0,
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
},
modalHeaderText: {
color: "#080808",
fontSize: 23,
fontWeight: "bold",
fontFamily: "Poppins",
paddingVertical: 10,
},
modalContent: {
backgroundColor: "white",
borderRadius: 25,
width: "100%",
},
modalImg: {
width: "100%",
height: 250,
},
detailBox: {
flexDirection: "row",
justifyContent: "center",
gap: 10,
paddingTop: 20,
paddingBottom: 15,
},
detailIcon: {
fontSize: 17,
color: "#080808",
opacity: 0.8,
},
btnBox: {
flexGrow: 1,
flexDirection: "row",
justifyContent: "center",
paddingVertical: 15,
},
closeBtn: {
fontSize: 18,
color: "#080808",
}
}
);