Ziwei Wang | 3d633dd | 2023-02-21 16:57:48 -0500 | [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 | */ |
| 18 | import GroupAddRounded from '@mui/icons-material/GroupAddRounded'; |
| 19 | import { Box, Card, CardContent, Container, Fab, Input, Typography } from '@mui/material'; |
| 20 | import { FormEvent, useContext, useState } from 'react'; |
| 21 | import { useTranslation } from 'react-i18next'; |
| 22 | import { Navigate } from 'react-router-dom'; |
| 23 | |
| 24 | import { AlertSnackbarContext } from '../contexts/AlertSnackbarProvider'; |
| 25 | import { useCheckAdminSetupStatusQuery, useSetupAdminMutation } from '../services/adminQueries'; |
| 26 | |
| 27 | export default function AdminSetup() { |
| 28 | const [password, setPassword] = useState(''); |
| 29 | const [passwordRepeat, setPasswordRepeat] = useState(''); |
| 30 | const { t } = useTranslation(); |
| 31 | |
| 32 | const { data: isSetupComplete } = useCheckAdminSetupStatusQuery(); |
| 33 | const setupAdminMutation = useSetupAdminMutation(); |
| 34 | const { isLoading } = setupAdminMutation; |
| 35 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 36 | |
| 37 | const handleSubmit = (e: FormEvent) => { |
| 38 | e.preventDefault(); |
| 39 | |
| 40 | if (isSetupComplete) { |
| 41 | setAlertContent({ |
| 42 | messageI18nKey: 'admin_page_setup_complete', |
| 43 | severity: 'error', |
| 44 | alertOpen: true, |
| 45 | }); |
| 46 | return; |
| 47 | } |
| 48 | if (!password) { |
| 49 | setAlertContent({ |
| 50 | messageI18nKey: 'password_input_helper_text_empty', |
| 51 | severity: 'error', |
| 52 | alertOpen: true, |
| 53 | }); |
| 54 | return; |
| 55 | } |
| 56 | if (password !== passwordRepeat) { |
| 57 | setAlertContent({ |
| 58 | messageI18nKey: 'password_input_helper_text_not_match', |
| 59 | severity: 'error', |
| 60 | alertOpen: true, |
| 61 | }); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | setupAdminMutation.mutate(password); |
| 66 | }; |
| 67 | |
| 68 | if (isSetupComplete) { |
| 69 | setAlertContent({ |
| 70 | messageI18nKey: 'redirect_admin_setup_complete', |
| 71 | severity: 'info', |
| 72 | alertOpen: true, |
| 73 | }); |
| 74 | return <Navigate to={'/admin/login'} />; |
| 75 | } |
| 76 | |
| 77 | if (isSetupComplete === false) { |
| 78 | return ( |
| 79 | <Container className="message-list"> |
| 80 | <Card> |
| 81 | <CardContent component="form" onSubmit={handleSubmit}> |
| 82 | <Typography gutterBottom variant="h5" component="h2"> |
| 83 | {t('admin_page_setup_title')} |
| 84 | </Typography> |
| 85 | <Box style={{ textAlign: 'center', marginTop: 8, marginBottom: 16 }}> |
| 86 | <div> |
| 87 | <Input value="admin" name="username" autoComplete="username" disabled /> |
| 88 | </div> |
| 89 | <div> |
| 90 | <Input |
| 91 | value={password} |
| 92 | onChange={(e) => setPassword(e.target.value)} |
| 93 | name="password" |
| 94 | type="password" |
| 95 | placeholder={t('admin_page_password_placeholder')} |
| 96 | autoComplete="new-password" |
| 97 | disabled={isLoading} |
| 98 | /> |
| 99 | </div> |
| 100 | <div> |
| 101 | <Input |
| 102 | value={passwordRepeat} |
| 103 | onChange={(e) => setPasswordRepeat(e.target.value)} |
| 104 | name="password" |
| 105 | error={passwordRepeat !== password} |
| 106 | type="password" |
| 107 | placeholder={t('admin_page_password_repeat_placeholder')} |
| 108 | autoComplete="new-password" |
| 109 | disabled={isLoading} |
| 110 | /> |
| 111 | </div> |
| 112 | </Box> |
| 113 | <Box style={{ textAlign: 'center', marginTop: 24 }}> |
| 114 | <Fab variant="extended" color="primary" type="submit" disabled={isLoading}> |
| 115 | <GroupAddRounded /> |
| 116 | {t('admin_page_create_button')} |
| 117 | </Fab> |
| 118 | </Box> |
| 119 | </CardContent> |
| 120 | </Card> |
| 121 | </Container> |
| 122 | ); |
| 123 | } |
| 124 | //when isSetupComplete is undefined |
| 125 | else { |
| 126 | return <div>{t('loading')}</div>; |
| 127 | } |
| 128 | } |