46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
import styles from './Form.module.scss';
|
|
import { publicRuntimeConfig } from '@/config/runtime';
|
|
import Button from '@/elements/Button';
|
|
import Error from '@/elements/Error';
|
|
import Input from '@/elements/Input';
|
|
import axios from 'axios';
|
|
import { useState } from 'react';
|
|
|
|
const { APP_BASE_PATH } = publicRuntimeConfig;
|
|
|
|
export default function Form() {
|
|
const [hasError, setHasError] = useState(false);
|
|
const error = hasError ? <Error>Неверный логин или пароль</Error> : null;
|
|
|
|
return (
|
|
<form
|
|
className={styles.form}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
|
|
const login = e.target[0].value;
|
|
const password = e.target[1].value;
|
|
const data = { login, password };
|
|
|
|
axios
|
|
.post('/login', data)
|
|
.then(() => {
|
|
const url =
|
|
(window.location.pathname.replace(APP_BASE_PATH, '') || '/') +
|
|
(window.location.search || '');
|
|
|
|
window.location.replace(url);
|
|
})
|
|
.catch(() => {
|
|
setHasError(true);
|
|
});
|
|
}}
|
|
>
|
|
<Input name="login" type="text" placeholder="Логин" required autoComplete="on" />
|
|
<Input name="password" type="password" placeholder="Пароль" required autoComplete="on" />
|
|
{error}
|
|
<Button>Войти</Button>
|
|
</form>
|
|
);
|
|
}
|