blob: c35c4f019ec2c7c26a0b1356460955cda88af518 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simon07b4eb02022-09-29 17:50:26 -040018import { Person } from '@mui/icons-material';
19import { ListItem, ListItemAvatar, ListItemText } from '@mui/material';
simond47ef9e2022-09-28 22:24:28 -040020import List from '@mui/material/List';
simon07b4eb02022-09-29 17:50:26 -040021import { useEffect, useState } from 'react';
22import Modal from 'react-modal';
23
simond47ef9e2022-09-28 22:24:28 -040024import authManager from '../AuthManager';
simond8ca2f22022-10-11 23:30:55 -040025import { useAppDispatch, useAppSelector } from '../redux/hooks';
simond47ef9e2022-09-28 22:24:28 -040026import ConversationAvatar from './ConversationAvatar';
idillon531b6f22022-09-16 14:02:00 -040027
ervinanoh99655642022-09-01 15:11:31 -040028const customStyles = {
29 content: {
simond47ef9e2022-09-28 22:24:28 -040030 top: '50%',
31 left: '50%',
32 right: 'auto',
33 bottom: 'auto',
34 marginRight: '-50%',
35 transform: 'translate(-50%, -50%)',
ervinanoh99655642022-09-01 15:11:31 -040036 },
37};
Larbi Gharibe9af9732021-03-31 15:08:01 +010038
Adrien Béraud86986032021-04-25 12:04:53 -040039export default function ContactList() {
simon416d0792022-11-03 02:46:18 -040040 const { accountId } = useAppSelector((state) => state.userInfo);
idillon531b6f22022-09-16 14:02:00 -040041 const dispatch = useAppDispatch();
Adrien Béraud995e8022021-04-08 13:46:51 -040042
idillon531b6f22022-09-16 14:02:00 -040043 const [contacts, setContacts] = useState([]);
ervinanoh99655642022-09-01 15:11:31 -040044 const [currentContact, setCurrentContact] = useState({});
45
46 const [modalIsOpen, setIsOpen] = useState(false);
47 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
48 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanoh8e918042022-09-06 10:30:59 -040049 const [blockOrRemove, setBlockOrRemove] = useState(true);
ervinanoh99655642022-09-01 15:11:31 -040050
51 const openModal = () => setIsOpen(true);
52 const openModalDetails = () => setModalDetailsIsOpen(true);
53 const openModalDelete = () => setModalDeleteIsOpen(true);
54
55 const closeModal = () => setIsOpen(false);
56
57 const closeModalDetails = () => setModalDetailsIsOpen(false);
58 const closeModalDelete = () => setModalDeleteIsOpen(false);
59
60 const getContactDetails = () => {
61 const controller = new AbortController();
62 authManager
ervinanoh8e918042022-09-06 10:30:59 -040063 .fetch(`/api/accounts/${accountId}/contacts/details/${currentContact.id}`, {
ervinanoh99655642022-09-01 15:11:31 -040064 signal: controller.signal,
65 })
66 .then((res) => res.json())
67 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -040068 console.log('CONTACT LIST - DETAILS: ', result);
ervinanoh99655642022-09-01 15:11:31 -040069 })
simond47ef9e2022-09-28 22:24:28 -040070 .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e));
ervinanoh99655642022-09-01 15:11:31 -040071 };
72
ervinanoh8e918042022-09-06 10:30:59 -040073 const removeOrBlock = (typeOfRemove) => {
simond47ef9e2022-09-28 22:24:28 -040074 console.log('REMOVE');
ervinanoh8e918042022-09-06 10:30:59 -040075 setBlockOrRemove(false);
76 const controller = new AbortController();
77 authManager
simond47ef9e2022-09-28 22:24:28 -040078 .fetch(`/api/accounts/${accountId}/contacts/${typeOfRemove}/${currentContact.id}`, {
79 signal: controller.signal,
80 method: 'DELETE',
81 })
ervinanoh8e918042022-09-06 10:30:59 -040082 .then((res) => res.json())
ervinanoh8e918042022-09-06 10:30:59 -040083 .catch((e) => console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e));
84 closeModalDelete();
85 };
idillon531b6f22022-09-16 14:02:00 -040086
87 useEffect(() => {
88 const controller = new AbortController();
89 authManager
ervinanoh8e918042022-09-06 10:30:59 -040090 .fetch(`/api/accounts/${accountId}/contacts`, {
ervinanoh99655642022-09-01 15:11:31 -040091 signal: controller.signal,
idillon531b6f22022-09-16 14:02:00 -040092 })
ervinanoh99655642022-09-01 15:11:31 -040093 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -040094 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -040095 console.log('CONTACTS: ', result);
ervinanoh99655642022-09-01 15:11:31 -040096 setContacts(result);
idillon531b6f22022-09-16 14:02:00 -040097 });
98 return () => controller.abort();
simon80b7b3b2022-09-28 17:50:10 -040099 }, [accountId, blockOrRemove]);
idillon531b6f22022-09-16 14:02:00 -0400100
101 return (
102 <div className="rooms-list">
ervinanoh99655642022-09-01 15:11:31 -0400103 <Modal
104 isOpen={modalIsOpen}
105 // onAfterOpen={afterOpenModal}
106 onRequestClose={closeModal}
107 style={customStyles}
108 contentLabel="Example Modal"
109 >
110 {/* <h2 ref={(_subtitle) => (subtitle = _subtitle)}>Hello</h2> */}
111 <button onClick={closeModal}>close</button>
112
113 {/* <div>
114 <Person /> Démarrer appel vidéo
115 </div>
116 <br />
117
118 <div>
119 <Person /> Démarrer appel audio
120 </div> */}
121 <br />
122
123 <div
124 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400125 console.log('open dialog Supprimer: ');
ervinanoh8e918042022-09-06 10:30:59 -0400126 setBlockOrRemove(false);
ervinanoh99655642022-09-01 15:11:31 -0400127 closeModal();
128 openModalDelete();
129 }}
130 >
131 <Person /> Supprimer contact
132 </div>
133 <br />
134
135 <div
136 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400137 console.log('open dialog BLOCK: ');
ervinanoh8e918042022-09-06 10:30:59 -0400138 setBlockOrRemove(true);
ervinanoh99655642022-09-01 15:11:31 -0400139 closeModal();
140 openModalDelete();
141 }}
142 >
143 <Person /> Bloquer le contact
144 </div>
145 <br />
146
147 <div
148 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400149 console.log('open details contact for: ');
ervinanoh99655642022-09-01 15:11:31 -0400150 closeModal();
151 openModalDetails();
152 getContactDetails();
153 }}
154 >
155 <Person /> Détails du contact
156 </div>
157 </Modal>
158 <Modal
159 isOpen={modalDeleteIsOpen}
160 // onAfterOpen={afterOpenModalDetails}
161 onRequestClose={closeModalDelete}
162 style={customStyles}
163 contentLabel="Merci de confirmer"
164 >
simond47ef9e2022-09-28 22:24:28 -0400165 Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact?
ervinanoh8e918042022-09-06 10:30:59 -0400166 <br />
167 {blockOrRemove ? (
simond47ef9e2022-09-28 22:24:28 -0400168 <button onClick={() => removeOrBlock('block')}>Bloquer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400169 ) : (
simond47ef9e2022-09-28 22:24:28 -0400170 <button onClick={() => removeOrBlock('remove')}>Supprimer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400171 )}
ervinanoh99655642022-09-01 15:11:31 -0400172 <button onClick={closeModalDelete}>Annuler</button>
173 </Modal>
174
175 <List>
ervinanoh8e918042022-09-06 10:30:59 -0400176 {contacts?.map((contact) => (
ervinanoh99655642022-09-01 15:11:31 -0400177 <ListItem
178 button
179 alignItems="flex-start"
ervinanoh8e918042022-09-06 10:30:59 -0400180 key={contact.id}
ervinanoh99655642022-09-01 15:11:31 -0400181 // selected={isSelected}
182 onClick={() => {
183 setCurrentContact(contact);
184 openModal();
185 }}
186 >
187 <ListItemAvatar>
188 <ConversationAvatar
189 // displayName={conversation.getDisplayNameNoFallback()}
190 // displayName={`${contact.id}`}
191 />
192 </ListItemAvatar>
193 <ListItemText primary={contact.id} secondary={contact.id} />
194 </ListItem>
195 ))}
196 </List>
idillon531b6f22022-09-16 14:02:00 -0400197 </div>
198 );
Larbi Gharibe9af9732021-03-31 15:08:01 +0100199}