регистрация

This commit is contained in:
DyCTaTOR 2024-01-11 00:04:41 +04:00
parent 81f072131f
commit 658ae323ba
5 changed files with 42 additions and 29 deletions

View File

@ -5,6 +5,12 @@
"login": "dyctator", "login": "dyctator",
"password": "12345", "password": "12345",
"role": "user" "role": "user"
},
{
"login": "user",
"password": "123",
"role": "user",
"id": 1
} }
], ],
"directions": [ "directions": [

View File

@ -4,16 +4,19 @@ import EntrysDataApiService from '../service/EntrysDataApiService';
const useEntrysData = (login, password) => { const useEntrysData = (login, password) => {
const [entrys, setEntrys] = useState([]); const [entrys, setEntrys] = useState([]);
const getLogin = async () => { const getEntrysData = async () => {
const expand = `?login=${login}`; let expand = `?login=${login}&password=${password}`;
const data = await EntrysDataApiService.getAll(); if (password === '') {
expand = `?login=${login}`;
}
const data = await EntrysDataApiService.getAll(expand);
setEntrys(data ?? []); setEntrys(data ?? []);
}; };
useEffect(() => { useEffect(() => {
getEntrysData(); getEntrysData();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, [login, password]);
return { return {
entrys, entrys,

View File

@ -2,11 +2,9 @@ import { useState } from 'react';
import EntrysDataApiService from '../service/EntrysDataApiService'; import EntrysDataApiService from '../service/EntrysDataApiService';
import useEntysDataItem from './DataItemHook'; import useEntysDataItem from './DataItemHook';
const useLinesItemForm = (id, linesChangeHandle) => { const useEntrysItemForm = (id) => {
const { item, setItem } = useEntysDataItem(id); const { item, setItem } = useEntysDataItem(id);
const [modified, setModified] = useState(false);
const [validated, setValidated] = useState(false); const [validated, setValidated] = useState(false);
const resetValidity = () => { const resetValidity = () => {
@ -16,7 +14,7 @@ const useLinesItemForm = (id, linesChangeHandle) => {
const getLineObject = (formData) => { const getLineObject = (formData) => {
const Login = formData.login.toString(); const Login = formData.login.toString();
const Password = formData.password.toString(); const Password = formData.password.toString();
const Role = formData.role.toString(); const Role = 'user';
return { return {
login: Login, login: Login,
password: Password, password: Password,
@ -44,8 +42,6 @@ const useLinesItemForm = (id, linesChangeHandle) => {
} else { } else {
await EntrysDataApiService.update(id, body); await EntrysDataApiService.update(id, body);
} }
if (linesChangeHandle) linesChangeHandle();
setModified(true);
return true; return true;
} }
setValidated(true); setValidated(true);
@ -55,11 +51,10 @@ const useLinesItemForm = (id, linesChangeHandle) => {
return { return {
item, item,
validated, validated,
handleSubmit, UseHandleSubmit: handleSubmit,
handleChange, UseHandleChange: handleChange,
resetValidity, resetValidity,
isModified: modified,
}; };
}; };
export default useLinesItemForm; export default useEntrysItemForm;

View File

@ -6,11 +6,11 @@ import Input from '../../input/Input.jsx';
import useEntrysData from '../hooks/EntrysDataHook'; import useEntrysData from '../hooks/EntrysDataHook';
const Entry = () => { const Entry = () => {
const { entrys } = useEntrysData();
const [validated, setValidated] = useState(false); const [validated, setValidated] = useState(false);
const [login, setLogin] = useState(''); const [login, setLogin] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const isLoginValid = (value) => /^[a-zA-Z]+$/.test(value); const isLoginValid = (value) => /^[a-zA-Z]+$/.test(value);
const { entrys } = useEntrysData(login, password);
const handleSubmit = (event) => { const handleSubmit = (event) => {
const form = event.currentTarget; const form = event.currentTarget;
@ -18,19 +18,14 @@ const Entry = () => {
event.stopPropagation(); event.stopPropagation();
if (form.checkValidity() !== false) { if (form.checkValidity() !== false) {
if (isLoginValid(login)) { if (isLoginValid(login)) {
entrys.map((item) => { if (entrys.length === 0) {
if (login === item.login) { toast.error('Аккаунт не найден');
if (password === item.password) { } else {
toast.success('Вы вошли в аккаунт'); setValidated(true);
return []; toast.success('Был произведён вход');
} }
}
toast.error('Данный пользователь не найден, либо был введён неверный пароль');
return [];
});
} else { } else {
toast.error('Логин должен быть введён латинскими символами'); toast.error('Логин должен быть введён латинскими символами');
return;
} }
} }
setValidated(true); setValidated(true);

View File

@ -4,13 +4,16 @@ import toast from 'react-hot-toast';
import { Button, Form } from 'react-bootstrap'; import { Button, Form } from 'react-bootstrap';
import Input from '../../input/Input.jsx'; import Input from '../../input/Input.jsx';
import useEntrysData from '../hooks/EntrysDataHook'; import useEntrysData from '../hooks/EntrysDataHook';
import useEntrysItemForm from '../hooks/EntrysDataItemHook';
const Entry = () => { const Entry = () => {
const { entrys } = useEntrysData();
const [validated, setValidated] = useState(false); const [validated, setValidated] = useState(false);
const [login, setLogin] = useState(''); const [login, setLogin] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const isLoginValid = (value) => /^[a-zA-Z]+$/.test(value); const isLoginValid = (value) => /^[a-zA-Z]+$/.test(value);
const { entrys } = useEntrysData(login, '');
const { UseHandleSubmit, UseHandleChange } = useEntrysItemForm();
const handleSubmit = (event) => { const handleSubmit = (event) => {
const form = event.currentTarget; const form = event.currentTarget;
@ -22,6 +25,7 @@ const Entry = () => {
if (isLoginExists) { if (isLoginExists) {
toast.error('Такой аккаунт уже создан'); toast.error('Такой аккаунт уже создан');
} else { } else {
UseHandleSubmit(event);
toast.success('Ваш аккаунт успешно создан'); toast.success('Ваш аккаунт успешно создан');
} }
} else { } else {
@ -32,6 +36,16 @@ const Entry = () => {
setValidated(true); setValidated(true);
}; };
const handleChangeLogin = (event) => {
setLogin(event.target.value);
UseHandleChange(event);
};
const handleChangePassword = (event) => {
setPassword(event.target.value);
UseHandleChange(event);
};
return ( return (
<main className="container-fluid text-center"> <main className="container-fluid text-center">
<span className="mainSt"> <span className="mainSt">
@ -43,10 +57,10 @@ const Entry = () => {
</span> </span>
<Form className="col-md-4 m-0 w-auto" onSubmit={ handleSubmit } noValidate validated={validated}> <Form className="col-md-4 m-0 w-auto" onSubmit={ handleSubmit } noValidate validated={validated}>
<label className="form-label"><b>Логин</b></label> <label className="form-label"><b>Логин</b></label>
<Input name="login" value = { login } onChange={(e) => setLogin(e.target.value)} <Input name="login" value = { login } onChange={ handleChangeLogin }
placeholder="dyctator" type="text" required /> placeholder="dyctator" type="text" required />
<label className="form-label"><b>Пароль</b></label> <label className="form-label"><b>Пароль</b></label>
<Input name="password" value = { password } onChange={(e) => setPassword(e.target.value)} <Input name="password" value = { password } onChange={ handleChangePassword }
type="password" required /> type="password" required />
<Button as={Link} to='/page4' className = "btn btn-info">Назад</Button> <Button as={Link} to='/page4' className = "btn btn-info">Назад</Button>
<Button className="btn btn-primary w-auto" type="submit" >Создать</Button> <Button className="btn btn-primary w-auto" type="submit" >Создать</Button>