blob: 3bc040be78c8e79deb103aa12b7d63b9f3de3376 [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
simon5da8ca62022-11-09 15:21:25 -050024import { useAuthContext } from '../contexts/AuthProvider';
simond8ca2f22022-10-11 23:30:55 -040025import { useAppDispatch, useAppSelector } from '../redux/hooks';
simon5da8ca62022-11-09 15:21:25 -050026import { apiUrl } from '../utils/constants';
simond47ef9e2022-09-28 22:24:28 -040027import ConversationAvatar from './ConversationAvatar';
idillon531b6f22022-09-16 14:02:00 -040028
ervinanoh99655642022-09-01 15:11:31 -040029const customStyles = {
30 content: {
simond47ef9e2022-09-28 22:24:28 -040031 top: '50%',
32 left: '50%',
33 right: 'auto',
34 bottom: 'auto',
35 marginRight: '-50%',
36 transform: 'translate(-50%, -50%)',
ervinanoh99655642022-09-01 15:11:31 -040037 },
38};
Larbi Gharibe9af9732021-03-31 15:08:01 +010039
Adrien Béraud86986032021-04-25 12:04:53 -040040export default function ContactList() {
simon5da8ca62022-11-09 15:21:25 -050041 const { token } = useAuthContext();
simon416d0792022-11-03 02:46:18 -040042 const { accountId } = useAppSelector((state) => state.userInfo);
idillon531b6f22022-09-16 14:02:00 -040043 const dispatch = useAppDispatch();
Adrien Béraud995e8022021-04-08 13:46:51 -040044
idillon531b6f22022-09-16 14:02:00 -040045 const [contacts, setContacts] = useState([]);
ervinanoh99655642022-09-01 15:11:31 -040046 const [currentContact, setCurrentContact] = useState({});
47
48 const [modalIsOpen, setIsOpen] = useState(false);
49 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
50 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanoh8e918042022-09-06 10:30:59 -040051 const [blockOrRemove, setBlockOrRemove] = useState(true);
ervinanoh99655642022-09-01 15:11:31 -040052
53 const openModal = () => setIsOpen(true);
54 const openModalDetails = () => setModalDetailsIsOpen(true);
55 const openModalDelete = () => setModalDeleteIsOpen(true);
56
57 const closeModal = () => setIsOpen(false);
58
59 const closeModalDetails = () => setModalDetailsIsOpen(false);
60 const closeModalDelete = () => setModalDeleteIsOpen(false);
61
62 const getContactDetails = () => {
63 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050064 fetch(new URL(`/contacts/${currentContact.id}`, apiUrl), {
65 signal: controller.signal,
66 headers: {
67 Authorization: `Bearer ${token}`,
68 },
69 })
ervinanoh99655642022-09-01 15:11:31 -040070 .then((res) => res.json())
71 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -040072 console.log('CONTACT LIST - DETAILS: ', result);
ervinanoh99655642022-09-01 15:11:31 -040073 })
simond47ef9e2022-09-28 22:24:28 -040074 .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e));
ervinanoh99655642022-09-01 15:11:31 -040075 };
76
simon5da8ca62022-11-09 15:21:25 -050077 const removeOrBlock = (block = false) => {
simond47ef9e2022-09-28 22:24:28 -040078 console.log('REMOVE');
ervinanoh8e918042022-09-06 10:30:59 -040079 setBlockOrRemove(false);
80 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050081 let url = `/contacts/${currentContact.id}`;
82 if (block) {
83 url += '/block';
84 }
85 fetch(new URL(url, apiUrl), {
86 signal: controller.signal,
87 method: block ? 'POST' : 'DELETE',
88 headers: {
89 Authorization: `Bearer ${token}`,
90 },
91 })
ervinanoh8e918042022-09-06 10:30:59 -040092 .then((res) => res.json())
simon5da8ca62022-11-09 15:21:25 -050093 .catch((e) => console.log(`ERROR ${block ? 'blocking' : 'removing'} CONTACT : `, e));
ervinanoh8e918042022-09-06 10:30:59 -040094 closeModalDelete();
95 };
idillon531b6f22022-09-16 14:02:00 -040096
97 useEffect(() => {
98 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050099 fetch(new URL(`/contacts`, apiUrl), {
100 signal: controller.signal,
101 headers: {
102 Authorization: `Bearer ${token}`,
103 },
104 })
ervinanoh99655642022-09-01 15:11:31 -0400105 .then((res) => res.json())
idillon531b6f22022-09-16 14:02:00 -0400106 .then((result) => {
simond47ef9e2022-09-28 22:24:28 -0400107 console.log('CONTACTS: ', result);
ervinanoh99655642022-09-01 15:11:31 -0400108 setContacts(result);
idillon531b6f22022-09-16 14:02:00 -0400109 });
110 return () => controller.abort();
simon5da8ca62022-11-09 15:21:25 -0500111 }, [token]);
idillon531b6f22022-09-16 14:02:00 -0400112
113 return (
114 <div className="rooms-list">
ervinanoh99655642022-09-01 15:11:31 -0400115 <Modal
116 isOpen={modalIsOpen}
117 // onAfterOpen={afterOpenModal}
118 onRequestClose={closeModal}
119 style={customStyles}
120 contentLabel="Example Modal"
121 >
122 {/* <h2 ref={(_subtitle) => (subtitle = _subtitle)}>Hello</h2> */}
123 <button onClick={closeModal}>close</button>
124
125 {/* <div>
126 <Person /> Démarrer appel vidéo
127 </div>
128 <br />
129
130 <div>
131 <Person /> Démarrer appel audio
132 </div> */}
133 <br />
134
135 <div
136 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400137 console.log('open dialog Supprimer: ');
ervinanoh8e918042022-09-06 10:30:59 -0400138 setBlockOrRemove(false);
ervinanoh99655642022-09-01 15:11:31 -0400139 closeModal();
140 openModalDelete();
141 }}
142 >
143 <Person /> Supprimer contact
144 </div>
145 <br />
146
147 <div
148 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400149 console.log('open dialog BLOCK: ');
ervinanoh8e918042022-09-06 10:30:59 -0400150 setBlockOrRemove(true);
ervinanoh99655642022-09-01 15:11:31 -0400151 closeModal();
152 openModalDelete();
153 }}
154 >
155 <Person /> Bloquer le contact
156 </div>
157 <br />
158
159 <div
160 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400161 console.log('open details contact for: ');
ervinanoh99655642022-09-01 15:11:31 -0400162 closeModal();
163 openModalDetails();
164 getContactDetails();
165 }}
166 >
167 <Person /> Détails du contact
168 </div>
169 </Modal>
170 <Modal
171 isOpen={modalDeleteIsOpen}
172 // onAfterOpen={afterOpenModalDetails}
173 onRequestClose={closeModalDelete}
174 style={customStyles}
175 contentLabel="Merci de confirmer"
176 >
simond47ef9e2022-09-28 22:24:28 -0400177 Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact?
ervinanoh8e918042022-09-06 10:30:59 -0400178 <br />
179 {blockOrRemove ? (
simon5da8ca62022-11-09 15:21:25 -0500180 <button onClick={() => removeOrBlock(true)}>Bloquer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400181 ) : (
simon5da8ca62022-11-09 15:21:25 -0500182 <button onClick={() => removeOrBlock()}>Supprimer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400183 )}
ervinanoh99655642022-09-01 15:11:31 -0400184 <button onClick={closeModalDelete}>Annuler</button>
185 </Modal>
186
187 <List>
ervinanoh8e918042022-09-06 10:30:59 -0400188 {contacts?.map((contact) => (
ervinanoh99655642022-09-01 15:11:31 -0400189 <ListItem
190 button
191 alignItems="flex-start"
ervinanoh8e918042022-09-06 10:30:59 -0400192 key={contact.id}
ervinanoh99655642022-09-01 15:11:31 -0400193 // selected={isSelected}
194 onClick={() => {
195 setCurrentContact(contact);
196 openModal();
197 }}
198 >
199 <ListItemAvatar>
200 <ConversationAvatar
201 // displayName={conversation.getDisplayNameNoFallback()}
202 // displayName={`${contact.id}`}
203 />
204 </ListItemAvatar>
205 <ListItemText primary={contact.id} secondary={contact.id} />
206 </ListItem>
207 ))}
208 </List>
idillon531b6f22022-09-16 14:02:00 -0400209 </div>
210 );
Larbi Gharibe9af9732021-03-31 15:08:01 +0100211}