blob: 2bd8ea9e61ab440611e81d1fcb2d7dca3eb4ec0d [file] [log] [blame]
Adrien BĂ©raud88a52442021-04-26 12:11:41 -04001import React, { useState } from 'react';
2import { Container, Card, CardContent, Typography, Fab, CardActions, Box } from '@material-ui/core';
3import { makeStyles } from '@material-ui/core/styles';
4import { AddRounded } from '@material-ui/icons';
5import UsernameChooser from '../components/UsernameChooser';
6import authManager from '../AuthManager'
7import { useHistory } from 'react-router';
8
9const 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
27export 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)
32 const history = useHistory()
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 },
43 body: JSON.stringify({
44 'Account.registeredName': name
45 })
46 })
47 .then(res => res.json())
48 .catch(error => {
49 setLoading(false)
50 setError(error)
51 })
52 console.log(result)
53 if (result && result.accountId)
54 history.replace(`/account/${result.accountId}/settings`)
55 }
56
57 return (
58 <Container>
59 <Card component="form" onSubmit={onSubmit} className={classes.wizardCard}>
60 <CardContent>
61 <Typography gutterBottom variant="h5" component="h2">
62 Create Jami account
63 </Typography>
64 <Typography variant="body2" color="textSecondary" component="p">
65 Welcome to the Jami web node setup.<br />
66 Let's start by creating a new administrator account to control access to the server configuration.
67 </Typography>
68
69 <Box className={classes.chooser} >
70 <UsernameChooser disabled={loading} setName={setName} />
71 </Box>
72 </CardContent>
73 <CardActions className={classes.actionArea}>
74 {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>}
75 <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}>
76 <AddRounded className={classes.extendedIcon} />
77 Register name
78 </Fab>
79 </CardActions>
80 </Card>
81 </Container>)
82}