blob: dd93376122f384b2ae726ac2ca202c47f646590b [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';
simon5da8ca62022-11-09 15:21:25 -050020import { useNavigate } from 'react-router-dom';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040021
simon5da8ca62022-11-09 15:21:25 -050022import { useAuthContext } from '../contexts/AuthProvider';
simond8ca2f22022-10-11 23:30:55 -040023import { setRefreshFromSlice } from '../redux/appSlice';
24import { useAppDispatch } from '../redux/hooks';
simon5da8ca62022-11-09 15:21:25 -050025import { apiUrl } from '../utils/constants';
ervinanoh34eb9472022-09-13 04:20:28 -040026
simonfe1de722022-10-02 00:21:43 -040027type AddContactPageProps = {
simonfe1de722022-10-02 00:21:43 -040028 contactId: string;
29};
30
simon5da8ca62022-11-09 15:21:25 -050031export default function AddContactPage({ contactId }: AddContactPageProps) {
32 const { token } = useAuthContext();
Adrien Béraudab519ff2022-05-03 15:34:48 -040033 const navigate = useNavigate();
simonfe1de722022-10-02 00:21:43 -040034
ervinanoh34eb9472022-09-13 04:20:28 -040035 const dispatch = useAppDispatch();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040036
simonfe1de722022-10-02 00:21:43 -040037 const handleClick = async () => {
simon5da8ca62022-11-09 15:21:25 -050038 const response = await fetch(new URL(`/conversations`, apiUrl), {
39 method: 'POST',
40 headers: {
41 Accept: 'application/json',
42 Authorization: `Bearer ${token}`,
43 'Content-Type': 'application/json',
44 },
45 body: JSON.stringify({ members: [contactId] }),
46 }).then((res) => {
47 dispatch(setRefreshFromSlice());
48 return res.json();
49 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040050
simond47ef9e2022-09-28 22:24:28 -040051 console.log(response);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040052 if (response.conversationId) {
simon5da8ca62022-11-09 15:21:25 -050053 navigate(`/account/conversation/${response.conversationId}`);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040054 }
simond47ef9e2022-09-28 22:24:28 -040055 };
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040056
57 return (
simond47ef9e2022-09-28 22:24:28 -040058 <Container className="messenger">
59 <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040060 <CardContent>
simond47ef9e2022-09-28 22:24:28 -040061 <Typography variant="h6">Jami key ID</Typography>
62 <Typography variant="body1">{contactId}</Typography>
63 <Box style={{ textAlign: 'center', marginTop: 16 }}>
64 <Fab variant="extended" color="primary" onClick={handleClick}>
65 <GroupAddRounded />
66 Add contact
67 </Fab>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040068 </Box>
69 </CardContent>
70 </Card>
simond47ef9e2022-09-28 22:24:28 -040071 </Container>
72 );
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040073}