Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -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 | */ |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 18 | import { |
| 19 | Box, |
| 20 | Button, |
| 21 | FormControl, |
| 22 | FormControlLabel, |
| 23 | Radio, |
| 24 | RadioGroup, |
| 25 | Stack, |
| 26 | Typography, |
| 27 | useMediaQuery, |
| 28 | } from '@mui/material'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 29 | import { Theme, useTheme } from '@mui/material/styles'; |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 30 | import { ChangeEvent, FormEvent, useContext, useState } from 'react'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 31 | import { useTranslation } from 'react-i18next'; |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame] | 32 | import { Form, Link } from 'react-router-dom'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 33 | |
| 34 | import { PasswordInput, UsernameInput } from '../components/Input'; |
| 35 | import ProcessingRequest from '../components/ProcessingRequest'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 36 | import withAuthUI from '../components/WithAuthUI'; |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 37 | import { AlertSnackbarContext } from '../contexts/AlertSnackbarProvider'; |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame] | 38 | import { useLoginMutation } from '../services/authQueries'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 39 | import { inputWidth } from '../utils/constants'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 40 | function LoginForm() { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 41 | const theme: Theme = useTheme(); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 42 | const { t } = useTranslation(); |
| 43 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 44 | const [username, setUsername] = useState<string>(''); |
| 45 | const [password, setPassword] = useState<string>(''); |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 46 | const [isJams, setIsJams] = useState<boolean>(false); |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 47 | |
| 48 | const { setAlertContent } = useContext(AlertSnackbarContext); |
| 49 | |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame] | 50 | const loginMutation = useLoginMutation(); |
| 51 | const { isLoading } = loginMutation; |
| 52 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 53 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 54 | |
| 55 | const handleUsername = (event: ChangeEvent<HTMLInputElement>) => { |
| 56 | setUsername(event.target.value); |
| 57 | }; |
| 58 | |
| 59 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
| 60 | setPassword(event.target.value); |
| 61 | }; |
| 62 | |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 63 | const handleIsJams = (event: ChangeEvent<HTMLInputElement>) => { |
| 64 | setIsJams(event.target.value === 'true'); |
| 65 | }; |
| 66 | |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 67 | const login = (event: FormEvent) => { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 68 | event.preventDefault(); |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 69 | if (username === '') { |
| 70 | setAlertContent({ messageI18nKey: 'username_input_helper_text_empty', severity: 'error', alertOpen: true }); |
| 71 | return; |
| 72 | } |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 73 | if (password === '') { |
| 74 | setAlertContent({ messageI18nKey: 'password_input_helper_text_empty', severity: 'error', alertOpen: true }); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame] | 78 | loginMutation.mutate({ username, password, isJams }); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 81 | return ( |
| 82 | <> |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame] | 83 | <ProcessingRequest open={isLoading} /> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 84 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 85 | <Stack |
| 86 | sx={{ |
| 87 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 88 | display: 'flex', |
| 89 | alignItems: 'center', |
| 90 | justifyContent: 'center', |
| 91 | }} |
| 92 | > |
| 93 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 94 | <Typography component={'span'} variant="h2"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 95 | {t('login_form_title')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 96 | </Typography> |
| 97 | </Box> |
| 98 | |
| 99 | <Form method="post" id="login-form"> |
| 100 | <div> |
| 101 | <UsernameInput |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 102 | value={username} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 103 | onChange={handleUsername} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 104 | tooltipTitle={t('login_form_username_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 105 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 106 | /> |
| 107 | </div> |
| 108 | <div> |
| 109 | <PasswordInput |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 110 | value={password} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 111 | onChange={handlePassword} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 112 | tooltipTitle={t('login_form_password_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 113 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 114 | /> |
| 115 | </div> |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 116 | <div> |
| 117 | <FormControl |
| 118 | sx={{ |
| 119 | width: theme.typography.pxToRem(inputWidth), |
| 120 | alignItems: 'center', |
| 121 | justifyContent: 'space-between', |
| 122 | }} |
| 123 | > |
| 124 | <RadioGroup row onChange={handleIsJams} value={isJams}> |
| 125 | <FormControlLabel value="false" control={<Radio />} label={t('jami')} /> |
| 126 | <FormControlLabel value="true" control={<Radio />} label={t('jams')} /> |
| 127 | </RadioGroup> |
| 128 | </FormControl> |
| 129 | </div> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 130 | |
| 131 | <Button |
| 132 | variant="contained" |
| 133 | type="submit" |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 134 | onClick={login} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 135 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 136 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 137 | {t('login_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 138 | </Button> |
| 139 | </Form> |
| 140 | |
| 141 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 142 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 143 | {t('login_form_to_registration_text')} |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 144 | <Link to={'/register'}>{t('login_form_to_registration_link')}</Link> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 145 | </Typography> |
| 146 | </Box> |
| 147 | </Stack> |
| 148 | </> |
| 149 | ); |
| 150 | } |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 151 | |
| 152 | export default withAuthUI(LoginForm); |