blob: 2dfae7357e226b57db22ccb4e9b59c372b74db53 [file] [log] [blame]
simon07b4eb02022-09-29 17:50:26 -04001import GroupAddRounded from '@mui/icons-material/GroupAddRounded';
2import { Box, Card, CardContent, Container, Fab, Typography } from '@mui/material';
simonfe1de722022-10-02 00:21:43 -04003import { useNavigate, useParams } from 'react-router-dom';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -04004
ervinanoh34eb9472022-09-13 04:20:28 -04005import { setRefreshFromSlice } from '../../redux/appSlice';
simon07b4eb02022-09-29 17:50:26 -04006import { useAppDispatch } from '../../redux/hooks';
7import authManager from '../AuthManager';
ervinanoh34eb9472022-09-13 04:20:28 -04008
simonfe1de722022-10-02 00:21:43 -04009type AddContactPageProps = {
10 accountId: string;
11 contactId: string;
12};
13
14export default function AddContactPage(props: AddContactPageProps) {
Adrien Béraudab519ff2022-05-03 15:34:48 -040015 const navigate = useNavigate();
simonfe1de722022-10-02 00:21:43 -040016
17 const params = useParams();
18 const accountId = props.accountId || params.accountId;
19 const contactId = props.contactId || params.contactId;
ervinanoh34eb9472022-09-13 04:20:28 -040020 const dispatch = useAppDispatch();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040021
simonfe1de722022-10-02 00:21:43 -040022 const handleClick = async () => {
simond47ef9e2022-09-28 22:24:28 -040023 const response = await authManager
24 .fetch(`/api/accounts/${accountId}/conversations`, {
25 method: 'POST',
26 headers: {
27 Accept: 'application/json',
28 'Content-Type': 'application/json',
29 },
30 body: JSON.stringify({ members: [contactId] }),
31 })
32 .then((res) => {
33 dispatch(setRefreshFromSlice());
34 return res.json();
35 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040036
simond47ef9e2022-09-28 22:24:28 -040037 console.log(response);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040038 if (response.conversationId) {
simond47ef9e2022-09-28 22:24:28 -040039 navigate(`/account/${accountId}/conversation/${response.conversationId}`);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040040 }
simond47ef9e2022-09-28 22:24:28 -040041 };
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040042
43 return (
simond47ef9e2022-09-28 22:24:28 -040044 <Container className="messenger">
45 <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040046 <CardContent>
simond47ef9e2022-09-28 22:24:28 -040047 <Typography variant="h6">Jami key ID</Typography>
48 <Typography variant="body1">{contactId}</Typography>
49 <Box style={{ textAlign: 'center', marginTop: 16 }}>
50 <Fab variant="extended" color="primary" onClick={handleClick}>
51 <GroupAddRounded />
52 Add contact
53 </Fab>
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040054 </Box>
55 </CardContent>
56 </Card>
simond47ef9e2022-09-28 22:24:28 -040057 </Container>
58 );
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040059}