blob: 0fbd21bc785f43f3899bfa8931f5c131b5184574 [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import { useNavigate } from 'react-router-dom';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -04002
Adrien Béraudab519ff2022-05-03 15:34:48 -04003import { Box, Container, Fab, Card, CardContent, Typography } from '@mui/material';
4import GroupAddRounded from '@mui/icons-material/GroupAddRounded';
simond47ef9e2022-09-28 22:24:28 -04005import authManager from '../AuthManager';
ervinanoh34eb9472022-09-13 04:20:28 -04006import { useAppDispatch } from '../../redux/hooks';
7import { setRefreshFromSlice } from '../../redux/appSlice';
8
Adrien Béraud35e7d7c2021-04-13 03:28:39 -04009export default function AddContactPage(props) {
Adrien Béraudab519ff2022-05-03 15:34:48 -040010 const navigate = useNavigate();
simond47ef9e2022-09-28 22:24:28 -040011 const accountId = props.accountId || props.match.params.accountId;
12 const contactId = props.contactId || props.match.params.contactId;
ervinanoh34eb9472022-09-13 04:20:28 -040013 const dispatch = useAppDispatch();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040014
simond47ef9e2022-09-28 22:24:28 -040015 const handleClick = async (e) => {
16 const response = await authManager
17 .fetch(`/api/accounts/${accountId}/conversations`, {
18 method: 'POST',
19 headers: {
20 Accept: 'application/json',
21 'Content-Type': 'application/json',
22 },
23 body: JSON.stringify({ members: [contactId] }),
24 })
25 .then((res) => {
26 dispatch(setRefreshFromSlice());
27 return res.json();
28 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040029
simond47ef9e2022-09-28 22:24:28 -040030 console.log(response);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040031 if (response.conversationId) {
simond47ef9e2022-09-28 22:24:28 -040032 navigate(`/account/${accountId}/conversation/${response.conversationId}`);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040033 }
simond47ef9e2022-09-28 22:24:28 -040034 };
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040035
36 return (
simond47ef9e2022-09-28 22:24:28 -040037 <Container className="messenger">
38 <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040039 <CardContent>
simond47ef9e2022-09-28 22:24:28 -040040 <Typography variant="h6">Jami key ID</Typography>
41 <Typography variant="body1">{contactId}</Typography>
42 <Box style={{ textAlign: 'center', marginTop: 16 }}>
43 <Fab variant="extended" color="primary" onClick={handleClick}>
44 <GroupAddRounded />
45 Add contact
46 </Fab>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040047 </Box>
48 </CardContent>
49 </Card>
simond47ef9e2022-09-28 22:24:28 -040050 </Container>
51 );
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040052}