blob: ac5976253e8dd67e5dd3ef9c30ce910b4a0ee6bb [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 },
Adrien Béraud0561d3c2021-05-02 11:23:54 -040043 body: JSON.stringify({ registerName: name })
Adrien Béraud88a52442021-04-26 12:11:41 -040044 })
45 .then(res => res.json())
46 .catch(error => {
47 setLoading(false)
48 setError(error)
49 })
50 console.log(result)
51 if (result && result.accountId)
52 history.replace(`/account/${result.accountId}/settings`)
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}