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 |
Larbi Gharib | b38e354 | 2022-12-07 06:09:10 -0500 | [diff] [blame] | 102 | data-cy="username-input" |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 103 | value={username} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 104 | onChange={handleUsername} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 105 | tooltipTitle={t('login_form_username_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 106 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 107 | /> |
| 108 | </div> |
| 109 | <div> |
| 110 | <PasswordInput |
Larbi Gharib | b38e354 | 2022-12-07 06:09:10 -0500 | [diff] [blame] | 111 | data-cy="password-input" |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 112 | value={password} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 113 | onChange={handlePassword} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 114 | tooltipTitle={t('login_form_password_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 115 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 116 | /> |
| 117 | </div> |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 118 | <div> |
| 119 | <FormControl |
| 120 | sx={{ |
| 121 | width: theme.typography.pxToRem(inputWidth), |
| 122 | alignItems: 'center', |
| 123 | justifyContent: 'space-between', |
| 124 | }} |
| 125 | > |
| 126 | <RadioGroup row onChange={handleIsJams} value={isJams}> |
| 127 | <FormControlLabel value="false" control={<Radio />} label={t('jami')} /> |
| 128 | <FormControlLabel value="true" control={<Radio />} label={t('jams')} /> |
| 129 | </RadioGroup> |
| 130 | </FormControl> |
| 131 | </div> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 132 | |
| 133 | <Button |
Larbi Gharib | b38e354 | 2022-12-07 06:09:10 -0500 | [diff] [blame] | 134 | data-cy="login-button" |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 135 | variant="contained" |
| 136 | type="submit" |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 137 | onClick={login} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 138 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
idillon | acebc51 | 2023-08-03 20:30:47 -0400 | [diff] [blame^] | 139 | disabled={username === '' || password === ''} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 140 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 141 | {t('login_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 142 | </Button> |
| 143 | </Form> |
| 144 | |
| 145 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 146 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 147 | {t('login_form_to_registration_text')} |
Larbi Gharib | b38e354 | 2022-12-07 06:09:10 -0500 | [diff] [blame] | 148 | <Link data-cy="register-link" to={'/register'}> |
| 149 | {t('login_form_to_registration_link')} |
| 150 | </Link> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 151 | </Typography> |
| 152 | </Box> |
| 153 | </Stack> |
| 154 | </> |
| 155 | ); |
| 156 | } |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 157 | |
| 158 | export default withAuthUI(LoginForm); |