blob: c1e516cfd4aa56320696d344e50eb2bcad7e9f13 [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 Wange8d77032023-01-11 15:57:38 -050022import { ContactDetails as ContactDetailType } from 'jami-web-common';
23import { useEffect, useMemo, useState } from 'react';
Ziwei Wangfea1d602023-01-09 17:16:01 -050024import Modal from 'react-modal';
25
26import { useAuthContext } from '../contexts/AuthProvider';
Ziwei Wange8d77032023-01-11 15:57:38 -050027import ContactDetailDialog from './ContactDetailDialog';
Ziwei Wangfea1d602023-01-09 17:16:01 -050028import ConversationAvatar from './ConversationAvatar';
29
30const customStyles = {
31 content: {
32 top: '50%',
33 left: '50%',
34 right: 'auto',
35 bottom: 'auto',
36 marginRight: '-50%',
37 transform: 'translate(-50%, -50%)',
38 },
39};
40
41export default function ContactList() {
42 const { axiosInstance } = useAuthContext();
43
44 // const { accountId } = useAppSelector((state) => state.userInfo);
45 // const dispatch = useAppDispatch();
46
Ziwei Wange8d77032023-01-11 15:57:38 -050047 const [contacts, setContacts] = useState<Array<ContactDetailType>>([]);
Ziwei Wangfea1d602023-01-09 17:16:01 -050048 const [currentContactId, setCurrentContactId] = useState<string>('');
49 const [parentModalOpen, setParentModalOpen] = useState<boolean>(false);
50 const [childModalOpen, setChildModalOpen] = useState<boolean>(false);
51 const [isBlocking, setIsBlocking] = useState(true);
Ziwei Wange8d77032023-01-11 15:57:38 -050052 const [contactDetail, setContactDetail] = useState<ContactDetailType>({
53 added: '',
54 confirmed: '',
55 conversationId: '',
56 id: '',
57 });
Ziwei Wangfea1d602023-01-09 17:16:01 -050058 const closeParentModal = () => setParentModalOpen(false);
59 const openChildModal = () => setChildModalOpen(true);
60 const closeChildModal = () => setChildModalOpen(false);
61
Ziwei Wange8d77032023-01-11 15:57:38 -050062 const [contactDetailDialogOpen, setContactDetailDialogOpen] = useState<boolean>(false);
63
Ziwei Wang727dbbb2023-01-10 13:59:50 -050064 const handleClickContact = (id: string): void => {
Ziwei Wangfea1d602023-01-09 17:16:01 -050065 setCurrentContactId(id);
66 setParentModalOpen(true);
67 };
Ziwei Wangfea1d602023-01-09 17:16:01 -050068
Ziwei Wange8d77032023-01-11 15:57:38 -050069 const getContactDetails = () => {
70 axiosInstance
71 .get<ContactDetailType>(`/contacts/${currentContactId}`)
72 .then(({ data }) => {
73 setContactDetail(data);
74 })
75 .catch((err) => {
76 console.log('ERROR GET CONTACT DETAILS: ', err);
77 });
78 };
Ziwei Wangfea1d602023-01-09 17:16:01 -050079
Ziwei Wang727dbbb2023-01-10 13:59:50 -050080 const removeContact = (): void => {
81 console.log('Removing contact');
Ziwei Wangfea1d602023-01-09 17:16:01 -050082
83 //Not sure if the server handles empty contact id
84 if (currentContactId === '') {
85 throw 'currentContactId is empty';
86 }
87
Ziwei Wang727dbbb2023-01-10 13:59:50 -050088 //TODO: put all urls in a single file that can be imported and referenced later
89 const url = `/contacts/${currentContactId}`;
Ziwei Wangfea1d602023-01-09 17:16:01 -050090
91 axiosInstance(url, {
Ziwei Wang727dbbb2023-01-10 13:59:50 -050092 method: 'DELETE',
93 })
94 .then((res) => {
95 if (res.status === 204) {
96 //removes the contact from UI, but does not make another request to fetch all contacts
97 setContacts((prev) => {
98 return prev.filter((contact) => contact.id !== currentContactId);
99 });
100 //TODO: show notification of success
101 }
102 })
103 .catch((err) => {
104 console.log(`ERROR removing CONTACT : ${currentContactId} `, err);
105 });
Ziwei Wangfea1d602023-01-09 17:16:01 -0500106
107 closeChildModal();
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500108 closeParentModal();
109 };
110
111 const blockContact = (): void => {
112 console.log('remove or block');
113
114 //Not sure if the server handles empty contact id
115 if (currentContactId === '') {
116 throw 'currentContactId is empty';
117 }
118
119 const url = `/contacts/${currentContactId}/block`;
120
121 axiosInstance(url, {
122 method: 'POST',
123 })
124 .then((res) => {
125 if (res.status === 204) {
126 //TODO: Integrate behaviours after blocking a contact
127 }
128 })
129 .catch((err) => {
130 console.log(`ERROR blocking CONTACT : ${currentContactId} `, err);
131 });
132
133 closeChildModal();
134 closeParentModal();
Ziwei Wangfea1d602023-01-09 17:16:01 -0500135 };
136
Ziwei Wange8d77032023-01-11 15:57:38 -0500137 const ContactDetailDialogElement = useMemo(() => {
138 const onClosingContactDetailDialog = () => setContactDetailDialogOpen(false);
139
140 return (
141 <ContactDetailDialog
142 contactDetail={contactDetail}
143 open={contactDetailDialogOpen}
144 onClose={onClosingContactDetailDialog}
145 />
146 );
147 }, [contactDetail, contactDetailDialogOpen]);
148
Ziwei Wangfea1d602023-01-09 17:16:01 -0500149 useEffect(() => {
150 const controller = new AbortController();
151 axiosInstance
152 .get(`/contacts`, {
153 signal: controller.signal,
154 })
155 .then(({ data }) => {
156 console.log('CONTACTS: ', data);
157 setContacts(data);
158 });
159 return () => controller.abort();
160 }, [axiosInstance]);
161
162 return (
163 <>
164 <Modal
165 isOpen={parentModalOpen}
166 onRequestClose={closeParentModal}
167 style={customStyles}
168 contentLabel="Example Modal"
169 >
170 <button onClick={closeParentModal}>close</button>
171
172 <div style={{ display: 'flex', flexDirection: 'column', rowGap: '10px' }}>
173 <button
174 onClick={() => {
175 console.log('open dialog Supprimer: ');
176 setIsBlocking(false);
Ziwei Wangfea1d602023-01-09 17:16:01 -0500177 openChildModal();
178 }}
179 >
180 <Person /> Supprimer contact
181 </button>
182
183 <button
184 onClick={() => {
185 console.log('open dialog BLOCK: ');
186 setIsBlocking(true);
Ziwei Wangfea1d602023-01-09 17:16:01 -0500187 openChildModal();
188 }}
189 >
190 <Person /> Bloquer le contact
191 </button>
192
193 <button
194 onClick={() => {
195 console.log('open details contact for: ');
196 closeParentModal();
Ziwei Wange8d77032023-01-11 15:57:38 -0500197 setContactDetailDialogOpen(true);
198 getContactDetails();
Ziwei Wangfea1d602023-01-09 17:16:01 -0500199 }}
200 >
201 <Person /> Détails du contact
202 </button>
203 </div>
204 </Modal>
205
206 <Modal
207 isOpen={childModalOpen}
208 onRequestClose={closeChildModal}
209 style={customStyles}
210 contentLabel="Merci de confirmer"
211 >
212 <Box>
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500213 <div>
214 Voulez vous vraiment {isBlocking ? 'bloquer' : 'supprimer'} ce contact {currentContactId}?
215 </div>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500216 <br />
217
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500218 <button onClick={isBlocking ? blockContact : removeContact}>{isBlocking ? 'bloquer' : 'supprimer'}</button>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500219
220 <button onClick={closeChildModal}>Annuler</button>
221 </Box>
222 </Modal>
223
Ziwei Wange8d77032023-01-11 15:57:38 -0500224 {ContactDetailDialogElement}
225
Ziwei Wangfea1d602023-01-09 17:16:01 -0500226 <List>
227 {contacts?.map((contact) => (
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500228 <ListItem alignItems="flex-start" key={contact.id} onClick={() => handleClickContact(contact.id)}>
Ziwei Wangfea1d602023-01-09 17:16:01 -0500229 <ListItemAvatar>
Ziwei Wang727dbbb2023-01-10 13:59:50 -0500230 <ConversationAvatar />
Ziwei Wangfea1d602023-01-09 17:16:01 -0500231 </ListItemAvatar>
232 <ListItemText primary={contact.id} secondary={contact.id} />
233 </ListItem>
234 ))}
235 </List>
236 </>
237 );
238}