simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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éraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 18 | import { AddRounded } from '@mui/icons-material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { Box, Card, CardActions, CardContent, Container, Fab, Typography } from '@mui/material'; |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 20 | import { FormEvent, useState } from 'react'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 21 | import { useNavigate } from 'react-router'; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 22 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import authManager from '../AuthManager'; |
| 24 | import UsernameChooser from '../components/UsernameChooser'; |
| 25 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 26 | export default function JamiAccountDialog() { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 27 | const [name, setName] = useState(''); |
| 28 | const [loading, setLoading] = useState(false); |
| 29 | const [error, setError] = useState(false); |
| 30 | const navigate = useNavigate(); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 31 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 32 | const onSubmit = async (event: FormEvent) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 33 | 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éraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 43 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | .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éraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 52 | |
| 53 | return ( |
| 54 | <Container> |
idillon | fb2af5b | 2022-09-16 13:40:08 -0400 | [diff] [blame] | 55 | <Card component="form" onSubmit={onSubmit}> |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 56 | <CardContent> |
| 57 | <Typography gutterBottom variant="h5" component="h2"> |
| 58 | Create Jami account |
| 59 | </Typography> |
| 60 | <Typography variant="body2" color="textSecondary" component="p"> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 61 | Welcome to the Jami web node setup. |
| 62 | <br /> |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 63 | Let's start by creating a new administrator account to control access to the server configuration. |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 64 | </Typography> |
| 65 | |
idillon | fb2af5b | 2022-09-16 13:40:08 -0400 | [diff] [blame] | 66 | <Box> |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 67 | <UsernameChooser disabled={loading} setName={setName} /> |
| 68 | </Box> |
| 69 | </CardContent> |
idillon | fb2af5b | 2022-09-16 13:40:08 -0400 | [diff] [blame] | 70 | <CardActions> |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 71 | {error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>} |
| 72 | <Fab color="primary" type="submit" variant="extended" disabled={!name || loading}> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | <AddRounded /> |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 74 | Register name |
| 75 | </Fab> |
| 76 | </CardActions> |
| 77 | </Card> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | </Container> |
| 79 | ); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 80 | } |