-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
465 lines (437 loc) · 15.5 KB
/
sample.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import React, { useState, useEffect, useRef } from 'react';
import { Row, Col } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { useSnackbar } from 'notistack';
import { listChats, createChat, deleteMessage } from '../actions/notificationActions';
import List from '@mui/material/List';
import Typography from '@mui/material/Typography';
import DeleteIcon from '@mui/icons-material/Delete';
import VisibilityIcon from '@mui/icons-material/Visibility';
import { useNavigate } from 'react-router-dom';
import CircularProgress from '@mui/material/CircularProgress';
import { connectWebsocket, disconnectWebsocket, receiveChatMessage } from '../actions/realActions'; // Correct import path
import { sendChat } from '../actions/realActions';
import { logout } from '../actions/userAction';
import { Link, useParams } from 'react-router-dom';
import { formatDistanceToNow } from 'date-fns';
import { CHAT_CREATE_RESET } from '../constants/notificationConstants';
import SendIcon from '@mui/icons-material/Send';
import Loader from '../components/Loader2';
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
import IconButton from '@mui/material/IconButton';
import { TextField } from '@mui/material';
import axios from 'axios';
function UserChatScreen() {
// WebSocket messages from Redux store - DO NOT TOUCH
const conversations = useSelector((state) => state.websocket.messages);
// New state for HTTP-fetched messages (chat history)
const [httpMessages, setHttpMessages] = useState([]);
const [loading, setLoading] = useState(true);
const [page, setPage] = useState(1);
const [searchText, setSearchText] = useState('');
const [totalPages, setTotalPages] = useState(1);
const { enqueueSnackbar } = useSnackbar(); // useSnackbar hook
const dispatch = useDispatch();
const { id } = useParams();
const [loading2, setLoading2] = useState(true);
const pusherNotificationsRef = useRef(null);
const messageListRef = useRef(null);
const navigate = useNavigate();
const userLogin = useSelector((state) => state.userLogin);
const { userInfo } = userLogin;
const [message, setMessage] = useState([]);
let [newMessage, setnewMessage] = useState({ message: "" });
const [receiver_id, setReceiver_id] = useState(id);
const [content, setContent] = useState('');
const chatCreate = useSelector((state) => state.chatCreate);
const { loading: loadingChat, error: errorChat, success, chat } = chatCreate;
const [hasExpired, setHasExpired] = useState(false);
const [currentTime, setCurrentTime] = useState(new Date());
const expirationTime = userInfo?.expiration_time;
const websocket = useSelector((state) => state.websocket); // Access the websocket state from the store
const isConnected = websocket.connected;
const socket = websocket.socket; // Get the actual socket instance
// WebSocket connection (DO NOT TOUCH)
useEffect(() => {
if (userInfo) {
dispatch(connectWebsocket());
}
return () => {
dispatch(disconnectWebsocket());
};
}, [dispatch, userInfo]);
useEffect(() => {
if (!userInfo) {
navigate('/home');
}
}, [navigate, userInfo]);
const logoutHandler = () => {
dispatch(logout());
navigate('/home');
window.location.reload();
};
useEffect(() => {
if (userInfo) {
const [, year, month, day, hour, minute, second] =
expirationTime.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
const expirationDateTime = new Date(year, month - 1, day, hour, minute, second);
setCurrentTime(new Date());
setHasExpired(expirationDateTime < new Date());
const timer = setInterval(() => setCurrentTime(new Date()), 1000);
return () => clearInterval(timer);
}
}, [expirationTime]);
useEffect(() => {
if (hasExpired) {
logoutHandler();
}
}, [navigate, hasExpired]);
// FETCH HTTP MESSAGES: Chat history from the database (DO NOT TOUCH WebSocket stuff)
useEffect(() => {
if (userInfo) {
const fetchData = async () => {
try {
setLoading(true);
const config = {
headers: {
'Content-type': 'application/json',
Authorization: `Bearer ${userInfo.token}`,
},
};
const response = await axios.get(
`/api/notifications/chats/${id}/?name=${searchText}&page=${page}`,
config
);
// Save fetched messages into the httpMessages state
setHttpMessages(response.data.results);
setTotalPages(response.data.total_pages);
console.log('HTTP fetched messages:', response.data.results);
} catch (error) {
console.error('Error fetching posts:', error);
} finally {
setLoading(false);
setLoading2(false);
}
};
fetchData();
}
}, [page, searchText, userInfo]);
// WebSocket onmessage handler (DO NOT TOUCH)
useEffect(() => {
if (socket && isConnected) {
socket.onmessage = (event) => {
const incomingMessage = JSON.parse(event.data).message;
dispatch(receiveChatMessage(incomingMessage));
};
}
return () => {
if (socket) {
socket.onmessage = null;
}
};
}, [socket, isConnected, dispatch]);
const handleLoadMore = () => {
// Increment the page to fetch the next set of posts
setPage((prevPage) => prevPage + 1);
};
const formatTimestamp = (timestamp) => {
const date = new Date(timestamp);
if (isNaN(date.getTime())) {
return 'Invalid Date';
}
const now = new Date();
const timeDifference = formatDistanceToNow(date, { addSuffix: true });
return timeDifference;
};
const handleChange = (event) => {
setnewMessage({
...newMessage,
[event.target.name]: event.target.value,
});
};
const handleDelete = (messageId) => {
// Dispatch an action to delete the message with the messageId
dispatch(deleteMessage(messageId));
};
const submitHandler = (e) => {
e.preventDefault();
dispatch(
sendChat({
receiver_id,
content,
})
);
setContent("");
};
return (
<div style={{}}>
<br />
<br />
<br />
<div>
<form onSubmit={submitHandler}>
<TextField
className="form-control"
placeholder="Write a message"
multiline
value={content}
onChange={(e) => setContent(e.target.value)}
autoFocus={false}
style={{
backgroundColor: '#fff5f5', // light red background
border: '1px solid #ffcccc', // light red border
borderRadius: '20px', // rounded corners for cloud effect
padding: '15px 20px', // internal padding
color: '#333', // text color
fontFamily: 'Arial, sans-serif', // font family
fontSize: '14px', // font size
outline: 'none', // remove default outline
boxShadow: '0 4px 8px rgba(255, 0, 0, 0.1)', // subtle shadow
transition: 'box-shadow 0.3s ease-in-out', // smooth transition for shadow
}}
onFocus={(e) =>
(e.target.style.boxShadow = '0 4px 12px rgba(255, 0, 0, 0.2)')
}
onBlur={(e) =>
(e.target.style.boxShadow = '0 4px 8px rgba(255, 0, 0, 0.1)')
}
/>
{loadingChat ? (
<CircularProgress />
) : (
<button type="submit" className="btn btn-primary mt-2">
<SendIcon />
</button>
)}
</form>
</div>
{/* Section for HTTP-fetched messages (chat history) */}
<div style={{ marginTop: '20px' }}>
<h4>Chat History (from Database)</h4>
{loading2 ? (
<div
style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
textAlign: 'center',
}}
>
<CircularProgress />
</div>
) : httpMessages.length < 1 ? (
<h6>No Chat History Available</h6>
) : (
httpMessages.map((msg, index) => {
if (!msg || !msg.sender) return null;
return (
<div
key={index}
style={{
display: 'flex',
flexDirection: 'column',
alignItems:
userInfo.id === msg.sender ? 'flex-end' : 'flex-start',
margin: '10px 0',
}}
>
<div
style={{
backgroundColor:
userInfo.id === msg.sender ? '#e0f7fa' : '#f1f1f1',
color: '#000',
borderRadius: '20px',
padding: '10px 20px',
maxWidth: '75%',
alignSelf:
userInfo.id === msg.sender ? 'flex-end' : 'flex-start',
wordWrap: 'break-word',
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<p
style={{
fontSize: 'small',
color:
userInfo.email === msg.name ? 'red' : 'blue',
margin: 0,
}}
>
{userInfo.email === msg.name ? (
<i style={{ fontSize: 'small', color: 'red' }}>
Me
</i>
) : (
<span>{msg.name}</span>
)}
</p>
<strong style={{ fontSize: 'x-small', color: '#555' }}>
{formatTimestamp(msg.timestamp)}
</strong>
<div>
{!msg.is_read && userInfo.id === msg.sender && (
<DeleteIcon
style={{
cursor: 'pointer',
marginLeft: '10px',
color: 'red',
}}
onClick={() => handleDelete(msg.id)}
/>
)}
{msg.is_read && userInfo.id === msg.sender && (
<VisibilityIcon
style={{
cursor: 'pointer',
marginLeft: '10px',
color: 'blue',
}}
/>
)}
</div>
</div>
<div
style={{
whiteSpace: 'pre-line',
overflow: 'hidden',
marginTop: '5px',
}}
>
<Typography variant="body2" component="div">
{msg.content}
</Typography>
</div>
</div>
</div>
);
})
)}
</div>
{/* Divider between HTTP-fetched and WebSocket messages */}
<hr style={{ margin: '30px 0' }} />
{/* Section for WebSocket messages */}
<div className="list-group list-group-flush border-bottom scrollarea" ref={messageListRef}>
<h4>Live Chat (WebSocket)</h4>
{conversations.length < 1 ? (
loading2 ? (
<div
style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
textAlign: 'center',
}}
>
<CircularProgress />
</div>
) : (
<h6>No New Chat :(</h6>
)
) : (
conversations.map((msg, index) => {
if (!msg || !msg.sender) return null;
return (
<div
key={index}
style={{
display: 'flex',
flexDirection: 'column',
alignItems:
userInfo.id === msg.sender ? 'flex-end' : 'flex-start',
margin: '10px 0',
}}
>
<div
style={{
backgroundColor:
userInfo.id === msg.sender ? '#e0f7fa' : '#f1f1f1',
color: '#000',
borderRadius: '20px',
padding: '10px 20px',
maxWidth: '75%',
alignSelf:
userInfo.id === msg.sender ? 'flex-end' : 'flex-start',
wordWrap: 'break-word',
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<p
style={{
fontSize: 'small',
color:
userInfo.email === msg.name ? 'red' : 'blue',
margin: 0,
}}
>
{userInfo.email === msg.name ? (
<i style={{ fontSize: 'small', color: 'red' }}>
Me
</i>
) : (
<span>{msg.name}</span>
)}
</p>
<strong style={{ fontSize: 'x-small', color: '#555' }}>
{formatTimestamp(msg.timestamp)}
</strong>
<div>
{!msg.is_read && userInfo.id === msg.sender && (
<DeleteIcon
style={{
cursor: 'pointer',
marginLeft: '10px',
color: 'red',
}}
onClick={() => handleDelete(msg.id)}
/>
)}
{msg.is_read && userInfo.id === msg.sender && (
<VisibilityIcon
style={{
cursor: 'pointer',
marginLeft: '10px',
color: 'blue',
}}
/>
)}
</div>
</div>
<div
style={{
whiteSpace: 'pre-line',
overflow: 'hidden',
marginTop: '5px',
}}
>
<Typography variant="body2" component="div">
{msg.content}
</Typography>
</div>
</div>
</div>
);
})
)}
</div>
</div>
);
}
export default UserChatScreen;