blob: 9274abc93c324f2741bd3c5e7bce269bd11cfd01 [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';
22import { useEffect, useState } from 'react';
23// import Modal from '@mui/material/Modal';
24import Modal from 'react-modal';
25
26import { useAuthContext } from '../contexts/AuthProvider';
27import ConversationAvatar from './ConversationAvatar';
28
29const customStyles = {
30 content: {
31 top: '50%',
32 left: '50%',
33 right: 'auto',
34 bottom: 'auto',
35 marginRight: '-50%',
36 transform: 'translate(-50%, -50%)',
37 },
38};
39
40export default function ContactList() {
41 const { axiosInstance } = useAuthContext();
42
43 // const { accountId } = useAppSelector((state) => state.userInfo);
44 // const dispatch = useAppDispatch();
45
46 const [contacts, setContacts] = useState<Array<any>>([]);
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);
51 // const [modalDetailsIsOpen, setModalDetailsIsOpen] = useState(false);
52
53 const closeParentModal = () => setParentModalOpen(false);
54 const openChildModal = () => setChildModalOpen(true);
55 const closeChildModal = () => setChildModalOpen(false);
56
57 const handleClickContact = (id: string) => {
58 setCurrentContactId(id);
59 setParentModalOpen(true);
60 };
61 // const openModalDetails = () => setModalDetailsIsOpen(true);
62
63 // const getContactDetails = () => {
64 // const controller = new AbortController();
65 // // try {
66 // // const { data } = await axiosInstance.get(`/contacts/${currentContactId}`, {
67 // // signal: controller.signal,
68 // // });
69 // // console.log('CONTACT LIST - DETAILS: ', data);
70 // // } catch (e) {
71 // // console.log('ERROR GET CONTACT DETAILS: ', e);
72 // // }
73
74 // axiosInstance
75 // .get(`/contacts/${currentContactId}`, {
76 // signal: controller.signal,
77 // })
78 // .then((data) => {
79 // console.log('CONTACT LIST - DETAILS: ', data);
80 // })
81 // .catch((err) => {
82 // console.log('ERROR GET CONTACT DETAILS: ', err);
83 // });
84 // };
85
86 const removeOrBlock = () => {
87 console.log('remove or block');
88 const controller = new AbortController();
89
90 //Not sure if the server handles empty contact id
91 if (currentContactId === '') {
92 throw 'currentContactId is empty';
93 }
94
95 let url = `/contacts/${currentContactId}`;
96 if (isBlocking) {
97 url += '/block';
98 }
99
100 // try {
101 // await axiosInstance(url, {
102 // signal: controller.signal,
103 // method: block ? 'POST' : 'DELETE',
104 // });
105 // } catch (e) {
106 // console.log(`ERROR ${block ? 'blocking' : 'removing'} CONTACT : `, e);
107 // }
108
109 axiosInstance(url, {
110 signal: controller.signal,
111 method: isBlocking ? 'POST' : 'DELETE',
112 }).catch((err) => {
113 console.log(`ERROR ${isBlocking ? 'blocking' : 'removing'} CONTACT : `, err);
114 });
115
116 closeChildModal();
117 };
118
119 useEffect(() => {
120 const controller = new AbortController();
121 axiosInstance
122 .get(`/contacts`, {
123 signal: controller.signal,
124 })
125 .then(({ data }) => {
126 console.log('CONTACTS: ', data);
127 setContacts(data);
128 });
129 return () => controller.abort();
130 }, [axiosInstance]);
131
132 return (
133 <>
134 <Modal
135 isOpen={parentModalOpen}
136 onRequestClose={closeParentModal}
137 style={customStyles}
138 contentLabel="Example Modal"
139 >
140 <button onClick={closeParentModal}>close</button>
141
142 <div style={{ display: 'flex', flexDirection: 'column', rowGap: '10px' }}>
143 <button
144 onClick={() => {
145 console.log('open dialog Supprimer: ');
146 setIsBlocking(false);
147 closeParentModal();
148 openChildModal();
149 }}
150 >
151 <Person /> Supprimer contact
152 </button>
153
154 <button
155 onClick={() => {
156 console.log('open dialog BLOCK: ');
157 setIsBlocking(true);
158 closeParentModal();
159 openChildModal();
160 }}
161 >
162 <Person /> Bloquer le contact
163 </button>
164
165 <button
166 onClick={() => {
167 console.log('open details contact for: ');
168 closeParentModal();
169 // openModalDetails();
170 // getContactDetails();
171 }}
172 >
173 <Person /> Détails du contact
174 </button>
175 </div>
176 </Modal>
177
178 <Modal
179 isOpen={childModalOpen}
180 onRequestClose={closeChildModal}
181 style={customStyles}
182 contentLabel="Merci de confirmer"
183 >
184 <Box>
185 <div>Voulez vous vraiment {isBlocking ? 'bloquer' : 'supprimer'} ce contact?</div>
186 <br />
187
188 <button onClick={() => removeOrBlock()}>{isBlocking ? 'bloquer' : 'supprimer'}</button>
189
190 <button onClick={closeChildModal}>Annuler</button>
191 </Box>
192 </Modal>
193
194 <List>
195 {contacts?.map((contact) => (
196 <ListItem
197 button
198 alignItems="flex-start"
199 key={contact.id}
200 // selected={isSelected}
201 onClick={() => handleClickContact(contact.id)}
202 >
203 <ListItemAvatar>
204 <ConversationAvatar
205 // displayName={conversation.getDisplayNameNoFallback()}
206 // displayName={`${contact.id}`}
207 />
208 </ListItemAvatar>
209 <ListItemText primary={contact.id} secondary={contact.id} />
210 </ListItem>
211 ))}
212 </List>
213 </>
214 );
215}