Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [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 | */ |
| 18 | |
| 19 | import { Person } from '@mui/icons-material'; |
| 20 | import { Box, ListItem, ListItemAvatar, ListItemText } from '@mui/material'; |
| 21 | import List from '@mui/material/List'; |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 22 | import { useMemo, useState } from 'react'; |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 23 | import Modal from 'react-modal'; |
| 24 | |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 25 | import { |
| 26 | useBlockContactMutation, |
| 27 | useContactListQuery, |
| 28 | useContactQuery, |
| 29 | useRemoveContactMutation, |
| 30 | } from '../services/contactQueries'; |
Ziwei Wang | e8d7703 | 2023-01-11 15:57:38 -0500 | [diff] [blame] | 31 | import ContactDetailDialog from './ContactDetailDialog'; |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 32 | import ConversationAvatar from './ConversationAvatar'; |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 33 | import LoadingPage from './Loading'; |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 34 | |
| 35 | const customStyles = { |
| 36 | content: { |
| 37 | top: '50%', |
| 38 | left: '50%', |
| 39 | right: 'auto', |
| 40 | bottom: 'auto', |
| 41 | marginRight: '-50%', |
| 42 | transform: 'translate(-50%, -50%)', |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | export default function ContactList() { |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 47 | const [currentContactId, setCurrentContactId] = useState<string>(''); |
| 48 | const [parentModalOpen, setParentModalOpen] = useState<boolean>(false); |
| 49 | const [childModalOpen, setChildModalOpen] = useState<boolean>(false); |
| 50 | const [isBlocking, setIsBlocking] = useState(true); |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 51 | |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 52 | const closeParentModal = () => setParentModalOpen(false); |
| 53 | const openChildModal = () => setChildModalOpen(true); |
| 54 | const closeChildModal = () => setChildModalOpen(false); |
| 55 | |
Ziwei Wang | e8d7703 | 2023-01-11 15:57:38 -0500 | [diff] [blame] | 56 | const [contactDetailDialogOpen, setContactDetailDialogOpen] = useState<boolean>(false); |
| 57 | |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 58 | const contactListQuery = useContactListQuery(); |
| 59 | const { isLoading, data: contacts } = contactListQuery; |
| 60 | |
| 61 | const removeContactMutation = useRemoveContactMutation(); |
| 62 | const blockContactMutation = useBlockContactMutation(); |
| 63 | |
| 64 | const singleContactQuery = useContactQuery(currentContactId); |
| 65 | const { data: contactDetail } = singleContactQuery; |
| 66 | |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 67 | const handleClickContact = (id: string): void => { |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 68 | setCurrentContactId(id); |
| 69 | setParentModalOpen(true); |
| 70 | }; |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 71 | |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 72 | const removeContact = (): void => { |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 73 | //Not sure if the server handles empty contact id |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 74 | |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 75 | removeContactMutation.mutate(currentContactId); |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 76 | |
| 77 | closeChildModal(); |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 78 | closeParentModal(); |
| 79 | }; |
| 80 | |
| 81 | const blockContact = (): void => { |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 82 | //Not sure if the server handles empty contact id |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 83 | |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 84 | blockContactMutation.mutate(currentContactId); |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 85 | |
| 86 | closeChildModal(); |
| 87 | closeParentModal(); |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 88 | }; |
| 89 | |
Ziwei Wang | e8d7703 | 2023-01-11 15:57:38 -0500 | [diff] [blame] | 90 | const ContactDetailDialogElement = useMemo(() => { |
| 91 | const onClosingContactDetailDialog = () => setContactDetailDialogOpen(false); |
| 92 | |
| 93 | return ( |
| 94 | <ContactDetailDialog |
| 95 | contactDetail={contactDetail} |
| 96 | open={contactDetailDialogOpen} |
| 97 | onClose={onClosingContactDetailDialog} |
| 98 | /> |
| 99 | ); |
| 100 | }, [contactDetail, contactDetailDialogOpen]); |
| 101 | |
Ziwei Wang | 046fb20 | 2023-01-17 18:27:17 -0500 | [diff] [blame] | 102 | if (isLoading) { |
| 103 | return <LoadingPage />; |
| 104 | } |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 105 | return ( |
| 106 | <> |
| 107 | <Modal |
| 108 | isOpen={parentModalOpen} |
| 109 | onRequestClose={closeParentModal} |
| 110 | style={customStyles} |
| 111 | contentLabel="Example Modal" |
| 112 | > |
| 113 | <button onClick={closeParentModal}>close</button> |
| 114 | |
| 115 | <div style={{ display: 'flex', flexDirection: 'column', rowGap: '10px' }}> |
| 116 | <button |
| 117 | onClick={() => { |
| 118 | console.log('open dialog Supprimer: '); |
| 119 | setIsBlocking(false); |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 120 | openChildModal(); |
| 121 | }} |
| 122 | > |
| 123 | <Person /> Supprimer contact |
| 124 | </button> |
| 125 | |
| 126 | <button |
| 127 | onClick={() => { |
| 128 | console.log('open dialog BLOCK: '); |
| 129 | setIsBlocking(true); |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 130 | openChildModal(); |
| 131 | }} |
| 132 | > |
| 133 | <Person /> Bloquer le contact |
| 134 | </button> |
| 135 | |
| 136 | <button |
| 137 | onClick={() => { |
| 138 | console.log('open details contact for: '); |
| 139 | closeParentModal(); |
Ziwei Wang | e8d7703 | 2023-01-11 15:57:38 -0500 | [diff] [blame] | 140 | setContactDetailDialogOpen(true); |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 141 | }} |
| 142 | > |
| 143 | <Person /> Détails du contact |
| 144 | </button> |
| 145 | </div> |
| 146 | </Modal> |
| 147 | |
| 148 | <Modal |
| 149 | isOpen={childModalOpen} |
| 150 | onRequestClose={closeChildModal} |
| 151 | style={customStyles} |
| 152 | contentLabel="Merci de confirmer" |
| 153 | > |
| 154 | <Box> |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 155 | <div> |
| 156 | Voulez vous vraiment {isBlocking ? 'bloquer' : 'supprimer'} ce contact {currentContactId}? |
| 157 | </div> |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 158 | <br /> |
| 159 | |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 160 | <button onClick={isBlocking ? blockContact : removeContact}>{isBlocking ? 'bloquer' : 'supprimer'}</button> |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 161 | |
| 162 | <button onClick={closeChildModal}>Annuler</button> |
| 163 | </Box> |
| 164 | </Modal> |
| 165 | |
Ziwei Wang | e8d7703 | 2023-01-11 15:57:38 -0500 | [diff] [blame] | 166 | {ContactDetailDialogElement} |
| 167 | |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 168 | <List> |
| 169 | {contacts?.map((contact) => ( |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 170 | <ListItem alignItems="flex-start" key={contact.id} onClick={() => handleClickContact(contact.id)}> |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 171 | <ListItemAvatar> |
Ziwei Wang | 727dbbb | 2023-01-10 13:59:50 -0500 | [diff] [blame] | 172 | <ConversationAvatar /> |
Ziwei Wang | fea1d60 | 2023-01-09 17:16:01 -0500 | [diff] [blame] | 173 | </ListItemAvatar> |
| 174 | <ListItemText primary={contact.id} secondary={contact.id} /> |
| 175 | </ListItem> |
| 176 | ))} |
| 177 | </List> |
| 178 | </> |
| 179 | ); |
| 180 | } |