blob: 1f980917e05dd6655978817034b379ba92cd6595 [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';
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() {
simon94fe53e2022-11-10 12:51:58 -050040 const { axiosInstance } = useAuthContext();
simon416d0792022-11-03 02:46:18 -040041 const { accountId } = useAppSelector((state) => state.userInfo);
idillon531b6f22022-09-16 14:02:00 -040042 const dispatch = useAppDispatch();
Adrien Béraud995e8022021-04-08 13:46:51 -040043
idillon531b6f22022-09-16 14:02:00 -040044 const [contacts, setContacts] = useState([]);
ervinanoh99655642022-09-01 15:11:31 -040045 const [currentContact, setCurrentContact] = useState({});
46
47 const [modalIsOpen, setIsOpen] = useState(false);
48 const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
49 const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false);
ervinanoh8e918042022-09-06 10:30:59 -040050 const [blockOrRemove, setBlockOrRemove] = useState(true);
ervinanoh99655642022-09-01 15:11:31 -040051
52 const openModal = () => setIsOpen(true);
53 const openModalDetails = () => setModalDetailsIsOpen(true);
54 const openModalDelete = () => setModalDeleteIsOpen(true);
55
56 const closeModal = () => setIsOpen(false);
57
58 const closeModalDetails = () => setModalDetailsIsOpen(false);
59 const closeModalDelete = () => setModalDeleteIsOpen(false);
60
simon94fe53e2022-11-10 12:51:58 -050061 const getContactDetails = async () => {
ervinanoh99655642022-09-01 15:11:31 -040062 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050063 try {
Misha Krieger-Raynauld46e9d242022-11-12 18:02:43 -050064 const { data } = await axiosInstance.get(`/contacts/${currentContact.id}`, {
simon94fe53e2022-11-10 12:51:58 -050065 signal: controller.signal,
66 });
67 console.log('CONTACT LIST - DETAILS: ', data);
68 } catch (e) {
69 console.log('ERROR GET CONTACT DETAILS: ', e);
70 }
ervinanoh99655642022-09-01 15:11:31 -040071 };
72
simon94fe53e2022-11-10 12:51:58 -050073 const removeOrBlock = async (block = false) => {
simond47ef9e2022-09-28 22:24:28 -040074 console.log('REMOVE');
ervinanoh8e918042022-09-06 10:30:59 -040075 setBlockOrRemove(false);
76 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050077 let url = `/contacts/${currentContact.id}`;
78 if (block) {
79 url += '/block';
80 }
simon94fe53e2022-11-10 12:51:58 -050081 try {
82 await axiosInstance(url, {
83 signal: controller.signal,
84 method: block ? 'POST' : 'DELETE',
85 });
86 } catch (e) {
87 console.log(`ERROR ${block ? 'blocking' : 'removing'} CONTACT : `, e);
88 }
ervinanoh8e918042022-09-06 10:30:59 -040089 closeModalDelete();
90 };
idillon531b6f22022-09-16 14:02:00 -040091
92 useEffect(() => {
93 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050094 axiosInstance
95 .get(`/contacts`, {
96 signal: controller.signal,
97 })
98 .then(({ data }) => {
99 console.log('CONTACTS: ', data);
100 setContacts(data);
idillon531b6f22022-09-16 14:02:00 -0400101 });
102 return () => controller.abort();
simon94fe53e2022-11-10 12:51:58 -0500103 }, [axiosInstance]);
idillon531b6f22022-09-16 14:02:00 -0400104
105 return (
106 <div className="rooms-list">
ervinanoh99655642022-09-01 15:11:31 -0400107 <Modal
108 isOpen={modalIsOpen}
109 // onAfterOpen={afterOpenModal}
110 onRequestClose={closeModal}
111 style={customStyles}
112 contentLabel="Example Modal"
113 >
114 {/* <h2 ref={(_subtitle) => (subtitle = _subtitle)}>Hello</h2> */}
115 <button onClick={closeModal}>close</button>
116
117 {/* <div>
118 <Person /> Démarrer appel vidéo
119 </div>
120 <br />
121
122 <div>
123 <Person /> Démarrer appel audio
124 </div> */}
125 <br />
126
127 <div
128 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400129 console.log('open dialog Supprimer: ');
ervinanoh8e918042022-09-06 10:30:59 -0400130 setBlockOrRemove(false);
ervinanoh99655642022-09-01 15:11:31 -0400131 closeModal();
132 openModalDelete();
133 }}
134 >
135 <Person /> Supprimer contact
136 </div>
137 <br />
138
139 <div
140 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400141 console.log('open dialog BLOCK: ');
ervinanoh8e918042022-09-06 10:30:59 -0400142 setBlockOrRemove(true);
ervinanoh99655642022-09-01 15:11:31 -0400143 closeModal();
144 openModalDelete();
145 }}
146 >
147 <Person /> Bloquer le contact
148 </div>
149 <br />
150
151 <div
152 onClick={() => {
simond47ef9e2022-09-28 22:24:28 -0400153 console.log('open details contact for: ');
ervinanoh99655642022-09-01 15:11:31 -0400154 closeModal();
155 openModalDetails();
156 getContactDetails();
157 }}
158 >
159 <Person /> Détails du contact
160 </div>
161 </Modal>
162 <Modal
163 isOpen={modalDeleteIsOpen}
164 // onAfterOpen={afterOpenModalDetails}
165 onRequestClose={closeModalDelete}
166 style={customStyles}
167 contentLabel="Merci de confirmer"
168 >
simond47ef9e2022-09-28 22:24:28 -0400169 Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact?
ervinanoh8e918042022-09-06 10:30:59 -0400170 <br />
171 {blockOrRemove ? (
simon5da8ca62022-11-09 15:21:25 -0500172 <button onClick={() => removeOrBlock(true)}>Bloquer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400173 ) : (
simon5da8ca62022-11-09 15:21:25 -0500174 <button onClick={() => removeOrBlock()}>Supprimer</button>
ervinanoh8e918042022-09-06 10:30:59 -0400175 )}
ervinanoh99655642022-09-01 15:11:31 -0400176 <button onClick={closeModalDelete}>Annuler</button>
177 </Modal>
178
179 <List>
ervinanoh8e918042022-09-06 10:30:59 -0400180 {contacts?.map((contact) => (
ervinanoh99655642022-09-01 15:11:31 -0400181 <ListItem
182 button
183 alignItems="flex-start"
ervinanoh8e918042022-09-06 10:30:59 -0400184 key={contact.id}
ervinanoh99655642022-09-01 15:11:31 -0400185 // selected={isSelected}
186 onClick={() => {
187 setCurrentContact(contact);
188 openModal();
189 }}
190 >
191 <ListItemAvatar>
192 <ConversationAvatar
193 // displayName={conversation.getDisplayNameNoFallback()}
194 // displayName={`${contact.id}`}
195 />
196 </ListItemAvatar>
197 <ListItemText primary={contact.id} secondary={contact.id} />
198 </ListItem>
199 ))}
200 </List>
idillon531b6f22022-09-16 14:02:00 -0400201 </div>
202 );
Larbi Gharibe9af9732021-03-31 15:08:01 +0100203}