Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 1 | import React, { useState } from 'react'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 2 | import { Container, Card, CardContent, Typography, Fab, CardActions, Box } from '@mui/material'; |
| 3 | import makeStyles from '@mui/styles/makeStyles'; |
| 4 | import { AddRounded } from '@mui/icons-material'; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 5 | import UsernameChooser from '../components/UsernameChooser'; |
| 6 | import authManager from '../AuthManager' |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 7 | import { useNavigate } from 'react-router'; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 8 | |
| 9 | const useStyles = makeStyles((theme) => ({ |
| 10 | extendedIcon: { |
| 11 | marginRight: theme.spacing(1), |
| 12 | }, |
| 13 | wizardCard: { |
| 14 | borderRadius: 8, |
| 15 | maxWidth: 360, |
| 16 | margin: "16px auto" |
| 17 | }, |
| 18 | actionArea: { |
| 19 | textAlign: 'center', |
| 20 | display: 'block' |
| 21 | }, |
| 22 | chooser: { |
| 23 | marginTop: 16 |
| 24 | } |
| 25 | })) |
| 26 | |
| 27 | export default function JamiAccountDialog(props) { |
| 28 | const classes = useStyles() |
| 29 | const [name, setName] = useState('') |
| 30 | const [loading, setLoading] = useState(false) |
| 31 | const [error, setError] = useState(false) |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 32 | const navigate = useNavigate() |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 33 | |
| 34 | const onSubmit = async event => { |
| 35 | event.preventDefault() |
| 36 | setLoading(true) |
| 37 | const result = await authManager.fetch('/api/accounts', { |
| 38 | method: 'POST', |
| 39 | headers: { |
| 40 | 'Accept': 'application/json', |
| 41 | 'Content-Type': 'application/json' |
| 42 | }, |
Adrien Béraud | 0561d3c | 2021-05-02 11:23:54 -0400 | [diff] [blame] | 43 | body: JSON.stringify({ registerName: name }) |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 44 | }) |
| 45 | .then(res => res.json()) |
| 46 | .catch(error => { |
| 47 | setLoading(false) |
| 48 | setError(error) |
| 49 | }) |
| 50 | console.log(result) |
| 51 | if (result && result.accountId) |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 52 | navigate(`/account/${result.accountId}/settings`) |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return ( |
| 56 | <Container> |
| 57 | <Card component="form" onSubmit={onSubmit} className={classes.wizardCard}> |
| 58 | <CardContent> |
| 59 | <Typography gutterBottom variant="h5" component="h2"> |
| 60 | Create Jami account |
| 61 | </Typography> |
| 62 | <Typography variant="body2" color="textSecondary" component="p"> |
| 63 | Welcome to the Jami web node setup.<br /> |
| 64 | Let's start by creating a new administrator account to control access to the server configuration. |
| 65 | </Typography> |
| 66 | |
| 67 | <Box className={classes.chooser} > |
| 68 | <UsernameChooser disabled={loading} setName={setName} /> |
| 69 | </Box> |
| 70 | </CardContent> |
| 71 | <CardActions className={classes.actionArea}> |
| 72 | {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>} |
| 73 | <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}> |
| 74 | <AddRounded className={classes.extendedIcon} /> |
| 75 | Register name |
| 76 | </Fab> |
| 77 | </CardActions> |
| 78 | </Card> |
| 79 | </Container>) |
| 80 | } |