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 | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 3 | import { useNavigate, useParams } 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 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 9 | type AddContactPageProps = { |
| 10 | accountId: string; |
| 11 | contactId: string; |
| 12 | }; |
| 13 | |
| 14 | export default function AddContactPage(props: AddContactPageProps) { |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 15 | const navigate = useNavigate(); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 16 | |
| 17 | const params = useParams(); |
| 18 | const accountId = props.accountId || params.accountId; |
| 19 | const contactId = props.contactId || params.contactId; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 20 | const dispatch = useAppDispatch(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 21 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 22 | const handleClick = async () => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 23 | 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éraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 36 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | console.log(response); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 38 | if (response.conversationId) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | navigate(`/account/${accountId}/conversation/${response.conversationId}`); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 40 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 41 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 42 | |
| 43 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | <Container className="messenger"> |
| 45 | <Card variant="outlined" style={{ borderRadius: 16, maxWidth: 560, margin: '16px auto' }}> |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 46 | <CardContent> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | <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éraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 54 | </Box> |
| 55 | </CardContent> |
| 56 | </Card> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 57 | </Container> |
| 58 | ); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 59 | } |