MSP-3103 add LoginPage

This commit is contained in:
Anastasiia committed 2024-04-08 17:14:41 +03:00
1 parent d8ea9dc7d2
commit e442fb187c
2 files changed
+110 -13

No files matched your search

+108 -11
View File
@@ -1,21 +1,118 @@
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import { Button, Divider } from 'antd'
import { AxiosError } from 'axios'
import { FormComponents, useAuth } from 'isp-ui-kit'
import { useForm } from 'react-hook-form'
import { useNavigate } from 'react-router-dom'
import { apiPaths } from '@constants/api/apiPaths.ts'
import { ValidationRules } from '@constants/form/validationRules.ts'
import { localStorageKeys } from '@constants/localStorageKeys.ts'
import { messages } from '@constants/messages.ts'
import { getConfigProperty } from '@utils/configUtils.ts'
import { LocalStorage } from '@utils/localStorageUtils.ts'
import { routePaths } from '@routes/routePaths.ts'
import { MSPError } from '@type/index.ts'
import { LoginRequest } from '@type/login.type.ts'
import './login-page.scss'
const passwordLoginEnabled = getConfigProperty('ENABLE_PASSWORD_LOGIN', true)
const LoginPage = () => {
const { login, isLoading } = useAuth()
const { handleSubmit, control, setError } = useForm<LoginRequest>({
mode: 'onChange'
})
const navigate = useNavigate()
const setFormError = (statusText: string, message: string): void => {
setError('email', {})
setError('password', {
message,
type: statusText
})
}
const handleError = (err: AxiosError<MSPError>): void => {
const { response } = err
if (response && response.status === 401) {
setFormError(response.statusText, messages.loginError)
} else {
setFormError('serverError', messages.serverError)
}
}
const handleSubmitForm = (data: LoginRequest): void => {
login(apiPaths.login, data, {
'X-APPLICATION-TOKEN': getConfigProperty(
'APP_TOKEN',
import.meta.env.VITE_APP_TOKEN
)
})
.then((response) => {
LocalStorage.set(localStorageKeys.HEADER_NAME, response.headerName)
LocalStorage.set(localStorageKeys.USER_TOKEN, response.token)
const redirectUrl =
localStorage.getItem('redirectUrl') || routePaths.home
navigate(redirectUrl, { replace: true })
localStorage.removeItem('redirectUrl')
})
.catch((err: any) => handleError(err))
}
const renderInternalAuthForm = () => {
if (!passwordLoginEnabled) {
return null
}
return (
<>
<FormComponents.FormInput
data-cy="email-input"
control={control}
name="email"
className="login-page__content__email-field"
prefix={<UserOutlined className="color" />}
placeholder="Логин"
rules={{ required: ValidationRules.required }}
/>
<FormComponents.FormInput
data-cy="password-input"
control={control}
name="password"
className="login-page__content__password-field"
prefix={<LockOutlined className="color" />}
type="password"
placeholder="Пароль"
rules={{ required: ValidationRules.required }}
/>
<Button
data-cy="submit-btn"
disabled={isLoading}
loading={isLoading}
type="primary"
className="login-page__content__submit-btn"
onClick={handleSubmit(handleSubmitForm)}
>
Войти
</Button>
</>
)
}
return (
<main className="login-page">
<button
onClick={() => {
const url = localStorage.getItem('redirectUrl')
if (url) {
navigate(url)
localStorage.removeItem('redirectUrl')
}
}}
>
Зарегистрироваться
</button>
<form className="login-page__content">
<h1 data-cy="login-title" className="login-page__content__title">
Вход в систему
</h1>
<Divider />
{renderInternalAuthForm()}
</form>
</main>
)
}
+2 -2
View File
@@ -3742,8 +3742,8 @@ isexe@^2.0.0:
integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==
"isp-ui-kit@https://github.com/txix-open/isp-ui-kit":
version "0.3.2"
resolved "https://github.com/txix-open/isp-ui-kit#dbbe386bfa9e870cf27d483dc8dcd9ae1494a51a"
version "0.3.3"
resolved "https://github.com/txix-open/isp-ui-kit#508a5bd9cb4efac0e9389ad2f9f8c60683d1712e"
dependencies:
"@codemirror/lang-javascript" "^6.2.1"
"@codemirror/lang-json" "^6.0.1"