blob: 40057b3cca07a560be80a33f68b208c0119fabe2 [file] [log] [blame]
simon07b4eb02022-09-29 17:50:26 -04001import { Box, ListItem, ListItemAvatar, ListItemText, Stack, Typography } from '@mui/material';
simond47ef9e2022-09-28 22:24:28 -04002import { QRCodeCanvas } from 'qrcode.react';
simon07b4eb02022-09-29 17:50:26 -04003import { useState } from 'react';
4import Modal from 'react-modal';
5import { useNavigate, useParams } from 'react-router-dom';
6
7import Conversation from '../../../model/Conversation';
ervinanoh34eb9472022-09-13 04:20:28 -04008import { setRefreshFromSlice } from '../../redux/appSlice';
9import { useAppDispatch } from '../../redux/hooks';
simon07b4eb02022-09-29 17:50:26 -040010import authManager from '../AuthManager';
11import ConversationAvatar from './ConversationAvatar';
12import { RemoveContactIcon, VideoCallIcon } from './svgIcons';
13import { AudioCallIcon, BlockContactIcon, ContactDetailsIcon, CrossIcon, MessageIcon } from './svgIcons';
ervinanoh34eb9472022-09-13 04:20:28 -040014
idillon531b6f22022-09-16 14:02:00 -040015const 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",
simond47ef9e2022-09-28 22:24:28 -040024 left: '94px',
25 width: '180px',
26 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',
ervinanoh8e918042022-09-06 10:30:59 -040031
simond47ef9e2022-09-28 22:24:28 -040032 textAlign: 'left',
33 font: 'normal normal normal 12px/26px Ubuntu',
34 letterSpacing: '0px',
35 color: '#000000',
ervinanohb81c3912022-09-08 04:13:24 -040036 },
37};
38
39const cancelStyles = {
40 content: {
simond47ef9e2022-09-28 22:24:28 -040041 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',
ervinanohb81c3912022-09-08 04:13:24 -040048
simond47ef9e2022-09-28 22:24:28 -040049 textAlign: 'left',
50 font: 'normal normal normal 12px/26px Ubuntu',
51 letterSpacing: '0px',
52 color: '#000000',
ervinanohb81c3912022-09-08 04:13:24 -040053 },
54};
55
56const contactDetailsStyles = {
57 content: {
simond47ef9e2022-09-28 22:24:28 -040058 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',
ervinanohb81c3912022-09-08 04:13:24 -040065
simond47ef9e2022-09-28 22:24:28 -040066 textAlign: 'left',
67 font: 'normal normal normal 12px/26px Ubuntu',
68 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 = {
simond47ef9e2022-09-28 22:24:28 -040074 flexDirection: 'row',
75 marginBottom: '4px',
76 spacing: '40px',
ervinanoh8e918042022-09-06 10:30:59 -040077 flex: 1,
simond47ef9e2022-09-28 22:24:28 -040078 alignItems: 'center',
ervinanoh8e918042022-09-06 10:30:59 -040079};
80
ervinanohb81c3912022-09-08 04:13:24 -040081const iconTextStyle = {
simond47ef9e2022-09-28 22:24:28 -040082 marginRight: '10px',
ervinanohb81c3912022-09-08 04:13:24 -040083};
84
simond47ef9e2022-09-28 22:24:28 -040085const iconColor = '#005699';
ervinanohb81c3912022-09-08 04:13:24 -040086
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);
simond47ef9e2022-09-28 22:24:28 -0400101 const [userId, setUserId] = useState(conversation?.getFirstMember()?.contact.getUri());
102 const [isSwarm, setIsSwarm] = useState('true');
idillon531b6f22022-09-16 14:02:00 -0400103
ervinanohb81c3912022-09-08 04:13:24 -0400104 const openModal = (e) => {
105 e.preventDefault();
106 console.log(e);
107 setIsOpen(true);
108 };
idillon531b6f22022-09-16 14:02:00 -0400109 const openModalDetails = () => setModalDetailsIsOpen(true);
110 const openModalDelete = () => setModalDeleteIsOpen(true);
idillon531b6f22022-09-16 14:02:00 -0400111 const closeModal = () => setIsOpen(false);
idillon531b6f22022-09-16 14:02:00 -0400112 const closeModalDetails = () => setModalDetailsIsOpen(false);
113 const closeModalDelete = () => setModalDeleteIsOpen(false);
114
ervinanohb81c3912022-09-08 04:13:24 -0400115 let subtitle;
idillon531b6f22022-09-16 14:02:00 -0400116 function afterOpenModal() {
117 // references are now sync'd and can be accessed.
simond47ef9e2022-09-28 22:24:28 -0400118 subtitle.style.color = '#f00';
idillon531b6f22022-09-16 14:02:00 -0400119 }
120
ervinanohb81c3912022-09-08 04:13:24 -0400121 const getContactDetails = () => {
idillon531b6f22022-09-16 14:02:00 -0400122 const controller = new AbortController();
123 authManager
simond47ef9e2022-09-28 22:24:28 -0400124 .fetch(`/api/accounts/${conversation.getAccountId()}/contacts/details/${userId}`, {
125 signal: controller.signal,
126 })
ervinanohb81c3912022-09-08 04:13:24 -0400127 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -0400128 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -0400129 console.log('CONTACT LIST - DETAILS: ', result);
idillon531b6f22022-09-16 14:02:00 -0400130 })
simond47ef9e2022-09-28 22:24:28 -0400131 .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e));
idillon531b6f22022-09-16 14:02:00 -0400132 };
133
ervinanohb81c3912022-09-08 04:13:24 -0400134 const removeOrBlock = (typeOfRemove) => {
135 console.log(typeOfRemove);
136 setBlockOrRemove(false);
137
simond47ef9e2022-09-28 22:24:28 -0400138 console.log('EEEH', typeOfRemove, conversation.getAccountId(), userId);
ervinanohb81c3912022-09-08 04:13:24 -0400139
140 const controller = new AbortController();
141 authManager
simond47ef9e2022-09-28 22:24:28 -0400142 .fetch(`/api/accounts/${conversation.getAccountId()}/contacts/${typeOfRemove}/${userId}`, {
143 signal: controller.signal,
144 method: 'DELETE',
145 })
ervinanohb81c3912022-09-08 04:13:24 -0400146 .then((res) => res.json())
ervinanoh34eb9472022-09-13 04:20:28 -0400147 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -0400148 console.log('propre');
ervinanoh34eb9472022-09-13 04:20:28 -0400149 dispatch(setRefreshFromSlice());
150 })
151 .catch((e) => {
152 console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e);
153 dispatch(setRefreshFromSlice());
154 });
ervinanohb81c3912022-09-08 04:13:24 -0400155 closeModalDelete();
ervinanohb81c3912022-09-08 04:13:24 -0400156 };
ervinanohb81c3912022-09-08 04:13:24 -0400157
simond47ef9e2022-09-28 22:24:28 -0400158 const uri = conversation.getId() ? `conversation/${conversation.getId()}` : `addContact/${userId}`;
idillon531b6f22022-09-16 14:02:00 -0400159 if (conversation instanceof Conversation) {
160 return (
ervinanohb81c3912022-09-08 04:13:24 -0400161 <div onContextMenu={openModal}>
162 <div name="Modal conversation">
163 <Modal
164 isOpen={modalIsOpen}
165 // onAfterOpen={afterOpenModal}
166 onRequestClose={closeModal}
167 style={customStyles}
168 contentLabel="Example Modal"
idillon531b6f22022-09-16 14:02:00 -0400169 >
ervinanohb81c3912022-09-08 04:13:24 -0400170 <Stack
171 onClick={() => {
172 navigate(`/account/${conversation.getAccountId()}/${uri}`);
173 closeModal();
174 }}
175 {...stackStyles}
176 >
177 <div style={{ ...iconTextStyle }}>
178 <MessageIcon style={{ color: iconColor }} />
179 </div>
180 Message
181 </Stack>
182 <Stack {...stackStyles}>
183 <div style={{ ...iconTextStyle }}>
184 <AudioCallIcon style={{ color: iconColor }} />
185 </div>
186 Démarrer appel audio
187 </Stack>
188
189 <Stack {...stackStyles}>
190 <div style={{ ...iconTextStyle }}>
191 <VideoCallIcon style={{ color: iconColor }} />
192 </div>
193 Démarrer appel vidéo
194 </Stack>
195
196 <Stack
197 {...stackStyles}
198 onClick={() => {
199 navigate(`/account/${conversation.getAccountId()}/`);
200 closeModal();
ervinanoh8e918042022-09-06 10:30:59 -0400201 }}
202 >
ervinanohb81c3912022-09-08 04:13:24 -0400203 <div style={{ ...iconTextStyle }}>
204 <CrossIcon style={{ color: iconColor }} />
205 </div>
206 Fermer la conversation
207 </Stack>
208
209 <Stack
210 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400211 console.log('open details contact for: ');
ervinanohb81c3912022-09-08 04:13:24 -0400212 closeModal();
213 openModalDetails();
214 getContactDetails();
215 }}
216 {...stackStyles}
217 >
218 <div style={{ ...iconTextStyle }}>
219 <ContactDetailsIcon style={{ color: iconColor }} />
220 </div>
221 Détails de la conversation
222 </Stack>
223
224 <Stack
225 onClick={() => {
226 setBlockOrRemove(true);
227 closeModal();
228 openModalDelete();
229 }}
230 {...stackStyles}
231 >
232 <div style={{ ...iconTextStyle }}>
233 <BlockContactIcon style={{ color: iconColor }} />
234 </div>
235 Bloquer le contact
236 </Stack>
237
238 <Stack
239 onClick={() => {
240 setBlockOrRemove(false);
241 closeModal();
242 openModalDelete();
243 }}
244 {...stackStyles}
245 >
246 <div style={{ ...iconTextStyle }}>
247 <RemoveContactIcon style={{ color: iconColor }} />
248 </div>
249 Supprimer contact
250 </Stack>
251 </Modal>
252 </div>
253
254 <div name="Contact details">
255 <Modal
256 isOpen={modalDetailsIsOpen}
257 onRequestClose={closeModalDetails}
258 style={contactDetailsStyles}
259 contentLabel="Détails contact"
260 >
simond47ef9e2022-09-28 22:24:28 -0400261 <Stack direction={'row'} alignContent="flex-end">
262 <Stack direction={'column'}>
263 <div style={{ height: '100px' }}>
264 <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} />
ervinanohb81c3912022-09-08 04:13:24 -0400265 </div>
266
267 <div
268 style={{
simond47ef9e2022-09-28 22:24:28 -0400269 fontSize: '20px',
270 marginBottom: '20px',
271 height: '20px',
ervinanohb81c3912022-09-08 04:13:24 -0400272 }}
273 >
274 Informations
275 </div>
276
simon80b7b3b2022-09-28 17:50:10 -0400277 <Typography variant="caption">Nom d&apos;utilisateur</Typography>
simond47ef9e2022-09-28 22:24:28 -0400278 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400279 <Typography variant="caption">Identifiant </Typography>
simond47ef9e2022-09-28 22:24:28 -0400280 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400281
282 <div
283 style={{
284 flex: 1,
simond47ef9e2022-09-28 22:24:28 -0400285 height: '150px',
286 direction: 'column',
287 flexDirection: 'column',
ervinanohb81c3912022-09-08 04:13:24 -0400288 // alignSelf: "flex-end",
289 }}
290 >
291 <Typography variant="caption">Code QR</Typography>
292 </div>
293
294 <Typography variant="caption">est un swarm </Typography>
295 </Stack>
296
simond47ef9e2022-09-28 22:24:28 -0400297 <Stack direction={'column'}>
ervinanohb81c3912022-09-08 04:13:24 -0400298 <div
299 style={{
simond47ef9e2022-09-28 22:24:28 -0400300 fontWeight: 'bold',
301 fontSize: '20px',
302 height: '100px',
ervinanohb81c3912022-09-08 04:13:24 -0400303 }}
304 >
simond47ef9e2022-09-28 22:24:28 -0400305 {conversation.getDisplayNameNoFallback() + '(resolved name)'}
ervinanohb81c3912022-09-08 04:13:24 -0400306 </div>
307
308 <div
309 style={{
simond47ef9e2022-09-28 22:24:28 -0400310 height: '40px',
ervinanohb81c3912022-09-08 04:13:24 -0400311 }}
312 />
313 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400314 <div style={{ fontWeight: 'bold' }}>{conversation.getDisplayNameNoFallback()}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400315 </Typography>
316
simond47ef9e2022-09-28 22:24:28 -0400317 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400318
319 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400320 <div style={{ fontWeight: 'bold' }}> {userId}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400321 </Typography>
322
simond47ef9e2022-09-28 22:24:28 -0400323 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400324
simond47ef9e2022-09-28 22:24:28 -0400325 <div height={'40px'}>
ervinanohb81c3912022-09-08 04:13:24 -0400326 <QRCodeCanvas value={`${userId}`} />
327 </div>
328
329 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400330 <div style={{ fontWeight: 'bold' }}> {isSwarm}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400331 </Typography>
332 </Stack>
333 </Stack>
334 <div
335 onClick={closeModalDetails}
336 style={{
simond47ef9e2022-09-28 22:24:28 -0400337 width: '100px',
338 borderStyle: 'solid',
339 textAlign: 'center',
340 borderRadius: '5px',
341 marginLeft: '150px',
342 marginTop: '10px',
ervinanohb81c3912022-09-08 04:13:24 -0400343 }}
344 >
345 <Typography variant="caption">Fermer</Typography>
ervinanoh8e918042022-09-06 10:30:59 -0400346 </div>
ervinanohb81c3912022-09-08 04:13:24 -0400347 </Modal>
348 </div>
idillon531b6f22022-09-16 14:02:00 -0400349
ervinanohb81c3912022-09-08 04:13:24 -0400350 <div name="Remove or block details">
351 <Modal
352 isOpen={modalDeleteIsOpen}
353 onRequestClose={closeModalDelete}
354 style={cancelStyles}
355 contentLabel="Merci de confirmer"
idillon531b6f22022-09-16 14:02:00 -0400356 >
ervinanohb81c3912022-09-08 04:13:24 -0400357 <Typography variant="h4">Merci de confirmer</Typography>
simond47ef9e2022-09-28 22:24:28 -0400358 <Stack direction={'column'} justifyContent="space-around" spacing={'75px'}>
359 <div style={{ textAlign: 'center', marginTop: '10%' }}>
ervinanohb81c3912022-09-08 04:13:24 -0400360 <Typography variant="body2">
simond47ef9e2022-09-28 22:24:28 -0400361 Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact?
ervinanohb81c3912022-09-08 04:13:24 -0400362 </Typography>
363 </div>
ervinanoh8e918042022-09-06 10:30:59 -0400364
simond47ef9e2022-09-28 22:24:28 -0400365 <Stack direction={'row'} top={'25px'} alignSelf="center" spacing={1}>
ervinanohb81c3912022-09-08 04:13:24 -0400366 <Box
367 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400368 if (blockOrRemove) removeOrBlock('block');
369 else removeOrBlock('remove');
ervinanohb81c3912022-09-08 04:13:24 -0400370 }}
371 style={{
simond47ef9e2022-09-28 22:24:28 -0400372 width: '100px',
373 textAlign: 'center',
374 borderStyle: 'solid',
375 borderColor: 'red',
376 borderRadius: '10px',
377 color: 'red',
ervinanohb81c3912022-09-08 04:13:24 -0400378 }}
379 >
simond47ef9e2022-09-28 22:24:28 -0400380 {blockOrRemove ? 'Bloquer' : 'Supprimer'}
ervinanohb81c3912022-09-08 04:13:24 -0400381 </Box>
382 <Box
383 onClick={closeModalDelete}
384 style={{
simond47ef9e2022-09-28 22:24:28 -0400385 width: '100px',
386 textAlign: 'center',
387 paddingLeft: '12px',
388 paddingRight: '12px',
389 borderStyle: 'solid',
390 borderRadius: '10px',
ervinanohb81c3912022-09-08 04:13:24 -0400391 }}
392 >
393 Annuler
394 </Box>
395 </Stack>
396 </Stack>
397 </Modal>
398 </div>
idillon531b6f22022-09-16 14:02:00 -0400399
400 <ListItem
401 button
402 alignItems="flex-start"
403 selected={isSelected}
simond47ef9e2022-09-28 22:24:28 -0400404 onClick={() => navigate(`/account/${conversation.getAccountId()}/${uri}`)}
idillon531b6f22022-09-16 14:02:00 -0400405 >
406 <ListItemAvatar>
simond47ef9e2022-09-28 22:24:28 -0400407 <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} />
idillon531b6f22022-09-16 14:02:00 -0400408 </ListItemAvatar>
simond47ef9e2022-09-28 22:24:28 -0400409 <ListItemText primary={conversation.getDisplayName()} secondary={conversation.getDisplayUri()} />
idillon531b6f22022-09-16 14:02:00 -0400410 </ListItem>
411 </div>
412 );
413 } else return null;
Adrien Béraud995e8022021-04-08 13:46:51 -0400414}