simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 18 | import { Person } from '@mui/icons-material'; |
| 19 | import { ListItem, ListItemAvatar, ListItemText } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import List from '@mui/material/List'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | import { useEffect, useState } from 'react'; |
| 22 | import Modal from 'react-modal'; |
| 23 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 24 | import { useAuthContext } from '../contexts/AuthProvider'; |
simon | d8ca2f2 | 2022-10-11 23:30:55 -0400 | [diff] [blame] | 25 | import { useAppDispatch, useAppSelector } from '../redux/hooks'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 26 | import { apiUrl } from '../utils/constants'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 27 | import ConversationAvatar from './ConversationAvatar'; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 28 | |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 29 | const customStyles = { |
| 30 | content: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 31 | top: '50%', |
| 32 | left: '50%', |
| 33 | right: 'auto', |
| 34 | bottom: 'auto', |
| 35 | marginRight: '-50%', |
| 36 | transform: 'translate(-50%, -50%)', |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 37 | }, |
| 38 | }; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 39 | |
Adrien Béraud | 8698603 | 2021-04-25 12:04:53 -0400 | [diff] [blame] | 40 | export default function ContactList() { |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 41 | const { token } = useAuthContext(); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 42 | const { accountId } = useAppSelector((state) => state.userInfo); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 43 | const dispatch = useAppDispatch(); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 44 | |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 45 | const [contacts, setContacts] = useState([]); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 46 | const [currentContact, setCurrentContact] = useState({}); |
| 47 | |
| 48 | const [modalIsOpen, setIsOpen] = useState(false); |
| 49 | const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false); |
| 50 | const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 51 | const [blockOrRemove, setBlockOrRemove] = useState(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 52 | |
| 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(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 64 | fetch(new URL(`/contacts/${currentContact.id}`, apiUrl), { |
| 65 | signal: controller.signal, |
| 66 | headers: { |
| 67 | Authorization: `Bearer ${token}`, |
| 68 | }, |
| 69 | }) |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 70 | .then((res) => res.json()) |
| 71 | .then((result) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 72 | console.log('CONTACT LIST - DETAILS: ', result); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 73 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 74 | .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e)); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 77 | const removeOrBlock = (block = false) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | console.log('REMOVE'); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 79 | setBlockOrRemove(false); |
| 80 | const controller = new AbortController(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 81 | 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 | }) |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 92 | .then((res) => res.json()) |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 93 | .catch((e) => console.log(`ERROR ${block ? 'blocking' : 'removing'} CONTACT : `, e)); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 94 | closeModalDelete(); |
| 95 | }; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 96 | |
| 97 | useEffect(() => { |
| 98 | const controller = new AbortController(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 99 | fetch(new URL(`/contacts`, apiUrl), { |
| 100 | signal: controller.signal, |
| 101 | headers: { |
| 102 | Authorization: `Bearer ${token}`, |
| 103 | }, |
| 104 | }) |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 105 | .then((res) => res.json()) |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 106 | .then((result) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 107 | console.log('CONTACTS: ', result); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 108 | setContacts(result); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 109 | }); |
| 110 | return () => controller.abort(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 111 | }, [token]); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 112 | |
| 113 | return ( |
| 114 | <div className="rooms-list"> |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 115 | <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={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 137 | console.log('open dialog Supprimer: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 138 | setBlockOrRemove(false); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 139 | closeModal(); |
| 140 | openModalDelete(); |
| 141 | }} |
| 142 | > |
| 143 | <Person /> Supprimer contact |
| 144 | </div> |
| 145 | <br /> |
| 146 | |
| 147 | <div |
| 148 | onClick={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 149 | console.log('open dialog BLOCK: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 150 | setBlockOrRemove(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 151 | closeModal(); |
| 152 | openModalDelete(); |
| 153 | }} |
| 154 | > |
| 155 | <Person /> Bloquer le contact |
| 156 | </div> |
| 157 | <br /> |
| 158 | |
| 159 | <div |
| 160 | onClick={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 161 | console.log('open details contact for: '); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 162 | 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 | > |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 177 | Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact? |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 178 | <br /> |
| 179 | {blockOrRemove ? ( |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 180 | <button onClick={() => removeOrBlock(true)}>Bloquer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 181 | ) : ( |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 182 | <button onClick={() => removeOrBlock()}>Supprimer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 183 | )} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 184 | <button onClick={closeModalDelete}>Annuler</button> |
| 185 | </Modal> |
| 186 | |
| 187 | <List> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 188 | {contacts?.map((contact) => ( |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 189 | <ListItem |
| 190 | button |
| 191 | alignItems="flex-start" |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 192 | key={contact.id} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 193 | // 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> |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 209 | </div> |
| 210 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 211 | } |