import ForwardIcon from '@mui/icons-material/Forward'; import { Box, Fab, Paper, Typography, Button } from '@mui/material'; import { useContext, useState } from 'react'; import { toast } from 'react-toastify'; import type { ValidateFieldsError } from 'async-validator'; import type { Locales } from 'i18n/i18n-types'; import type { FC } from 'react'; import type { SignInRequest } from 'types'; import * as AuthenticationApi from 'api/authentication'; import { PROJECT_NAME } from 'api/env'; import { ValidatedTextField } from 'components'; import { AuthenticationContext } from 'contexts/authentication'; import { ReactComponent as DEflag } from 'i18n/DE.svg'; import { ReactComponent as FRflag } from 'i18n/FR.svg'; import { ReactComponent as GBflag } from 'i18n/GB.svg'; import { ReactComponent as NLflag } from 'i18n/NL.svg'; import { ReactComponent as NOflag } from 'i18n/NO.svg'; import { ReactComponent as PLflag } from 'i18n/PL.svg'; import { ReactComponent as SVflag } from 'i18n/SV.svg'; import { ReactComponent as TRflag } from 'i18n/TR.svg'; import { I18nContext } from 'i18n/i18n-react'; import { loadLocaleAsync } from 'i18n/i18n-util.async'; import { extractErrorMessage, onEnterCallback, updateValue } from 'utils'; import { SIGN_IN_REQUEST_VALIDATOR, validate } from 'validators'; const SignIn: FC = () => { const authenticationContext = useContext(AuthenticationContext); const { LL, setLocale, locale } = useContext(I18nContext); const [signInRequest, setSignInRequest] = useState({ username: '', password: '' }); const [processing, setProcessing] = useState(false); const [fieldErrors, setFieldErrors] = useState(); const updateLoginRequestValue = updateValue(setSignInRequest); const signIn = async () => { try { const { data: loginResponse } = await AuthenticationApi.signIn(signInRequest); authenticationContext.signIn(loginResponse.access_token); } catch (error) { if (error.response) { if (error.response?.status === 401) { toast.warn(LL.INVALID_LOGIN()); } } else { toast.error(extractErrorMessage(error, LL.ERROR())); } setProcessing(false); } }; const validateAndSignIn = async () => { setProcessing(true); SIGN_IN_REQUEST_VALIDATOR.messages({ required: LL.IS_REQUIRED('%s') }); try { await validate(SIGN_IN_REQUEST_VALIDATOR, signInRequest); await signIn(); } catch (errors: any) { setFieldErrors(errors); setProcessing(false); } }; const submitOnEnter = onEnterCallback(signIn); const selectLocale = async (loc: Locales) => { localStorage.setItem('lang', loc); await loadLocaleAsync(loc); setLocale(loc); }; return ( theme.breakpoints.values.sm} > ({ textAlign: 'center', padding: theme.spacing(2), paddingTop: '172px', backgroundImage: 'url("/app/icon.png")', backgroundRepeat: 'no-repeat', backgroundPosition: '50% ' + theme.spacing(2), width: '100%' })} > {PROJECT_NAME} {LL.SIGN_IN()} ); }; export default SignIn;