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 | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 24 | import authManager from '../AuthManager'; |
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() { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 40 | const { accountId, account } = useAppSelector((state) => state.userInfo); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 41 | const dispatch = useAppDispatch(); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 42 | |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 43 | const [contacts, setContacts] = useState([]); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 44 | const [currentContact, setCurrentContact] = useState({}); |
| 45 | |
| 46 | const [modalIsOpen, setIsOpen] = useState(false); |
| 47 | const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false); |
| 48 | const [modalDeleteIsOpen, setModalDeleteIsOpen] = useState(false); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 49 | const [blockOrRemove, setBlockOrRemove] = useState(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 50 | |
| 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 |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 63 | .fetch(`/api/accounts/${accountId}/contacts/details/${currentContact.id}`, { |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 64 | signal: controller.signal, |
| 65 | }) |
| 66 | .then((res) => res.json()) |
| 67 | .then((result) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | console.log('CONTACT LIST - DETAILS: ', result); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 69 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 70 | .catch((e) => console.log('ERROR GET CONTACT DETAILS: ', e)); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 73 | const removeOrBlock = (typeOfRemove) => { |
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(); |
| 77 | authManager |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | .fetch(`/api/accounts/${accountId}/contacts/${typeOfRemove}/${currentContact.id}`, { |
| 79 | signal: controller.signal, |
| 80 | method: 'DELETE', |
| 81 | }) |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 82 | .then((res) => res.json()) |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 83 | .catch((e) => console.log(`ERROR ${typeOfRemove}ing CONTACT : `, e)); |
| 84 | closeModalDelete(); |
| 85 | }; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 86 | |
| 87 | useEffect(() => { |
| 88 | const controller = new AbortController(); |
| 89 | authManager |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 90 | .fetch(`/api/accounts/${accountId}/contacts`, { |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 91 | signal: controller.signal, |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 92 | }) |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 93 | .then((res) => res.json()) |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 94 | .then((result) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 95 | console.log('CONTACTS: ', result); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 96 | setContacts(result); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 97 | }); |
| 98 | return () => controller.abort(); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 99 | }, [accountId, blockOrRemove]); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 100 | |
| 101 | return ( |
| 102 | <div className="rooms-list"> |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 103 | <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={() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 125 | console.log('open dialog Supprimer: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 126 | setBlockOrRemove(false); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 127 | closeModal(); |
| 128 | openModalDelete(); |
| 129 | }} |
| 130 | > |
| 131 | <Person /> Supprimer contact |
| 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 BLOCK: '); |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 138 | setBlockOrRemove(true); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 139 | closeModal(); |
| 140 | openModalDelete(); |
| 141 | }} |
| 142 | > |
| 143 | <Person /> Bloquer le 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 details contact for: '); |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 150 | 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 | > |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 165 | Voulez vous vraiment {blockOrRemove ? 'bloquer' : 'supprimer'} ce contact? |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 166 | <br /> |
| 167 | {blockOrRemove ? ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 168 | <button onClick={() => removeOrBlock('block')}>Bloquer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 169 | ) : ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 170 | <button onClick={() => removeOrBlock('remove')}>Supprimer</button> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 171 | )} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 172 | <button onClick={closeModalDelete}>Annuler</button> |
| 173 | </Modal> |
| 174 | |
| 175 | <List> |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 176 | {contacts?.map((contact) => ( |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 177 | <ListItem |
| 178 | button |
| 179 | alignItems="flex-start" |
ervinanoh | 8e91804 | 2022-09-06 10:30:59 -0400 | [diff] [blame] | 180 | key={contact.id} |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 181 | // 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> |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 197 | </div> |
| 198 | ); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 199 | } |