blob: da80533215cc0d3fd1e882890cf2bf5260bc15a4 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
Adrien Béraudab519ff2022-05-03 15:34:48 -040018import { AddRounded } from '@mui/icons-material';
simon07b4eb02022-09-29 17:50:26 -040019import { Box, Card, CardActions, CardContent, Container, Fab, Typography } from '@mui/material';
simonfe1de722022-10-02 00:21:43 -040020import { FormEvent, useState } from 'react';
Adrien Béraudab519ff2022-05-03 15:34:48 -040021import { useNavigate } from 'react-router';
Adrien Béraud88a52442021-04-26 12:11:41 -040022
simon07b4eb02022-09-29 17:50:26 -040023import authManager from '../AuthManager';
24import UsernameChooser from '../components/UsernameChooser';
25
simonfe1de722022-10-02 00:21:43 -040026export default function JamiAccountDialog() {
simond47ef9e2022-09-28 22:24:28 -040027 const [name, setName] = useState('');
28 const [loading, setLoading] = useState(false);
29 const [error, setError] = useState(false);
30 const navigate = useNavigate();
Adrien Béraud88a52442021-04-26 12:11:41 -040031
simonfe1de722022-10-02 00:21:43 -040032 const onSubmit = async (event: FormEvent) => {
simond47ef9e2022-09-28 22:24:28 -040033 event.preventDefault();
34 setLoading(true);
35 const result = await authManager
36 .fetch('/api/accounts', {
37 method: 'POST',
38 headers: {
39 Accept: 'application/json',
40 'Content-Type': 'application/json',
41 },
42 body: JSON.stringify({ registerName: name }),
Adrien Béraud88a52442021-04-26 12:11:41 -040043 })
simond47ef9e2022-09-28 22:24:28 -040044 .then((res) => res.json())
45 .catch((error) => {
46 setLoading(false);
47 setError(error);
48 });
49 console.log(result);
50 if (result && result.accountId) navigate(`/account/${result.accountId}/settings`);
51 };
Adrien Béraud88a52442021-04-26 12:11:41 -040052
53 return (
54 <Container>
idillonfb2af5b2022-09-16 13:40:08 -040055 <Card component="form" onSubmit={onSubmit}>
Adrien Béraud88a52442021-04-26 12:11:41 -040056 <CardContent>
57 <Typography gutterBottom variant="h5" component="h2">
58 Create Jami account
59 </Typography>
60 <Typography variant="body2" color="textSecondary" component="p">
simond47ef9e2022-09-28 22:24:28 -040061 Welcome to the Jami web node setup.
62 <br />
simon80b7b3b2022-09-28 17:50:10 -040063 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 -040064 </Typography>
65
idillonfb2af5b2022-09-16 13:40:08 -040066 <Box>
Adrien Béraud88a52442021-04-26 12:11:41 -040067 <UsernameChooser disabled={loading} setName={setName} />
68 </Box>
69 </CardContent>
idillonfb2af5b2022-09-16 13:40:08 -040070 <CardActions>
Adrien Béraud88a52442021-04-26 12:11:41 -040071 {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>}
72 <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}>
simond47ef9e2022-09-28 22:24:28 -040073 <AddRounded />
Adrien Béraud88a52442021-04-26 12:11:41 -040074 Register name
75 </Fab>
76 </CardActions>
77 </Card>
simond47ef9e2022-09-28 22:24:28 -040078 </Container>
79 );
Adrien Béraud88a52442021-04-26 12:11:41 -040080}