blob: 8360e060aa1fab8514a7bdb9e0aff09a9ecaf973 [file] [log] [blame]
Adrien Béraudab519ff2022-05-03 15:34:48 -04001import { AddRounded } from '@mui/icons-material';
simon07b4eb02022-09-29 17:50:26 -04002import { Box, Card, CardActions, CardContent, Container, Fab, Typography } from '@mui/material';
simonfe1de722022-10-02 00:21:43 -04003import { FormEvent, useState } from 'react';
Adrien Béraudab519ff2022-05-03 15:34:48 -04004import { useNavigate } from 'react-router';
Adrien Béraud88a52442021-04-26 12:11:41 -04005
simon07b4eb02022-09-29 17:50:26 -04006import authManager from '../AuthManager';
7import UsernameChooser from '../components/UsernameChooser';
8
simonfe1de722022-10-02 00:21:43 -04009export default function JamiAccountDialog() {
simond47ef9e2022-09-28 22:24:28 -040010 const [name, setName] = useState('');
11 const [loading, setLoading] = useState(false);
12 const [error, setError] = useState(false);
13 const navigate = useNavigate();
Adrien Béraud88a52442021-04-26 12:11:41 -040014
simonfe1de722022-10-02 00:21:43 -040015 const onSubmit = async (event: FormEvent) => {
simond47ef9e2022-09-28 22:24:28 -040016 event.preventDefault();
17 setLoading(true);
18 const result = await authManager
19 .fetch('/api/accounts', {
20 method: 'POST',
21 headers: {
22 Accept: 'application/json',
23 'Content-Type': 'application/json',
24 },
25 body: JSON.stringify({ registerName: name }),
Adrien Béraud88a52442021-04-26 12:11:41 -040026 })
simond47ef9e2022-09-28 22:24:28 -040027 .then((res) => res.json())
28 .catch((error) => {
29 setLoading(false);
30 setError(error);
31 });
32 console.log(result);
33 if (result && result.accountId) navigate(`/account/${result.accountId}/settings`);
34 };
Adrien Béraud88a52442021-04-26 12:11:41 -040035
36 return (
37 <Container>
idillonfb2af5b2022-09-16 13:40:08 -040038 <Card component="form" onSubmit={onSubmit}>
Adrien Béraud88a52442021-04-26 12:11:41 -040039 <CardContent>
40 <Typography gutterBottom variant="h5" component="h2">
41 Create Jami account
42 </Typography>
43 <Typography variant="body2" color="textSecondary" component="p">
simond47ef9e2022-09-28 22:24:28 -040044 Welcome to the Jami web node setup.
45 <br />
simon80b7b3b2022-09-28 17:50:10 -040046 Let&apos;s start by creating a new administrator account to control access to the server configuration.
Adrien Béraud88a52442021-04-26 12:11:41 -040047 </Typography>
48
idillonfb2af5b2022-09-16 13:40:08 -040049 <Box>
Adrien Béraud88a52442021-04-26 12:11:41 -040050 <UsernameChooser disabled={loading} setName={setName} />
51 </Box>
52 </CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040053 <CardActions>
Adrien Béraud88a52442021-04-26 12:11:41 -040054 {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>}
55 <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}>
simond47ef9e2022-09-28 22:24:28 -040056 <AddRounded />
Adrien Béraud88a52442021-04-26 12:11:41 -040057 Register name
58 </Fab>
59 </CardActions>
60 </Card>
simond47ef9e2022-09-28 22:24:28 -040061 </Container>
62 );
Adrien Béraud88a52442021-04-26 12:11:41 -040063}