blob: b0dfad0dd4ba831943548650e1b307aadd8f8ca1 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simon07b4eb02022-09-29 17:50:26 -040018import GroupAddRounded from '@mui/icons-material/GroupAddRounded';
19import { Box, Card, CardContent, Container, Fab, Typography } from '@mui/material';
simonfe1de722022-10-02 00:21:43 -040020import { useNavigate, useParams } from 'react-router-dom';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040021
simon07b4eb02022-09-29 17:50:26 -040022import authManager from '../AuthManager';
simond8ca2f22022-10-11 23:30:55 -040023import { setRefreshFromSlice } from '../redux/appSlice';
24import { useAppDispatch } from '../redux/hooks';
ervinanoh34eb9472022-09-13 04:20:28 -040025
simonfe1de722022-10-02 00:21:43 -040026type AddContactPageProps = {
27 accountId: string;
28 contactId: string;
29};
30
31export default function AddContactPage(props: AddContactPageProps) {
Adrien Béraudab519ff2022-05-03 15:34:48 -040032 const navigate = useNavigate();
simonfe1de722022-10-02 00:21:43 -040033
34 const params = useParams();
35 const accountId = props.accountId || params.accountId;
36 const contactId = props.contactId || params.contactId;
ervinanoh34eb9472022-09-13 04:20:28 -040037 const dispatch = useAppDispatch();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040038
simonfe1de722022-10-02 00:21:43 -040039 const handleClick = async () => {
simond47ef9e2022-09-28 22:24:28 -040040 const response = await authManager
41 .fetch(`/api/accounts/${accountId}/conversations`, {
42 method: 'POST',
43 headers: {
44 Accept: 'application/json',
45 'Content-Type': 'application/json',
46 },
47 body: JSON.stringify({ members: [contactId] }),
48 })
49 .then((res) => {
50 dispatch(setRefreshFromSlice());
51 return res.json();
52 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040053
simond47ef9e2022-09-28 22:24:28 -040054 console.log(response);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040055 if (response.conversationId) {
Michelle Sepkap Simeb3dd3122022-11-03 02:12:39 -040056 navigate(`/deprecated-account/${accountId}/conversation/${response.conversationId}`);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040057 }
simond47ef9e2022-09-28 22:24:28 -040058 };
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040059
60 return (
simond47ef9e2022-09-28 22:24:28 -040061 <Container className="messenger">
62 <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040063 <CardContent>
simond47ef9e2022-09-28 22:24:28 -040064 <Typography variant="h6">Jami key ID</Typography>
65 <Typography variant="body1">{contactId}</Typography>
66 <Box style={{ textAlign: 'center', marginTop: 16 }}>
67 <Fab variant="extended" color="primary" onClick={handleClick}>
68 <GroupAddRounded />
69 Add contact
70 </Fab>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040071 </Box>
72 </CardContent>
73 </Card>
simond47ef9e2022-09-28 22:24:28 -040074 </Container>
75 );
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040076}