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 | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 26 | import ConversationAvatar from './ConversationAvatar'; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 27 | |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 28 | const customStyles = { |
| 29 | content: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | top: '50%', |
| 31 | left: '50%', |
| 32 | right: 'auto', |
| 33 | bottom: 'auto', |
| 34 | marginRight: '-50%', |
| 35 | transform: 'translate(-50%, -50%)', |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 36 | }, |
| 37 | }; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 38 | |
Adrien Béraud | 8698603 | 2021-04-25 12:04:53 -0400 | [diff] [blame] | 39 | export default function ContactList() { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 40 | const { axiosInstance } = useAuthContext(); |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 41 | const { accountId } = useAppSelector((state) => state.userInfo); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 42 | const dispatch = useAppDispatch(); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 43 | |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 44 | const [contacts, setContacts] = useState([]); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 45 | const [currentContact, setCurrentContact] = useState({}); |
| 46 | |
| 47 | const [modalIsOpen, setIsOpen] = useState(false); |
| 48 | const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false); |
| 49 | const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 50 | const [blockOrRemove, setBlockOrRemove] = useState(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 51 | |
| 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 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 61 | const getContactDetails = async () => { |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 62 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 63 | try { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 64 | const { data } = await axiosInstance.get(`/contacts/${currentContact.id}`, { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 65 | signal: controller.signal, |
| 66 | }); |
| 67 | console.log('CONTACT LIST - DETAILS: ', data); |
| 68 | } catch (e) { |
| 69 | console.log('ERROR GET CONTACT DETAILS: ', e); |
| 70 | } |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 73 | const removeOrBlock = async (block = false) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 74 | console.log('REMOVE'); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 75 | setBlockOrRemove(false); |
| 76 | const controller = new AbortController(); |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 77 | let url = `/contacts/${currentContact.id}`; |
| 78 | if (block) { |
| 79 | url += '/block'; |
| 80 | } |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 81 | 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 | } |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 89 | closeModalDelete(); |
| 90 | }; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 91 | |
| 92 | useEffect(() => { |
| 93 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 94 | axiosInstance |
| 95 | .get(`/contacts`, { |
| 96 | signal: controller.signal, |
| 97 | }) |
| 98 | .then(({ data }) => { |
| 99 | console.log('CONTACTS: ', data); |
| 100 | setContacts(data); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 101 | }); |
| 102 | return () => controller.abort(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 103 | }, [axiosInstance]); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 104 | |
| 105 | return ( |
| 106 | <div className="rooms-list"> |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 107 | <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={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 129 | console.log('open dialog Supprimer: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 130 | setBlockOrRemove(false); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 131 | closeModal(); |
| 132 | openModalDelete(); |
| 133 | }} |
| 134 | > |
| 135 | <Person /> Supprimer contact |
| 136 | </div> |
| 137 | <br /> |
| 138 | |
| 139 | <div |
| 140 | onClick={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 141 | console.log('open dialog BLOCK: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 142 | setBlockOrRemove(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 143 | closeModal(); |
| 144 | openModalDelete(); |
| 145 | }} |
| 146 | > |
| 147 | <Person /> Bloquer le contact |
| 148 | </div> |
| 149 | <br /> |
| 150 | |
| 151 | <div |
| 152 | onClick={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 153 | console.log('open details contact for: '); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 154 | 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 | > |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 169 | Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact? |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 170 | <br /> |
| 171 | {blockOrRemove ? ( |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 172 | <button onClick={() => removeOrBlock(true)}>Bloquer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 173 | ) : ( |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 174 | <button onClick={() => removeOrBlock()}>Supprimer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 175 | )} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 176 | <button onClick={closeModalDelete}>Annuler</button> |
| 177 | </Modal> |
| 178 | |
| 179 | <List> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 180 | {contacts?.map((contact) => ( |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 181 | <ListItem |
| 182 | button |
| 183 | alignItems="flex-start" |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 184 | key={contact.id} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 185 | // 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> |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 201 | </div> |
| 202 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 203 | } |