blob: aab6dc10e28a0ed338123c1b977e2fefb895cc1c [file] [log] [blame]
idillon531b6f22022-09-16 14:02:00 -04001import Modal from 'react-modal';
simond47ef9e2022-09-28 22:24:28 -04002import authManager from '../AuthManager';
3import ConversationAvatar from './ConversationAvatar';
4import Conversation from '../../../model/Conversation';
5import { useState } from 'react';
6import { useNavigate, useParams } from 'react-router-dom';
7import { 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';
simond47ef9e2022-09-28 22:24:28 -040010import { QRCodeCanvas } from 'qrcode.react';
ervinanoh34eb9472022-09-13 04:20:28 -040011import { setRefreshFromSlice } from '../../redux/appSlice';
12import { useAppDispatch } from '../../redux/hooks';
13
idillon531b6f22022-09-16 14:02:00 -040014const customStyles = {
15 content: {
ervinanoh8e918042022-09-06 10:30:59 -040016 // right: "auto",
17 // bottom: "auto",
18 // // marginRight: "-50%",
19 // transform: "translate(-50%, -50%)",
ervinanoh8e918042022-09-06 10:30:59 -040020 // padding: "16px"
21
22 // top: "1364px",
simond47ef9e2022-09-28 22:24:28 -040023 left: '94px',
24 width: '180px',
25 height: '262px',
26 background: '#FFFFFF 0% 0% no-repeat padding-box',
27 boxShadow: '3px 3px 7px #00000029',
28 borderRadius: '5px 20px 20px 20px',
29 opacity: '1',
ervinanoh8e918042022-09-06 10:30:59 -040030
simond47ef9e2022-09-28 22:24:28 -040031 textAlign: 'left',
32 font: 'normal normal normal 12px/26px Ubuntu',
33 letterSpacing: '0px',
34 color: '#000000',
ervinanohb81c3912022-09-08 04:13:24 -040035 },
36};
37
38const cancelStyles = {
39 content: {
simond47ef9e2022-09-28 22:24:28 -040040 left: '94px',
41 width: '300px',
42 height: '220px',
43 background: '#FFFFFF 0% 0% no-repeat padding-box',
44 boxShadow: '3px 3px 7px #00000029',
45 borderRadius: '20px',
46 opacity: '1',
ervinanohb81c3912022-09-08 04:13:24 -040047
simond47ef9e2022-09-28 22:24:28 -040048 textAlign: 'left',
49 font: 'normal normal normal 12px/26px Ubuntu',
50 letterSpacing: '0px',
51 color: '#000000',
ervinanohb81c3912022-09-08 04:13:24 -040052 },
53};
54
55const contactDetailsStyles = {
56 content: {
simond47ef9e2022-09-28 22:24:28 -040057 left: '94px',
58 width: '450px',
59 height: '450px',
60 background: '#FFFFFF 0% 0% no-repeat padding-box',
61 boxShadow: '3px 3px 7px #00000029',
62 borderRadius: '20px',
63 opacity: '1',
ervinanohb81c3912022-09-08 04:13:24 -040064
simond47ef9e2022-09-28 22:24:28 -040065 textAlign: 'left',
66 font: 'normal normal normal 12px/26px Ubuntu',
67 letterSpacing: '0px',
68 color: '#000000',
idillon531b6f22022-09-16 14:02:00 -040069 },
70};
Adrien Béraud995e8022021-04-08 13:46:51 -040071
ervinanoh8e918042022-09-06 10:30:59 -040072const stackStyles = {
simond47ef9e2022-09-28 22:24:28 -040073 flexDirection: 'row',
74 marginBottom: '4px',
75 spacing: '40px',
ervinanoh8e918042022-09-06 10:30:59 -040076 flex: 1,
simond47ef9e2022-09-28 22:24:28 -040077 alignItems: 'center',
ervinanoh8e918042022-09-06 10:30:59 -040078};
79
ervinanohb81c3912022-09-08 04:13:24 -040080const iconTextStyle = {
simond47ef9e2022-09-28 22:24:28 -040081 marginRight: '10px',
ervinanohb81c3912022-09-08 04:13:24 -040082};
83
simond47ef9e2022-09-28 22:24:28 -040084const iconColor = '#005699';
ervinanohb81c3912022-09-08 04:13:24 -040085
Adrien Béraudaf09a462021-04-15 18:02:29 -040086export default function ConversationListItem(props) {
idillon531b6f22022-09-16 14:02:00 -040087 const { conversationId, contactId } = useParams();
ervinanoh34eb9472022-09-13 04:20:28 -040088 const dispatch = useAppDispatch();
89
idillon531b6f22022-09-16 14:02:00 -040090 const conversation = props.conversation;
Adrien Béraud995e8022021-04-08 13:46:51 -040091
idillon531b6f22022-09-16 14:02:00 -040092 const pathId = conversationId || contactId;
93 const isSelected = conversation.getDisplayUri() === pathId;
94 const navigate = useNavigate();
95
idillon531b6f22022-09-16 14:02:00 -040096 const [modalIsOpen, setIsOpen] = useState(false);
97 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
98 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanohb81c3912022-09-08 04:13:24 -040099 const [blockOrRemove, setBlockOrRemove] = useState(true);
simond47ef9e2022-09-28 22:24:28 -0400100 const [userId, setUserId] = useState(conversation?.getFirstMember()?.contact.getUri());
101 const [isSwarm, setIsSwarm] = useState('true');
idillon531b6f22022-09-16 14:02:00 -0400102
ervinanohb81c3912022-09-08 04:13:24 -0400103 const openModal = (e) => {
104 e.preventDefault();
105 console.log(e);
106 setIsOpen(true);
107 };
idillon531b6f22022-09-16 14:02:00 -0400108 const openModalDetails = () => setModalDetailsIsOpen(true);
109 const openModalDelete = () => setModalDeleteIsOpen(true);
idillon531b6f22022-09-16 14:02:00 -0400110 const closeModal = () => setIsOpen(false);
idillon531b6f22022-09-16 14:02:00 -0400111 const closeModalDetails = () => setModalDetailsIsOpen(false);
112 const closeModalDelete = () => setModalDeleteIsOpen(false);
113
ervinanohb81c3912022-09-08 04:13:24 -0400114 let subtitle;
idillon531b6f22022-09-16 14:02:00 -0400115 function afterOpenModal() {
116 // references are now sync'd and can be accessed.
simond47ef9e2022-09-28 22:24:28 -0400117 subtitle.style.color = '#f00';
idillon531b6f22022-09-16 14:02:00 -0400118 }
119
ervinanohb81c3912022-09-08 04:13:24 -0400120 const getContactDetails = () => {
idillon531b6f22022-09-16 14:02:00 -0400121 const controller = new AbortController();
122 authManager
simond47ef9e2022-09-28 22:24:28 -0400123 .fetch(`/api/accounts/${conversation.getAccountId()}/contacts/details/${userId}`, {
124 signal: controller.signal,
125 })
ervinanohb81c3912022-09-08 04:13:24 -0400126 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -0400127 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -0400128 console.log('CONTACT LIST - DETAILS: ', result);
idillon531b6f22022-09-16 14:02:00 -0400129 })
simond47ef9e2022-09-28 22:24:28 -0400130 .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e));
idillon531b6f22022-09-16 14:02:00 -0400131 };
132
ervinanohb81c3912022-09-08 04:13:24 -0400133 const removeOrBlock = (typeOfRemove) => {
134 console.log(typeOfRemove);
135 setBlockOrRemove(false);
136
simond47ef9e2022-09-28 22:24:28 -0400137 console.log('EEEH', typeOfRemove, conversation.getAccountId(), userId);
ervinanohb81c3912022-09-08 04:13:24 -0400138
139 const controller = new AbortController();
140 authManager
simond47ef9e2022-09-28 22:24:28 -0400141 .fetch(`/api/accounts/${conversation.getAccountId()}/contacts/${typeOfRemove}/${userId}`, {
142 signal: controller.signal,
143 method: 'DELETE',
144 })
ervinanohb81c3912022-09-08 04:13:24 -0400145 .then((res) => res.json())
ervinanoh34eb9472022-09-13 04:20:28 -0400146 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -0400147 console.log('propre');
ervinanoh34eb9472022-09-13 04:20:28 -0400148 dispatch(setRefreshFromSlice());
149 })
150 .catch((e) => {
151 console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e);
152 dispatch(setRefreshFromSlice());
153 });
ervinanohb81c3912022-09-08 04:13:24 -0400154 closeModalDelete();
ervinanohb81c3912022-09-08 04:13:24 -0400155 };
ervinanohb81c3912022-09-08 04:13:24 -0400156
simond47ef9e2022-09-28 22:24:28 -0400157 const uri = conversation.getId() ? `conversation/${conversation.getId()}` : `addContact/${userId}`;
idillon531b6f22022-09-16 14:02:00 -0400158 if (conversation instanceof Conversation) {
159 return (
ervinanohb81c3912022-09-08 04:13:24 -0400160 <div onContextMenu={openModal}>
161 <div name="Modal conversation">
162 <Modal
163 isOpen={modalIsOpen}
164 // onAfterOpen={afterOpenModal}
165 onRequestClose={closeModal}
166 style={customStyles}
167 contentLabel="Example Modal"
idillon531b6f22022-09-16 14:02:00 -0400168 >
ervinanohb81c3912022-09-08 04:13:24 -0400169 <Stack
170 onClick={() => {
171 navigate(`/account/${conversation.getAccountId()}/${uri}`);
172 closeModal();
173 }}
174 {...stackStyles}
175 >
176 <div style={{ ...iconTextStyle }}>
177 <MessageIcon style={{ color: iconColor }} />
178 </div>
179 Message
180 </Stack>
181 <Stack {...stackStyles}>
182 <div style={{ ...iconTextStyle }}>
183 <AudioCallIcon style={{ color: iconColor }} />
184 </div>
185 Démarrer appel audio
186 </Stack>
187
188 <Stack {...stackStyles}>
189 <div style={{ ...iconTextStyle }}>
190 <VideoCallIcon style={{ color: iconColor }} />
191 </div>
192 Démarrer appel vidéo
193 </Stack>
194
195 <Stack
196 {...stackStyles}
197 onClick={() => {
198 navigate(`/account/${conversation.getAccountId()}/`);
199 closeModal();
ervinanoh8e918042022-09-06 10:30:59 -0400200 }}
201 >
ervinanohb81c3912022-09-08 04:13:24 -0400202 <div style={{ ...iconTextStyle }}>
203 <CrossIcon style={{ color: iconColor }} />
204 </div>
205 Fermer la conversation
206 </Stack>
207
208 <Stack
209 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400210 console.log('open details contact for: ');
ervinanohb81c3912022-09-08 04:13:24 -0400211 closeModal();
212 openModalDetails();
213 getContactDetails();
214 }}
215 {...stackStyles}
216 >
217 <div style={{ ...iconTextStyle }}>
218 <ContactDetailsIcon style={{ color: iconColor }} />
219 </div>
220 Détails de la conversation
221 </Stack>
222
223 <Stack
224 onClick={() => {
225 setBlockOrRemove(true);
226 closeModal();
227 openModalDelete();
228 }}
229 {...stackStyles}
230 >
231 <div style={{ ...iconTextStyle }}>
232 <BlockContactIcon style={{ color: iconColor }} />
233 </div>
234 Bloquer le contact
235 </Stack>
236
237 <Stack
238 onClick={() => {
239 setBlockOrRemove(false);
240 closeModal();
241 openModalDelete();
242 }}
243 {...stackStyles}
244 >
245 <div style={{ ...iconTextStyle }}>
246 <RemoveContactIcon style={{ color: iconColor }} />
247 </div>
248 Supprimer contact
249 </Stack>
250 </Modal>
251 </div>
252
253 <div name="Contact details">
254 <Modal
255 isOpen={modalDetailsIsOpen}
256 onRequestClose={closeModalDetails}
257 style={contactDetailsStyles}
258 contentLabel="Détails contact"
259 >
simond47ef9e2022-09-28 22:24:28 -0400260 <Stack direction={'row'} alignContent="flex-end">
261 <Stack direction={'column'}>
262 <div style={{ height: '100px' }}>
263 <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} />
ervinanohb81c3912022-09-08 04:13:24 -0400264 </div>
265
266 <div
267 style={{
simond47ef9e2022-09-28 22:24:28 -0400268 fontSize: '20px',
269 marginBottom: '20px',
270 height: '20px',
ervinanohb81c3912022-09-08 04:13:24 -0400271 }}
272 >
273 Informations
274 </div>
275
276 <Typography variant="caption">Nom d'utilisateur</Typography>
simond47ef9e2022-09-28 22:24:28 -0400277 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400278 <Typography variant="caption">Identifiant </Typography>
simond47ef9e2022-09-28 22:24:28 -0400279 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400280
281 <div
282 style={{
283 flex: 1,
simond47ef9e2022-09-28 22:24:28 -0400284 height: '150px',
285 direction: 'column',
286 flexDirection: 'column',
ervinanohb81c3912022-09-08 04:13:24 -0400287 // alignSelf: "flex-end",
288 }}
289 >
290 <Typography variant="caption">Code QR</Typography>
291 </div>
292
293 <Typography variant="caption">est un swarm </Typography>
294 </Stack>
295
simond47ef9e2022-09-28 22:24:28 -0400296 <Stack direction={'column'}>
ervinanohb81c3912022-09-08 04:13:24 -0400297 <div
298 style={{
simond47ef9e2022-09-28 22:24:28 -0400299 fontWeight: 'bold',
300 fontSize: '20px',
301 height: '100px',
ervinanohb81c3912022-09-08 04:13:24 -0400302 }}
303 >
simond47ef9e2022-09-28 22:24:28 -0400304 {conversation.getDisplayNameNoFallback() + '(resolved name)'}
ervinanohb81c3912022-09-08 04:13:24 -0400305 </div>
306
307 <div
308 style={{
simond47ef9e2022-09-28 22:24:28 -0400309 height: '40px',
ervinanohb81c3912022-09-08 04:13:24 -0400310 }}
311 />
312 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400313 <div style={{ fontWeight: 'bold' }}>{conversation.getDisplayNameNoFallback()}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400314 </Typography>
315
simond47ef9e2022-09-28 22:24:28 -0400316 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400317
318 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400319 <div style={{ fontWeight: 'bold' }}> {userId}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400320 </Typography>
321
simond47ef9e2022-09-28 22:24:28 -0400322 <div style={{ height: '20px' }} />
ervinanohb81c3912022-09-08 04:13:24 -0400323
simond47ef9e2022-09-28 22:24:28 -0400324 <div height={'40px'}>
ervinanohb81c3912022-09-08 04:13:24 -0400325 <QRCodeCanvas value={`${userId}`} />
326 </div>
327
328 <Typography variant="caption">
simond47ef9e2022-09-28 22:24:28 -0400329 <div style={{ fontWeight: 'bold' }}> {isSwarm}</div>
ervinanohb81c3912022-09-08 04:13:24 -0400330 </Typography>
331 </Stack>
332 </Stack>
333 <div
334 onClick={closeModalDetails}
335 style={{
simond47ef9e2022-09-28 22:24:28 -0400336 width: '100px',
337 borderStyle: 'solid',
338 textAlign: 'center',
339 borderRadius: '5px',
340 marginLeft: '150px',
341 marginTop: '10px',
ervinanohb81c3912022-09-08 04:13:24 -0400342 }}
343 >
344 <Typography variant="caption">Fermer</Typography>
ervinanoh8e918042022-09-06 10:30:59 -0400345 </div>
ervinanohb81c3912022-09-08 04:13:24 -0400346 </Modal>
347 </div>
idillon531b6f22022-09-16 14:02:00 -0400348
ervinanohb81c3912022-09-08 04:13:24 -0400349 <div name="Remove or block details">
350 <Modal
351 isOpen={modalDeleteIsOpen}
352 onRequestClose={closeModalDelete}
353 style={cancelStyles}
354 contentLabel="Merci de confirmer"
idillon531b6f22022-09-16 14:02:00 -0400355 >
ervinanohb81c3912022-09-08 04:13:24 -0400356 <Typography variant="h4">Merci de confirmer</Typography>
simond47ef9e2022-09-28 22:24:28 -0400357 <Stack direction={'column'} justifyContent="space-around" spacing={'75px'}>
358 <div style={{ textAlign: 'center', marginTop: '10%' }}>
ervinanohb81c3912022-09-08 04:13:24 -0400359 <Typography variant="body2">
simond47ef9e2022-09-28 22:24:28 -0400360 Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact?
ervinanohb81c3912022-09-08 04:13:24 -0400361 </Typography>
362 </div>
ervinanoh8e918042022-09-06 10:30:59 -0400363
simond47ef9e2022-09-28 22:24:28 -0400364 <Stack direction={'row'} top={'25px'} alignSelf="center" spacing={1}>
ervinanohb81c3912022-09-08 04:13:24 -0400365 <Box
366 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400367 if (blockOrRemove) removeOrBlock('block');
368 else removeOrBlock('remove');
ervinanohb81c3912022-09-08 04:13:24 -0400369 }}
370 style={{
simond47ef9e2022-09-28 22:24:28 -0400371 width: '100px',
372 textAlign: 'center',
373 borderStyle: 'solid',
374 borderColor: 'red',
375 borderRadius: '10px',
376 color: 'red',
ervinanohb81c3912022-09-08 04:13:24 -0400377 }}
378 >
simond47ef9e2022-09-28 22:24:28 -0400379 {blockOrRemove ? 'Bloquer' : 'Supprimer'}
ervinanohb81c3912022-09-08 04:13:24 -0400380 </Box>
381 <Box
382 onClick={closeModalDelete}
383 style={{
simond47ef9e2022-09-28 22:24:28 -0400384 width: '100px',
385 textAlign: 'center',
386 paddingLeft: '12px',
387 paddingRight: '12px',
388 borderStyle: 'solid',
389 borderRadius: '10px',
ervinanohb81c3912022-09-08 04:13:24 -0400390 }}
391 >
392 Annuler
393 </Box>
394 </Stack>
395 </Stack>
396 </Modal>
397 </div>
idillon531b6f22022-09-16 14:02:00 -0400398
399 <ListItem
400 button
401 alignItems="flex-start"
402 selected={isSelected}
simond47ef9e2022-09-28 22:24:28 -0400403 onClick={() => navigate(`/account/${conversation.getAccountId()}/${uri}`)}
idillon531b6f22022-09-16 14:02:00 -0400404 >
405 <ListItemAvatar>
simond47ef9e2022-09-28 22:24:28 -0400406 <ConversationAvatar displayName={conversation.getDisplayNameNoFallback()} />
idillon531b6f22022-09-16 14:02:00 -0400407 </ListItemAvatar>
simond47ef9e2022-09-28 22:24:28 -0400408 <ListItemText primary={conversation.getDisplayName()} secondary={conversation.getDisplayUri()} />
idillon531b6f22022-09-16 14:02:00 -0400409 </ListItem>
410 </div>
411 );
412 } else return null;
Adrien Béraud995e8022021-04-08 13:46:51 -0400413}