blob: e0b8e491ea28951a077d105e2fa8d9828eebcfe3 [file] [log] [blame]
idillon531b6f22022-09-16 14:02:00 -04001import Modal from 'react-modal';
idillon531b6f22022-09-16 14:02:00 -04002import authManager from '../AuthManager'
ervinanoh8e918042022-09-06 10:30:59 -04003import ConversationAvatar from './ConversationAvatar'
4import Conversation from '../../../model/Conversation'
Adrien Béraud023f7cf2022-09-18 14:57:53 -04005import { useState } from "react";
ervinanoh8e918042022-09-06 10:30:59 -04006import { useNavigate, useParams } from "react-router-dom"
Adrien Béraud023f7cf2022-09-18 14:57:53 -04007import { ListItem, Stack, ListItemAvatar, ListItemText, Box, Typography } from '@mui/material'
ervinanohb81c3912022-09-08 04:13:24 -04008import { RemoveContactIcon, VideoCallIcon } from './svgIcons';
9import { AudioCallIcon, BlockContactIcon, ContactDetailsIcon, CrossIcon, MessageIcon } from './svgIcons';
ervinanoh34eb9472022-09-13 04:20:28 -040010import { QRCodeCanvas} from 'qrcode.react';
11import { setRefreshFromSlice } from '../../redux/appSlice';
12import { useAppDispatch } from '../../redux/hooks';
13
idillon531b6f22022-09-16 14:02:00 -040014
15const customStyles = {
16 content: {
ervinanoh8e918042022-09-06 10:30:59 -040017 // right: "auto",
18 // bottom: "auto",
19 // // marginRight: "-50%",
20 // transform: "translate(-50%, -50%)",
ervinanoh8e918042022-09-06 10:30:59 -040021 // padding: "16px"
22
23 // top: "1364px",
24 left: "94px",
ervinanohb81c3912022-09-08 04:13:24 -040025 width: "180px",
ervinanoh8e918042022-09-06 10:30:59 -040026 height: "262px",
27 background: "#FFFFFF 0% 0% no-repeat padding-box",
28 boxShadow: "3px 3px 7px #00000029",
29 borderRadius: "5px 20px 20px 20px",
30 opacity: "1",
31
32 textAlign: "left",
ervinanohb81c3912022-09-08 04:13:24 -040033 font: "normal normal normal 12px/26px Ubuntu",
34 letterSpacing: "0px",
35 color: "#000000",
36 },
37};
38
39const cancelStyles = {
40 content: {
41 left: "94px",
42 width: "300px",
43 height: "220px",
44 background: "#FFFFFF 0% 0% no-repeat padding-box",
45 boxShadow: "3px 3px 7px #00000029",
46 borderRadius: "20px",
47 opacity: "1",
48
49 textAlign: "left",
50 font: "normal normal normal 12px/26px Ubuntu",
51 letterSpacing: "0px",
52 color: "#000000",
53 },
54};
55
56const contactDetailsStyles = {
57 content: {
58 left: "94px",
59 width: "450px",
60 height: "450px",
61 background: "#FFFFFF 0% 0% no-repeat padding-box",
62 boxShadow: "3px 3px 7px #00000029",
63 borderRadius: "20px",
64 opacity: "1",
65
66 textAlign: "left",
67 font: "normal normal normal 12px/26px Ubuntu",
ervinanoh8e918042022-09-06 10:30:59 -040068 letterSpacing: "0px",
69 color: "#000000",
idillon531b6f22022-09-16 14:02:00 -040070 },
71};
Adrien Béraud995e8022021-04-08 13:46:51 -040072
ervinanoh8e918042022-09-06 10:30:59 -040073const stackStyles = {
74 flexDirection: "row",
ervinanohb81c3912022-09-08 04:13:24 -040075 marginBottom: "4px",
76 spacing: "40px",
ervinanoh8e918042022-09-06 10:30:59 -040077 flex: 1,
78 alignItems: "center",
ervinanoh8e918042022-09-06 10:30:59 -040079};
80
ervinanohb81c3912022-09-08 04:13:24 -040081const iconTextStyle = {
82 marginRight: "10px",
83};
84
85const iconColor = "#005699";
86
Adrien Béraudaf09a462021-04-15 18:02:29 -040087export default function ConversationListItem(props) {
idillon531b6f22022-09-16 14:02:00 -040088 const { conversationId, contactId } = useParams();
ervinanoh34eb9472022-09-13 04:20:28 -040089 const dispatch = useAppDispatch();
90
idillon531b6f22022-09-16 14:02:00 -040091 const conversation = props.conversation;
Adrien Béraud995e8022021-04-08 13:46:51 -040092
idillon531b6f22022-09-16 14:02:00 -040093 const pathId = conversationId || contactId;
94 const isSelected = conversation.getDisplayUri() === pathId;
95 const navigate = useNavigate();
96
idillon531b6f22022-09-16 14:02:00 -040097 const [modalIsOpen, setIsOpen] = useState(false);
98 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
99 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanohb81c3912022-09-08 04:13:24 -0400100 const [blockOrRemove, setBlockOrRemove] = useState(true);
101 const [userId, setUserId] = useState(
ervinanoh34eb9472022-09-13 04:20:28 -0400102 conversation?.getFirstMember()?.contact.getUri()
ervinanohb81c3912022-09-08 04:13:24 -0400103 );
104 const [isSwarm, setIsSwarm] = useState("true");
idillon531b6f22022-09-16 14:02:00 -0400105
ervinanohb81c3912022-09-08 04:13:24 -0400106 const openModal = (e) => {
107 e.preventDefault();
108 console.log(e);
109 setIsOpen(true);
110 };
idillon531b6f22022-09-16 14:02:00 -0400111 const openModalDetails = () => setModalDetailsIsOpen(true);
112 const openModalDelete = () => setModalDeleteIsOpen(true);
idillon531b6f22022-09-16 14:02:00 -0400113 const closeModal = () => setIsOpen(false);
idillon531b6f22022-09-16 14:02:00 -0400114 const closeModalDetails = () => setModalDetailsIsOpen(false);
115 const closeModalDelete = () => setModalDeleteIsOpen(false);
116
ervinanohb81c3912022-09-08 04:13:24 -0400117 let subtitle;
idillon531b6f22022-09-16 14:02:00 -0400118 function afterOpenModal() {
119 // references are now sync'd and can be accessed.
120 subtitle.style.color = "#f00";
121 }
122
ervinanohb81c3912022-09-08 04:13:24 -0400123 const getContactDetails = () => {
idillon531b6f22022-09-16 14:02:00 -0400124 const controller = new AbortController();
125 authManager
ervinanohb81c3912022-09-08 04:13:24 -0400126 .fetch(
127 `/api/accounts/${conversation.getAccountId()}/contacts/details/${userId}`,
128 {
129 signal: controller.signal,
130 }
131 )
132 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -0400133 .then((result) => {
ervinanohb81c3912022-09-08 04:13:24 -0400134 console.log("CONTACT LIST - DETAILS: ", result);
idillon531b6f22022-09-16 14:02:00 -0400135 })
ervinanohb81c3912022-09-08 04:13:24 -0400136 .catch((e) => console.log("ERROR GET CONTACT DETAILS: ", e));
idillon531b6f22022-09-16 14:02:00 -0400137 };
138
ervinanohb81c3912022-09-08 04:13:24 -0400139 const removeOrBlock = (typeOfRemove) => {
140 console.log(typeOfRemove);
141 setBlockOrRemove(false);
142
143 console.log("EEEH", typeOfRemove, conversation.getAccountId(), userId);
144
145 const controller = new AbortController();
146 authManager
147 .fetch(
148 `/api/accounts/${conversation.getAccountId()}/contacts/${typeOfRemove}/${userId}`,
149 {
150 signal: controller.signal,
151 method: "DELETE",
152 }
153 )
154 .then((res) => res.json())
ervinanoh34eb9472022-09-13 04:20:28 -0400155 .then((result) => {
156 console.log("propre");
157 dispatch(setRefreshFromSlice());
158 })
159 .catch((e) => {
160 console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e);
161 dispatch(setRefreshFromSlice());
162 });
ervinanohb81c3912022-09-08 04:13:24 -0400163 closeModalDelete();
ervinanohb81c3912022-09-08 04:13:24 -0400164 };
ervinanohb81c3912022-09-08 04:13:24 -0400165
idillon531b6f22022-09-16 14:02:00 -0400166
167 const uri = conversation.getId()
168 ? `conversation/${conversation.getId()}`
ervinanohb81c3912022-09-08 04:13:24 -0400169 : `addContact/${userId}`;
idillon531b6f22022-09-16 14:02:00 -0400170 if (conversation instanceof Conversation) {
171 return (
ervinanohb81c3912022-09-08 04:13:24 -0400172 <div onContextMenu={openModal}>
173 <div name="Modal conversation">
174 <Modal
175 isOpen={modalIsOpen}
176 // onAfterOpen={afterOpenModal}
177 onRequestClose={closeModal}
178 style={customStyles}
179 contentLabel="Example Modal"
idillon531b6f22022-09-16 14:02:00 -0400180 >
ervinanohb81c3912022-09-08 04:13:24 -0400181 <Stack
182 onClick={() => {
183 navigate(`/account/${conversation.getAccountId()}/${uri}`);
184 closeModal();
185 }}
186 {...stackStyles}
187 >
188 <div style={{ ...iconTextStyle }}>
189 <MessageIcon style={{ color: iconColor }} />
190 </div>
191 Message
192 </Stack>
193 <Stack {...stackStyles}>
194 <div style={{ ...iconTextStyle }}>
195 <AudioCallIcon style={{ color: iconColor }} />
196 </div>
197 Démarrer appel audio
198 </Stack>
199
200 <Stack {...stackStyles}>
201 <div style={{ ...iconTextStyle }}>
202 <VideoCallIcon style={{ color: iconColor }} />
203 </div>
204 Démarrer appel vidéo
205 </Stack>
206
207 <Stack
208 {...stackStyles}
209 onClick={() => {
210 navigate(`/account/${conversation.getAccountId()}/`);
211 closeModal();
ervinanoh8e918042022-09-06 10:30:59 -0400212 }}
213 >
ervinanohb81c3912022-09-08 04:13:24 -0400214 <div style={{ ...iconTextStyle }}>
215 <CrossIcon style={{ color: iconColor }} />
216 </div>
217 Fermer la conversation
218 </Stack>
219
220 <Stack
221 onClick={() => {
222 console.log("open details contact for: ");
223 closeModal();
224 openModalDetails();
225 getContactDetails();
226 }}
227 {...stackStyles}
228 >
229 <div style={{ ...iconTextStyle }}>
230 <ContactDetailsIcon style={{ color: iconColor }} />
231 </div>
232 Détails de la conversation
233 </Stack>
234
235 <Stack
236 onClick={() => {
237 setBlockOrRemove(true);
238 closeModal();
239 openModalDelete();
240 }}
241 {...stackStyles}
242 >
243 <div style={{ ...iconTextStyle }}>
244 <BlockContactIcon style={{ color: iconColor }} />
245 </div>
246 Bloquer le contact
247 </Stack>
248
249 <Stack
250 onClick={() => {
251 setBlockOrRemove(false);
252 closeModal();
253 openModalDelete();
254 }}
255 {...stackStyles}
256 >
257 <div style={{ ...iconTextStyle }}>
258 <RemoveContactIcon style={{ color: iconColor }} />
259 </div>
260 Supprimer contact
261 </Stack>
262 </Modal>
263 </div>
264
265 <div name="Contact details">
266 <Modal
267 isOpen={modalDetailsIsOpen}
268 onRequestClose={closeModalDetails}
269 style={contactDetailsStyles}
270 contentLabel="Détails contact"
271 >
272 <Stack direction={"row"} alignContent="flex-end">
273 <Stack direction={"column"}>
274 <div style={{ height: "100px" }}>
275 <ConversationAvatar
276 displayName={conversation.getDisplayNameNoFallback()}
277 />
278 </div>
279
280 <div
281 style={{
282 fontSize: "20px",
283 marginBottom: "20px",
284 height: "20px",
285 }}
286 >
287 Informations
288 </div>
289
290 <Typography variant="caption">Nom d'utilisateur</Typography>
291 <div style={{ height: "20px" }} />
292 <Typography variant="caption">Identifiant </Typography>
293 <div style={{ height: "20px" }} />
294
295 <div
296 style={{
297 flex: 1,
298 height: "150px",
299 direction: "column",
300 flexDirection: "column",
301 // alignSelf: "flex-end",
302 }}
303 >
304 <Typography variant="caption">Code QR</Typography>
305 </div>
306
307 <Typography variant="caption">est un swarm </Typography>
308 </Stack>
309
310 <Stack direction={"column"}>
311 <div
312 style={{
313 fontWeight: "bold",
314 fontSize: "20px",
315 height: "100px",
316 }}
317 >
318 {conversation.getDisplayNameNoFallback() + "(resolved name)"}
319 </div>
320
321 <div
322 style={{
323 height: "40px",
324 }}
325 />
326 <Typography variant="caption">
327 <div style={{ fontWeight: "bold" }}>
328 {conversation.getDisplayNameNoFallback()}
329 </div>
330 </Typography>
331
332 <div style={{ height: "20px" }} />
333
334 <Typography variant="caption">
335 <div style={{ fontWeight: "bold" }}> {userId}</div>
336 </Typography>
337
338 <div style={{ height: "20px" }} />
339
340 <div height={"40px"}>
341 <QRCodeCanvas value={`${userId}`} />
342 </div>
343
344 <Typography variant="caption">
345 <div style={{ fontWeight: "bold" }}> {isSwarm}</div>
346 </Typography>
347 </Stack>
348 </Stack>
349 <div
350 onClick={closeModalDetails}
351 style={{
352 width: "100px",
353 borderStyle: "solid",
354 textAlign: "center",
355 borderRadius: "5px",
356 marginLeft: "150px",
357 marginTop: "10px",
358 }}
359 >
360 <Typography variant="caption">Fermer</Typography>
ervinanoh8e918042022-09-06 10:30:59 -0400361 </div>
ervinanohb81c3912022-09-08 04:13:24 -0400362 </Modal>
363 </div>
idillon531b6f22022-09-16 14:02:00 -0400364
ervinanohb81c3912022-09-08 04:13:24 -0400365 <div name="Remove or block details">
366 <Modal
367 isOpen={modalDeleteIsOpen}
368 onRequestClose={closeModalDelete}
369 style={cancelStyles}
370 contentLabel="Merci de confirmer"
idillon531b6f22022-09-16 14:02:00 -0400371 >
ervinanohb81c3912022-09-08 04:13:24 -0400372 <Typography variant="h4">Merci de confirmer</Typography>
373 <Stack
374 direction={"column"}
375 justifyContent="space-around"
376 spacing={"75px"}
377 >
378 <div style={{ textAlign: "center", marginTop: "10%" }}>
379 <Typography variant="body2">
380 Voulez vous vraiment {blockOrRemove ? "bloquer" : "supprimer"}{" "}
381 ce contact?
382 </Typography>
383 </div>
ervinanoh8e918042022-09-06 10:30:59 -0400384
ervinanohb81c3912022-09-08 04:13:24 -0400385 <Stack
386 direction={"row"}
387 top={"25px"}
388 alignSelf="center"
389 spacing={1}
390 >
391 <Box
392 onClick={() => {
393 if (blockOrRemove) removeOrBlock("block");
394 else removeOrBlock("remove");
395 }}
396 style={{
397 width: "100px",
398 textAlign: "center",
399 borderStyle: "solid",
400 borderColor: "red",
401 borderRadius: "10px",
402 color: "red",
403 }}
404 >
405 {blockOrRemove ? "Bloquer" : "Supprimer"}
406 </Box>
407 <Box
408 onClick={closeModalDelete}
409 style={{
410 width: "100px",
411 textAlign: "center",
412 paddingLeft: "12px",
413 paddingRight: "12px",
414 borderStyle: "solid",
415 borderRadius: "10px",
416 }}
417 >
418 Annuler
419 </Box>
420 </Stack>
421 </Stack>
422 </Modal>
423 </div>
idillon531b6f22022-09-16 14:02:00 -0400424
425 <ListItem
426 button
427 alignItems="flex-start"
428 selected={isSelected}
ervinanohb81c3912022-09-08 04:13:24 -0400429 onClick={() =>
430 navigate(`/account/${conversation.getAccountId()}/${uri}`)
431 }
idillon531b6f22022-09-16 14:02:00 -0400432 >
433 <ListItemAvatar>
434 <ConversationAvatar
435 displayName={conversation.getDisplayNameNoFallback()}
436 />
437 </ListItemAvatar>
438 <ListItemText
439 primary={conversation.getDisplayName()}
440 secondary={conversation.getDisplayUri()}
441 />
442 </ListItem>
443 </div>
444 );
445 } else return null;
Adrien Béraud995e8022021-04-08 13:46:51 -0400446}