blob: acecd27ccb7ac956e97ab3497b5d65349fbd8b5e [file] [log] [blame]
Adrien Béraud88a52442021-04-26 12:11:41 -04001import React, { useState } from 'react';
Adrien Béraudab519ff2022-05-03 15:34:48 -04002import { Container, Card, CardContent, Typography, Fab, CardActions, Box } from '@mui/material';
Adrien Béraudab519ff2022-05-03 15:34:48 -04003import { AddRounded } from '@mui/icons-material';
Adrien Béraud88a52442021-04-26 12:11:41 -04004import UsernameChooser from '../components/UsernameChooser';
5import authManager from '../AuthManager'
Adrien Béraudab519ff2022-05-03 15:34:48 -04006import { useNavigate } from 'react-router';
Adrien Béraud88a52442021-04-26 12:11:41 -04007
Adrien Béraud88a52442021-04-26 12:11:41 -04008export default function JamiAccountDialog(props) {
Adrien Béraud88a52442021-04-26 12:11:41 -04009 const [name, setName] = useState('')
10 const [loading, setLoading] = useState(false)
11 const [error, setError] = useState(false)
Adrien Béraudab519ff2022-05-03 15:34:48 -040012 const navigate = useNavigate()
Adrien Béraud88a52442021-04-26 12:11:41 -040013
14 const onSubmit = async event => {
15 event.preventDefault()
16 setLoading(true)
17 const result = await authManager.fetch('/api/accounts', {
18 method: 'POST',
19 headers: {
20 'Accept': 'application/json',
21 'Content-Type': 'application/json'
22 },
Adrien Béraud0561d3c2021-05-02 11:23:54 -040023 body: JSON.stringify({ registerName: name })
Adrien Béraud88a52442021-04-26 12:11:41 -040024 })
25 .then(res => res.json())
26 .catch(error => {
27 setLoading(false)
28 setError(error)
29 })
30 console.log(result)
31 if (result && result.accountId)
Adrien Béraudab519ff2022-05-03 15:34:48 -040032 navigate(`/account/${result.accountId}/settings`)
Adrien Béraud88a52442021-04-26 12:11:41 -040033 }
34
35 return (
36 <Container>
idillonfb2af5b2022-09-16 13:40:08 -040037 <Card component="form" onSubmit={onSubmit}>
Adrien Béraud88a52442021-04-26 12:11:41 -040038 <CardContent>
39 <Typography gutterBottom variant="h5" component="h2">
40 Create Jami account
41 </Typography>
42 <Typography variant="body2" color="textSecondary" component="p">
43 Welcome to the Jami web node setup.<br />
44 Let's start by creating a new administrator account to control access to the server configuration.
45 </Typography>
46
idillonfb2af5b2022-09-16 13:40:08 -040047 <Box>
Adrien Béraud88a52442021-04-26 12:11:41 -040048 <UsernameChooser disabled={loading} setName={setName} />
49 </Box>
50 </CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040051 <CardActions>
Adrien Béraud88a52442021-04-26 12:11:41 -040052 {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>}
53 <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}>
idillonfb2af5b2022-09-16 13:40:08 -040054 <AddRounded/>
Adrien Béraud88a52442021-04-26 12:11:41 -040055 Register name
56 </Fab>
57 </CardActions>
58 </Card>
59 </Container>)
60}