simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 1 | import GroupAddRounded from '@mui/icons-material/GroupAddRounded'; |
| 2 | import { Box, Card, CardContent, Container, Fab, Typography } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 3 | import { useNavigate } from 'react-router-dom'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 4 | |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 5 | import { setRefreshFromSlice } from '../../redux/appSlice'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 6 | import { useAppDispatch } from '../../redux/hooks'; |
| 7 | import authManager from '../AuthManager'; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 8 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 9 | export default function AddContactPage(props) { |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 10 | const navigate = useNavigate(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 11 | const accountId = props.accountId || props.match.params.accountId; |
| 12 | const contactId = props.contactId || props.match.params.contactId; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 13 | const dispatch = useAppDispatch(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 14 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 15 | 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éraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 29 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | console.log(response); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 31 | if (response.conversationId) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 32 | navigate(`/account/${accountId}/conversation/${response.conversationId}`); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 33 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 34 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 35 | |
| 36 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | <Container className="messenger"> |
| 38 | <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}> |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 39 | <CardContent> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 40 | <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éraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 47 | </Box> |
| 48 | </CardContent> |
| 49 | </Card> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | </Container> |
| 51 | ); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 52 | } |