blob: 8d47b48f618cc50c08abfda6fcd0cf4d8a4e5cce [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import { 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';
simond47ef9e2022-09-28 22:24:28 -04005import 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) {
simond47ef9e2022-09-28 22:24:28 -04009 const [name, setName] = useState('');
10 const [loading, setLoading] = useState(false);
11 const [error, setError] = useState(false);
12 const navigate = useNavigate();
Adrien Béraud88a52442021-04-26 12:11:41 -040013
simond47ef9e2022-09-28 22:24:28 -040014 const onSubmit = async (event) => {
15 event.preventDefault();
16 setLoading(true);
17 const result = await authManager
18 .fetch('/api/accounts', {
19 method: 'POST',
20 headers: {
21 Accept: 'application/json',
22 'Content-Type': 'application/json',
23 },
24 body: JSON.stringify({ registerName: name }),
Adrien Béraud88a52442021-04-26 12:11:41 -040025 })
simond47ef9e2022-09-28 22:24:28 -040026 .then((res) => res.json())
27 .catch((error) => {
28 setLoading(false);
29 setError(error);
30 });
31 console.log(result);
32 if (result && result.accountId) navigate(`/account/${result.accountId}/settings`);
33 };
Adrien Béraud88a52442021-04-26 12:11:41 -040034
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">
simond47ef9e2022-09-28 22:24:28 -040043 Welcome to the Jami web node setup.
44 <br />
Adrien Béraud88a52442021-04-26 12:11:41 -040045 Let's start by creating a new administrator account to control access to the server configuration.
46 </Typography>
47
idillonfb2af5b2022-09-16 13:40:08 -040048 <Box>
Adrien Béraud88a52442021-04-26 12:11:41 -040049 <UsernameChooser disabled={loading} setName={setName} />
50 </Box>
51 </CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040052 <CardActions>
Adrien Béraud88a52442021-04-26 12:11:41 -040053 {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>}
54 <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}>
simond47ef9e2022-09-28 22:24:28 -040055 <AddRounded />
Adrien Béraud88a52442021-04-26 12:11:41 -040056 Register name
57 </Fab>
58 </CardActions>
59 </Card>
simond47ef9e2022-09-28 22:24:28 -040060 </Container>
61 );
Adrien Béraud88a52442021-04-26 12:11:41 -040062}