blob: 36932bfcc7339cdaf0c0df793c3efdf2c461097a [file] [log] [blame]
ervinanohb81c3912022-09-08 04:13:24 -04001import React, { useEffect } from 'react';
idillon531b6f22022-09-16 14:02:00 -04002import Modal from 'react-modal';
idillon531b6f22022-09-16 14:02:00 -04003import authManager from '../AuthManager'
ervinanoh8e918042022-09-06 10:30:59 -04004import ConversationAvatar from './ConversationAvatar'
5import Conversation from '../../../model/Conversation'
6import React, { useState } from "react";
7import { useNavigate, useParams } from "react-router-dom"
ervinanohb81c3912022-09-08 04:13:24 -04008import { ListItem, ListItemAvatar, ListItemText, Box, Typography } from '@mui/material'
ervinanoh34eb9472022-09-13 04:20:28 -04009import { Button, Stack, Typography, Modal as ModalUM } from "@mui/material"
ervinanohb81c3912022-09-08 04:13:24 -040010import { RemoveContactIcon, VideoCallIcon } from './svgIcons';
11import { AudioCallIcon, BlockContactIcon, ContactDetailsIcon, CrossIcon, MessageIcon } from './svgIcons';
ervinanoh34eb9472022-09-13 04:20:28 -040012import { QRCodeCanvas} from 'qrcode.react';
13import { setRefreshFromSlice } from '../../redux/appSlice';
14import { useAppDispatch } from '../../redux/hooks';
15
idillon531b6f22022-09-16 14:02:00 -040016
17const customStyles = {
18 content: {
ervinanoh8e918042022-09-06 10:30:59 -040019 // right: "auto",
20 // bottom: "auto",
21 // // marginRight: "-50%",
22 // transform: "translate(-50%, -50%)",
ervinanoh8e918042022-09-06 10:30:59 -040023 // padding: "16px"
24
25 // top: "1364px",
26 left: "94px",
ervinanohb81c3912022-09-08 04:13:24 -040027 width: "180px",
ervinanoh8e918042022-09-06 10:30:59 -040028 height: "262px",
29 background: "#FFFFFF 0% 0% no-repeat padding-box",
30 boxShadow: "3px 3px 7px #00000029",
31 borderRadius: "5px 20px 20px 20px",
32 opacity: "1",
33
34 textAlign: "left",
ervinanohb81c3912022-09-08 04:13:24 -040035 font: "normal normal normal 12px/26px Ubuntu",
36 letterSpacing: "0px",
37 color: "#000000",
38 },
39};
40
41const cancelStyles = {
42 content: {
43 left: "94px",
44 width: "300px",
45 height: "220px",
46 background: "#FFFFFF 0% 0% no-repeat padding-box",
47 boxShadow: "3px 3px 7px #00000029",
48 borderRadius: "20px",
49 opacity: "1",
50
51 textAlign: "left",
52 font: "normal normal normal 12px/26px Ubuntu",
53 letterSpacing: "0px",
54 color: "#000000",
55 },
56};
57
58const contactDetailsStyles = {
59 content: {
60 left: "94px",
61 width: "450px",
62 height: "450px",
63 background: "#FFFFFF 0% 0% no-repeat padding-box",
64 boxShadow: "3px 3px 7px #00000029",
65 borderRadius: "20px",
66 opacity: "1",
67
68 textAlign: "left",
69 font: "normal normal normal 12px/26px Ubuntu",
ervinanoh8e918042022-09-06 10:30:59 -040070 letterSpacing: "0px",
71 color: "#000000",
idillon531b6f22022-09-16 14:02:00 -040072 },
73};
Adrien Béraud995e8022021-04-08 13:46:51 -040074
ervinanoh8e918042022-09-06 10:30:59 -040075const stackStyles = {
76 flexDirection: "row",
ervinanohb81c3912022-09-08 04:13:24 -040077 marginBottom: "4px",
78 spacing: "40px",
ervinanoh8e918042022-09-06 10:30:59 -040079 flex: 1,
80 alignItems: "center",
ervinanoh8e918042022-09-06 10:30:59 -040081};
82
ervinanohb81c3912022-09-08 04:13:24 -040083const iconTextStyle = {
84 marginRight: "10px",
85};
86
87const iconColor = "#005699";
88
Adrien Béraudaf09a462021-04-15 18:02:29 -040089export default function ConversationListItem(props) {
idillon531b6f22022-09-16 14:02:00 -040090 const { conversationId, contactId } = useParams();
ervinanoh34eb9472022-09-13 04:20:28 -040091 const dispatch = useAppDispatch();
92
idillon531b6f22022-09-16 14:02:00 -040093 const conversation = props.conversation;
Adrien Béraud995e8022021-04-08 13:46:51 -040094
idillon531b6f22022-09-16 14:02:00 -040095 const pathId = conversationId || contactId;
96 const isSelected = conversation.getDisplayUri() === pathId;
97 const navigate = useNavigate();
98
idillon531b6f22022-09-16 14:02:00 -040099 const [modalIsOpen, setIsOpen] = useState(false);
100 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
101 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanohb81c3912022-09-08 04:13:24 -0400102 const [blockOrRemove, setBlockOrRemove] = useState(true);
103 const [userId, setUserId] = useState(
ervinanoh34eb9472022-09-13 04:20:28 -0400104 conversation?.getFirstMember()?.contact.getUri()
ervinanohb81c3912022-09-08 04:13:24 -0400105 );
106 const [isSwarm, setIsSwarm] = useState("true");
idillon531b6f22022-09-16 14:02:00 -0400107
ervinanohb81c3912022-09-08 04:13:24 -0400108 const openModal = (e) => {
109 e.preventDefault();
110 console.log(e);
111 setIsOpen(true);
112 };
idillon531b6f22022-09-16 14:02:00 -0400113 const openModalDetails = () => setModalDetailsIsOpen(true);
114 const openModalDelete = () => setModalDeleteIsOpen(true);
idillon531b6f22022-09-16 14:02:00 -0400115 const closeModal = () => setIsOpen(false);
idillon531b6f22022-09-16 14:02:00 -0400116 const closeModalDetails = () => setModalDetailsIsOpen(false);
117 const closeModalDelete = () => setModalDeleteIsOpen(false);
118
ervinanohb81c3912022-09-08 04:13:24 -0400119 let subtitle;
idillon531b6f22022-09-16 14:02:00 -0400120 function afterOpenModal() {
121 // references are now sync'd and can be accessed.
122 subtitle.style.color = "#f00";
123 }
124
ervinanohb81c3912022-09-08 04:13:24 -0400125 const getContactDetails = () => {
idillon531b6f22022-09-16 14:02:00 -0400126 const controller = new AbortController();
127 authManager
ervinanohb81c3912022-09-08 04:13:24 -0400128 .fetch(
129 `/api/accounts/${conversation.getAccountId()}/contacts/details/${userId}`,
130 {
131 signal: controller.signal,
132 }
133 )
134 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -0400135 .then((result) => {
ervinanohb81c3912022-09-08 04:13:24 -0400136 console.log("CONTACT LIST - DETAILS: ", result);
idillon531b6f22022-09-16 14:02:00 -0400137 })
ervinanohb81c3912022-09-08 04:13:24 -0400138 .catch((e) => console.log("ERROR GET CONTACT DETAILS: ", e));
idillon531b6f22022-09-16 14:02:00 -0400139 };
140
ervinanohb81c3912022-09-08 04:13:24 -0400141 const removeOrBlock = (typeOfRemove) => {
142 console.log(typeOfRemove);
143 setBlockOrRemove(false);
144
145 console.log("EEEH", typeOfRemove, conversation.getAccountId(), userId);
146
147 const controller = new AbortController();
148 authManager
149 .fetch(
150 `/api/accounts/${conversation.getAccountId()}/contacts/${typeOfRemove}/${userId}`,
151 {
152 signal: controller.signal,
153 method: "DELETE",
154 }
155 )
156 .then((res) => res.json())
ervinanoh34eb9472022-09-13 04:20:28 -0400157 .then((result) => {
158 console.log("propre");
159 dispatch(setRefreshFromSlice());
160 })
161 .catch((e) => {
162 console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e);
163 dispatch(setRefreshFromSlice());
164 });
ervinanohb81c3912022-09-08 04:13:24 -0400165 closeModalDelete();
ervinanohb81c3912022-09-08 04:13:24 -0400166 };
ervinanohb81c3912022-09-08 04:13:24 -0400167
idillon531b6f22022-09-16 14:02:00 -0400168
169 const uri = conversation.getId()
170 ? `conversation/${conversation.getId()}`
ervinanohb81c3912022-09-08 04:13:24 -0400171 : `addContact/${userId}`;
idillon531b6f22022-09-16 14:02:00 -0400172 if (conversation instanceof Conversation) {
173 return (
ervinanohb81c3912022-09-08 04:13:24 -0400174 <div onContextMenu={openModal}>
175 <div name="Modal conversation">
176 <Modal
177 isOpen={modalIsOpen}
178 // onAfterOpen={afterOpenModal}
179 onRequestClose={closeModal}
180 style={customStyles}
181 contentLabel="Example Modal"
idillon531b6f22022-09-16 14:02:00 -0400182 >
ervinanohb81c3912022-09-08 04:13:24 -0400183 <Stack
184 onClick={() => {
185 navigate(`/account/${conversation.getAccountId()}/${uri}`);
186 closeModal();
187 }}
188 {...stackStyles}
189 >
190 <div style={{ ...iconTextStyle }}>
191 <MessageIcon style={{ color: iconColor }} />
192 </div>
193 Message
194 </Stack>
195 <Stack {...stackStyles}>
196 <div style={{ ...iconTextStyle }}>
197 <AudioCallIcon style={{ color: iconColor }} />
198 </div>
199 Démarrer appel audio
200 </Stack>
201
202 <Stack {...stackStyles}>
203 <div style={{ ...iconTextStyle }}>
204 <VideoCallIcon style={{ color: iconColor }} />
205 </div>
206 Démarrer appel vidéo
207 </Stack>
208
209 <Stack
210 {...stackStyles}
211 onClick={() => {
212 navigate(`/account/${conversation.getAccountId()}/`);
213 closeModal();
ervinanoh8e918042022-09-06 10:30:59 -0400214 }}
215 >
ervinanohb81c3912022-09-08 04:13:24 -0400216 <div style={{ ...iconTextStyle }}>
217 <CrossIcon style={{ color: iconColor }} />
218 </div>
219 Fermer la conversation
220 </Stack>
221
222 <Stack
223 onClick={() => {
224 console.log("open details contact for: ");
225 closeModal();
226 openModalDetails();
227 getContactDetails();
228 }}
229 {...stackStyles}
230 >
231 <div style={{ ...iconTextStyle }}>
232 <ContactDetailsIcon style={{ color: iconColor }} />
233 </div>
234 Détails de la conversation
235 </Stack>
236
237 <Stack
238 onClick={() => {
239 setBlockOrRemove(true);
240 closeModal();
241 openModalDelete();
242 }}
243 {...stackStyles}
244 >
245 <div style={{ ...iconTextStyle }}>
246 <BlockContactIcon style={{ color: iconColor }} />
247 </div>
248 Bloquer le contact
249 </Stack>
250
251 <Stack
252 onClick={() => {
253 setBlockOrRemove(false);
254 closeModal();
255 openModalDelete();
256 }}
257 {...stackStyles}
258 >
259 <div style={{ ...iconTextStyle }}>
260 <RemoveContactIcon style={{ color: iconColor }} />
261 </div>
262 Supprimer contact
263 </Stack>
264 </Modal>
265 </div>
266
267 <div name="Contact details">
268 <Modal
269 isOpen={modalDetailsIsOpen}
270 onRequestClose={closeModalDetails}
271 style={contactDetailsStyles}
272 contentLabel="Détails contact"
273 >
274 <Stack direction={"row"} alignContent="flex-end">
275 <Stack direction={"column"}>
276 <div style={{ height: "100px" }}>
277 <ConversationAvatar
278 displayName={conversation.getDisplayNameNoFallback()}
279 />
280 </div>
281
282 <div
283 style={{
284 fontSize: "20px",
285 marginBottom: "20px",
286 height: "20px",
287 }}
288 >
289 Informations
290 </div>
291
292 <Typography variant="caption">Nom d'utilisateur</Typography>
293 <div style={{ height: "20px" }} />
294 <Typography variant="caption">Identifiant </Typography>
295 <div style={{ height: "20px" }} />
296
297 <div
298 style={{
299 flex: 1,
300 height: "150px",
301 direction: "column",
302 flexDirection: "column",
303 // alignSelf: "flex-end",
304 }}
305 >
306 <Typography variant="caption">Code QR</Typography>
307 </div>
308
309 <Typography variant="caption">est un swarm </Typography>
310 </Stack>
311
312 <Stack direction={"column"}>
313 <div
314 style={{
315 fontWeight: "bold",
316 fontSize: "20px",
317 height: "100px",
318 }}
319 >
320 {conversation.getDisplayNameNoFallback() + "(resolved name)"}
321 </div>
322
323 <div
324 style={{
325 height: "40px",
326 }}
327 />
328 <Typography variant="caption">
329 <div style={{ fontWeight: "bold" }}>
330 {conversation.getDisplayNameNoFallback()}
331 </div>
332 </Typography>
333
334 <div style={{ height: "20px" }} />
335
336 <Typography variant="caption">
337 <div style={{ fontWeight: "bold" }}> {userId}</div>
338 </Typography>
339
340 <div style={{ height: "20px" }} />
341
342 <div height={"40px"}>
343 <QRCodeCanvas value={`${userId}`} />
344 </div>
345
346 <Typography variant="caption">
347 <div style={{ fontWeight: "bold" }}> {isSwarm}</div>
348 </Typography>
349 </Stack>
350 </Stack>
351 <div
352 onClick={closeModalDetails}
353 style={{
354 width: "100px",
355 borderStyle: "solid",
356 textAlign: "center",
357 borderRadius: "5px",
358 marginLeft: "150px",
359 marginTop: "10px",
360 }}
361 >
362 <Typography variant="caption">Fermer</Typography>
ervinanoh8e918042022-09-06 10:30:59 -0400363 </div>
ervinanohb81c3912022-09-08 04:13:24 -0400364 </Modal>
365 </div>
idillon531b6f22022-09-16 14:02:00 -0400366
ervinanohb81c3912022-09-08 04:13:24 -0400367 <div name="Remove or block details">
368 <Modal
369 isOpen={modalDeleteIsOpen}
370 onRequestClose={closeModalDelete}
371 style={cancelStyles}
372 contentLabel="Merci de confirmer"
idillon531b6f22022-09-16 14:02:00 -0400373 >
ervinanohb81c3912022-09-08 04:13:24 -0400374 <Typography variant="h4">Merci de confirmer</Typography>
375 <Stack
376 direction={"column"}
377 justifyContent="space-around"
378 spacing={"75px"}
379 >
380 <div style={{ textAlign: "center", marginTop: "10%" }}>
381 <Typography variant="body2">
382 Voulez vous vraiment {blockOrRemove ? "bloquer" : "supprimer"}{" "}
383 ce contact?
384 </Typography>
385 </div>
ervinanoh8e918042022-09-06 10:30:59 -0400386
ervinanohb81c3912022-09-08 04:13:24 -0400387 <Stack
388 direction={"row"}
389 top={"25px"}
390 alignSelf="center"
391 spacing={1}
392 >
393 <Box
394 onClick={() => {
395 if (blockOrRemove) removeOrBlock("block");
396 else removeOrBlock("remove");
397 }}
398 style={{
399 width: "100px",
400 textAlign: "center",
401 borderStyle: "solid",
402 borderColor: "red",
403 borderRadius: "10px",
404 color: "red",
405 }}
406 >
407 {blockOrRemove ? "Bloquer" : "Supprimer"}
408 </Box>
409 <Box
410 onClick={closeModalDelete}
411 style={{
412 width: "100px",
413 textAlign: "center",
414 paddingLeft: "12px",
415 paddingRight: "12px",
416 borderStyle: "solid",
417 borderRadius: "10px",
418 }}
419 >
420 Annuler
421 </Box>
422 </Stack>
423 </Stack>
424 </Modal>
425 </div>
idillon531b6f22022-09-16 14:02:00 -0400426
427 <ListItem
428 button
429 alignItems="flex-start"
430 selected={isSelected}
ervinanohb81c3912022-09-08 04:13:24 -0400431 onClick={() =>
432 navigate(`/account/${conversation.getAccountId()}/${uri}`)
433 }
idillon531b6f22022-09-16 14:02:00 -0400434 >
435 <ListItemAvatar>
436 <ConversationAvatar
437 displayName={conversation.getDisplayNameNoFallback()}
438 />
439 </ListItemAvatar>
440 <ListItemText
441 primary={conversation.getDisplayName()}
442 secondary={conversation.getDisplayUri()}
443 />
444 </ListItem>
445 </div>
446 );
447 } else return null;
Adrien Béraud995e8022021-04-08 13:46:51 -0400448}