blob: 53d3828344b28b2f9c8a5fd35c89b91d0188b3c9 [file] [log] [blame]
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -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 */
Gabriel Rochon7057b4f2022-11-21 13:28:01 -050018import {
19 Box,
20 Button,
21 FormControl,
22 FormControlLabel,
23 Radio,
24 RadioGroup,
25 Stack,
26 Typography,
27 useMediaQuery,
28} from '@mui/material';
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040029import { Theme, useTheme } from '@mui/material/styles';
Ziwei Wang49765ec2023-02-13 16:35:32 -050030import { ChangeEvent, FormEvent, useContext, useState } from 'react';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040031import { useTranslation } from 'react-i18next';
Ziwei Wang45baf532023-02-16 15:41:49 -050032import { Form, Link } from 'react-router-dom';
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040033
34import { PasswordInput, UsernameInput } from '../components/Input';
35import ProcessingRequest from '../components/ProcessingRequest';
Ziwei Wang3ce1ac02023-02-03 11:59:03 -050036import withAuthUI from '../components/WithAuthUI';
Ziwei Wang49765ec2023-02-13 16:35:32 -050037import { AlertSnackbarContext } from '../contexts/AlertSnackbarProvider';
Ziwei Wang45baf532023-02-16 15:41:49 -050038import { useLoginMutation } from '../services/authQueries';
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040039import { inputWidth } from '../utils/constants';
Ziwei Wang3ce1ac02023-02-03 11:59:03 -050040function LoginForm() {
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040041 const theme: Theme = useTheme();
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040042 const { t } = useTranslation();
43
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040044 const [username, setUsername] = useState<string>('');
45 const [password, setPassword] = useState<string>('');
Gabriel Rochon7057b4f2022-11-21 13:28:01 -050046 const [isJams, setIsJams] = useState<boolean>(false);
Ziwei Wang49765ec2023-02-13 16:35:32 -050047
48 const { setAlertContent } = useContext(AlertSnackbarContext);
49
Ziwei Wang45baf532023-02-16 15:41:49 -050050 const loginMutation = useLoginMutation();
51 const { isLoading } = loginMutation;
52
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050053 const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs'));
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040054
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 Rochon7057b4f2022-11-21 13:28:01 -050063 const handleIsJams = (event: ChangeEvent<HTMLInputElement>) => {
64 setIsJams(event.target.value === 'true');
65 };
66
Ziwei Wang49765ec2023-02-13 16:35:32 -050067 const login = (event: FormEvent) => {
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040068 event.preventDefault();
Ziwei Wang49765ec2023-02-13 16:35:32 -050069 if (username === '') {
70 setAlertContent({ messageI18nKey: 'username_input_helper_text_empty', severity: 'error', alertOpen: true });
71 return;
72 }
Ziwei Wang49765ec2023-02-13 16:35:32 -050073 if (password === '') {
74 setAlertContent({ messageI18nKey: 'password_input_helper_text_empty', severity: 'error', alertOpen: true });
Ziwei Wang9b4e2c12023-02-06 14:45:37 -050075 return;
76 }
77
Ziwei Wang45baf532023-02-16 15:41:49 -050078 loginMutation.mutate({ username, password, isJams });
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040079 };
80
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040081 return (
82 <>
Ziwei Wang45baf532023-02-16 15:41:49 -050083 <ProcessingRequest open={isLoading} />
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040084
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040085 <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 Sime559cc802022-11-05 12:06:40 -040095 {t('login_form_title')}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040096 </Typography>
97 </Box>
98
99 <Form method="post" id="login-form">
100 <div>
101 <UsernameInput
Larbi Gharibb38e3542022-12-07 06:09:10 -0500102 data-cy="username-input"
Ziwei Wang9b4e2c12023-02-06 14:45:37 -0500103 value={username}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400104 onChange={handleUsername}
Michelle Sepkap Sime559cc802022-11-05 12:06:40 -0400105 tooltipTitle={t('login_form_username_tooltip')}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400106 sx={{ width: theme.typography.pxToRem(inputWidth) }}
107 />
108 </div>
109 <div>
110 <PasswordInput
Larbi Gharibb38e3542022-12-07 06:09:10 -0500111 data-cy="password-input"
Ziwei Wang9b4e2c12023-02-06 14:45:37 -0500112 value={password}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400113 onChange={handlePassword}
Michelle Sepkap Sime559cc802022-11-05 12:06:40 -0400114 tooltipTitle={t('login_form_password_tooltip')}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400115 sx={{ width: theme.typography.pxToRem(inputWidth) }}
116 />
117 </div>
Gabriel Rochon7057b4f2022-11-21 13:28:01 -0500118 <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 Sime51c00452022-10-31 21:26:38 -0400132
133 <Button
Larbi Gharibb38e3542022-12-07 06:09:10 -0500134 data-cy="login-button"
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400135 variant="contained"
136 type="submit"
Ziwei Wang9b4e2c12023-02-06 14:45:37 -0500137 onClick={login}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400138 sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }}
idillonacebc512023-08-03 20:30:47 -0400139 disabled={username === '' || password === ''}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400140 >
Michelle Sepkap Sime559cc802022-11-05 12:06:40 -0400141 {t('login_form_submit_button')}
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400142 </Button>
143 </Form>
144
145 <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}>
146 <Typography variant="body1">
Michelle Sepkap Sime559cc802022-11-05 12:06:40 -0400147 {t('login_form_to_registration_text')} &nbsp;
Larbi Gharibb38e3542022-12-07 06:09:10 -0500148 <Link data-cy="register-link" to={'/register'}>
149 {t('login_form_to_registration_link')}
150 </Link>
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -0400151 </Typography>
152 </Box>
153 </Stack>
154 </>
155 );
156}
Ziwei Wang3ce1ac02023-02-03 11:59:03 -0500157
158export default withAuthUI(LoginForm);