Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 1 | import { useNavigate } from "react-router-dom"; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 2 | |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 3 | import { Box, Container, Fab, Card, CardContent, Typography } from '@mui/material'; |
| 4 | import GroupAddRounded from '@mui/icons-material/GroupAddRounded'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 5 | import authManager from '../AuthManager' |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 6 | import { useAppDispatch } from '../../redux/hooks'; |
| 7 | import { setRefreshFromSlice } from '../../redux/appSlice'; |
| 8 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 9 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 10 | export default function AddContactPage(props) { |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 11 | const navigate = useNavigate(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 12 | const accountId = props.accountId || props.match.params.accountId |
| 13 | const contactId = props.contactId || props.match.params.contactId |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 14 | const dispatch = useAppDispatch(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 15 | |
| 16 | const handleClick = async e => { |
| 17 | const response = await authManager.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]}) |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 24 | }).then(res => { |
| 25 | dispatch(setRefreshFromSlice()) |
| 26 | return res.json() |
| 27 | }) |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 28 | |
| 29 | console.log(response) |
| 30 | if (response.conversationId) { |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 31 | navigate(`/account/${accountId}/conversation/${response.conversationId}`) |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | return ( |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 36 | <Container className='messenger'> |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 37 | <Card variant='outlined' style={{ borderRadius: 16, maxWidth: 560, margin: "16px auto" }}> |
| 38 | <CardContent> |
| 39 | <Typography variant='h6'>Jami key ID</Typography> |
| 40 | <Typography variant='body1'>{contactId}</Typography> |
| 41 | <Box style={{textAlign: 'center', marginTop: 16}}> |
| 42 | <Fab variant='extended' color='primary' onClick={handleClick}> |
idillon | fb2af5b | 2022-09-16 13:40:08 -0400 | [diff] [blame] | 43 | <GroupAddRounded/> |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 44 | Add contact |
| 45 | </Fab> |
| 46 | </Box> |
| 47 | </CardContent> |
| 48 | </Card> |
| 49 | </Container>) |
| 50 | } |