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 | */ |
| 18 | import { Box, Button, Stack, Typography, useMediaQuery } from '@mui/material'; |
| 19 | import { Theme, useTheme } from '@mui/material/styles'; |
| 20 | import { ChangeEvent, FormEvent, MouseEvent, useState } from 'react'; |
| 21 | import { Form } from 'react-router-dom'; |
| 22 | |
| 23 | import { PasswordInput, UsernameInput } from '../components/Input'; |
| 24 | import ProcessingRequest from '../components/ProcessingRequest'; |
| 25 | import { inputWidth } from '../utils/constants'; |
| 26 | |
| 27 | type JamiLoginProps = { |
| 28 | register: () => void; |
| 29 | }; |
| 30 | |
| 31 | export default function JamiLogin(props: JamiLoginProps) { |
| 32 | const theme: Theme = useTheme(); |
| 33 | const [username, setUsername] = useState<string>(''); |
| 34 | const [password, setPassword] = useState<string>(''); |
| 35 | const [isLoggingInUser, setIsLoggingInUser] = useState<boolean>(false); |
| 36 | |
| 37 | const handleUsername = (event: ChangeEvent<HTMLInputElement>) => { |
| 38 | setUsername(event.target.value); |
| 39 | }; |
| 40 | |
| 41 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
| 42 | setPassword(event.target.value); |
| 43 | }; |
| 44 | |
| 45 | const register = (event: MouseEvent<HTMLAnchorElement>) => { |
| 46 | event.preventDefault(); |
| 47 | props.register(); |
| 48 | }; |
| 49 | |
| 50 | const authenticateUser = async (event: FormEvent) => { |
| 51 | event.preventDefault(); |
| 52 | if (username.length > 0 && password.length > 0) { |
| 53 | setIsLoggingInUser(true); |
| 54 | |
| 55 | // TODO: Replace with login logic (https://git.jami.net/savoirfairelinux/jami-web/-/issues/75). |
| 56 | await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 57 | console.log('Login'); |
| 58 | setIsLoggingInUser(false); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
| 63 | |
| 64 | return ( |
| 65 | <> |
| 66 | <ProcessingRequest open={isLoggingInUser} /> |
| 67 | |
| 68 | <Stack |
| 69 | sx={{ |
| 70 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 71 | display: 'flex', |
| 72 | alignItems: 'center', |
| 73 | justifyContent: 'center', |
| 74 | }} |
| 75 | > |
| 76 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 77 | <Typography component={'span'} variant="h2"> |
| 78 | LOGIN |
| 79 | </Typography> |
| 80 | </Box> |
| 81 | |
| 82 | <Form method="post" id="login-form"> |
| 83 | <div> |
| 84 | <UsernameInput |
| 85 | onChange={handleUsername} |
| 86 | tooltipTitle={'The username you registered with'} |
| 87 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 88 | /> |
| 89 | </div> |
| 90 | <div> |
| 91 | <PasswordInput |
| 92 | onChange={handlePassword} |
| 93 | tooltipTitle={'The password you registered with'} |
| 94 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 95 | /> |
| 96 | </div> |
| 97 | |
| 98 | <Button |
| 99 | variant="contained" |
| 100 | type="submit" |
| 101 | onClick={authenticateUser} |
| 102 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 103 | > |
| 104 | LOG IN |
| 105 | </Button> |
| 106 | </Form> |
| 107 | |
| 108 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 109 | <Typography variant="body1"> |
| 110 | Need an account ? |
| 111 | <a href="" onClick={register}> |
| 112 | REGISTER |
| 113 | </a> |
| 114 | </Typography> |
| 115 | </Box> |
| 116 | </Stack> |
| 117 | </> |
| 118 | ); |
| 119 | } |