blob: 2ff82524f336002a0a5c0e2b79d00eaa26bbc681 [file] [log] [blame]
Ziwei Wangfea1d602023-01-09 17:16:01 -05001/*
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
19import { Person } from '@mui/icons-material';
20import { Box, ListItem, ListItemAvatar, ListItemText } from '@mui/material';
21import List from '@mui/material/List';
Ziwei Wang046fb202023-01-17 18:27:17 -050022import { useMemo, useState } from 'react';
Ziwei Wangfea1d602023-01-09 17:16:01 -050023import Modal from 'react-modal';
24
Ziwei Wang046fb202023-01-17 18:27:17 -050025import {
26 useBlockContactMutation,
27 useContactListQuery,
28 useContactQuery,
29 useRemoveContactMutation,
30} from '../services/contactQueries';
Ziwei Wange8d77032023-01-11 15:57:38 -050031import ContactDetailDialog from './ContactDetailDialog';
Ziwei Wangfea1d602023-01-09 17:16:01 -050032import ConversationAvatar from './ConversationAvatar';
Ziwei Wang046fb202023-01-17 18:27:17 -050033import LoadingPage from './Loading';
Ziwei Wangfea1d602023-01-09 17:16:01 -050034
35const 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
46export default function ContactList() {
Ziwei Wangfea1d602023-01-09 17:16:01 -050047 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 Wang046fb202023-01-17 18:27:17 -050051
Ziwei Wangfea1d602023-01-09 17:16:01 -050052 const closeParentModal = () => setParentModalOpen(false);
53 const openChildModal = () => setChildModalOpen(true);
54 const closeChildModal = () => setChildModalOpen(false);
55
Ziwei Wange8d77032023-01-11 15:57:38 -050056 const [contactDetailDialogOpen, setContactDetailDialogOpen] = useState<boolean>(false);
57
Ziwei Wang046fb202023-01-17 18:27:17 -050058 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 Wang727dbbb2023-01-10 13:59:50 -050067 const handleClickContact = (id: string): void => {
Ziwei Wangfea1d602023-01-09 17:16:01 -050068 setCurrentContactId(id);
69 setParentModalOpen(true);
70 };
Ziwei Wangfea1d602023-01-09 17:16:01 -050071
Ziwei Wang727dbbb2023-01-10 13:59:50 -050072 const removeContact = (): void => {
Ziwei Wangfea1d602023-01-09 17:16:01 -050073 //Not sure if the server handles empty contact id
Ziwei Wangfea1d602023-01-09 17:16:01 -050074
Ziwei Wang046fb202023-01-17 18:27:17 -050075 removeContactMutation.mutate(currentContactId);
Ziwei Wangfea1d602023-01-09 17:16:01 -050076
77 closeChildModal();
Ziwei Wang727dbbb2023-01-10 13:59:50 -050078 closeParentModal();
79 };
80
81 const blockContact = (): void => {
Ziwei Wang727dbbb2023-01-10 13:59:50 -050082 //Not sure if the server handles empty contact id
Ziwei Wang727dbbb2023-01-10 13:59:50 -050083
Ziwei Wang046fb202023-01-17 18:27:17 -050084 blockContactMutation.mutate(currentContactId);
Ziwei Wang727dbbb2023-01-10 13:59:50 -050085
86 closeChildModal();
87 closeParentModal();
Ziwei Wangfea1d602023-01-09 17:16:01 -050088 };
89
Ziwei Wange8d77032023-01-11 15:57:38 -050090 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 Wang046fb202023-01-17 18:27:17 -0500102 if (isLoading) {
103 return <LoadingPage />;
104 }
Ziwei Wangfea1d602023-01-09 17:16:01 -0500105 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 Wangfea1d602023-01-09 17:16:01 -0500120 openChildModal();
121 }}
122 >
123 <Person /> Supprimer contact
124 </button>
125
126 <button
127 onClick={() => {
128 console.log('open dialog BLOCK: ');
129 setIsBlocking(true);
Ziwei Wangfea1d602023-01-09 17:16:01 -0500130 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 Wange8d77032023-01-11 15:57:38 -0500140 setContactDetailDialogOpen(true);
Ziwei Wangfea1d602023-01-09 17:16:01 -0500141 }}
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 Wang727dbbb2023-01-10 13:59:50 -0500155 <div>
156 Voulez vous vraiment {isBlocking ? 'bloquer' : 'supprimer'} ce contact {currentContactId}?
157 </div>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500158 <br />
159
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500160 <button onClick={isBlocking ? blockContact : removeContact}>{isBlocking ? 'bloquer' : 'supprimer'}</button>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500161
162 <button onClick={closeChildModal}>Annuler</button>
163 </Box>
164 </Modal>
165
Ziwei Wange8d77032023-01-11 15:57:38 -0500166 {ContactDetailDialogElement}
167
Ziwei Wangfea1d602023-01-09 17:16:01 -0500168 <List>
169 {contacts?.map((contact) => (
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500170 <ListItem alignItems="flex-start" key={contact.id} onClick={() => handleClickContact(contact.id)}>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500171 <ListItemAvatar>
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500172 <ConversationAvatar />
Ziwei Wangfea1d602023-01-09 17:16:01 -0500173 </ListItemAvatar>
174 <ListItemText primary={contact.id} secondary={contact.id} />
175 </ListItem>
176 ))}
177 </List>
178 </>
179 );
180}