ALL READY

This commit is contained in:
gg12 darfren 2023-12-19 13:46:39 +04:00
parent a0b391de56
commit 6a8989d679
9 changed files with 171 additions and 84 deletions

View File

@ -11,6 +11,24 @@
"email": "vk1d@mail.ru", "email": "vk1d@mail.ru",
"password": "1234", "password": "1234",
"id": 2 "id": 2
},
{
"email": "test@mail.ru",
"handle": "Test123",
"password": "sadsadsa",
"id": 3
},
{
"email": "tester@mail.ru",
"handle": "Tester",
"password": "etster",
"id": 4
},
{
"email": "tester2@mail.ru",
"handle": "tester2",
"password": "tester2",
"id": 5
} }
], ],
"groups": [ "groups": [

View File

@ -1,42 +1,18 @@
import { useContext } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import AddGroupApiService from './service/AddGroupApiService/AddGroupApiService.jsx'; import useSubmit from './hooks/AddGroupHook.jsx';
import { UserContext } from '../../providers/UserProvider.jsx';
import useGroup from '../../providers/hooks/CurrentGroupHook.jsx';
import useSteps from '../../providers/hooks/AddGroupHook.jsx';
const AddGroupPage = () => { const AddGroupPage = () => {
const { stepsChange } = useSteps();
const { user } = useContext(UserContext);
console.log(user);
const { groupChange } = useGroup();
const { register, handleSubmit, formState: { errors } } = useForm(); const { register, handleSubmit, formState: { errors } } = useForm();
console.log(errors); console.log(errors);
const navigate = useNavigate(); const navigate = useNavigate();
const isNumeric = (n) => (!!Number(n)); const { makeSubmit } = useSubmit();
const getNewGroup = (formData) => { const madeSubmit = async (data) => {
const namet = formData.name; const res = await makeSubmit(data);
const memberCountt = formData.memberCount; console.log(res);
return { if (res === true) navigate('/addmember');
name: namet, else alert('кол-во участников должно быть числом');
memberCount: memberCountt,
userId: user.id,
};
};
const makeSubmit = async (data) => {
if (isNumeric(data.memberCount) !== true) {
alert('кол-во участников должно быть числом');
return;
}
// eslint-disable-next-line radix
const val = Number.parseInt(data.memberCount);
console.log(val);
stepsChange(val);
const curGroup = await AddGroupApiService.create(getNewGroup(data));
groupChange(curGroup);
navigate('/addmember');
}; };
return ( return (
<main> <main>
@ -45,7 +21,7 @@ const AddGroupPage = () => {
<div className="add-group"> <div className="add-group">
<div className="form-group col-7"> <div className="form-group col-7">
<form className = "form-horizontal" onSubmit = {handleSubmit(async (data) => { <form className = "form-horizontal" onSubmit = {handleSubmit(async (data) => {
await makeSubmit(data); await madeSubmit(data);
})}> })}>
<label>название</label> <label>название</label>
<input {...register('name', { required: 'Это поле обязательно' })} className="form-control " type="handle" placeholder="название"></input> <input {...register('name', { required: 'Это поле обязательно' })} className="form-control " type="handle" placeholder="название"></input>

View File

@ -0,0 +1,39 @@
import { useContext } from 'react';
import { UserContext } from '../../../providers/UserProvider.jsx';
import useSteps from '../../../providers/hooks/AddGroupHook.jsx';
import useGroup from '../../../providers/hooks/CurrentGroupHook.jsx';
import AddGroupApiService from '../service/AddGroupApiService/AddGroupApiService.jsx';
const useSubmit = () => {
const { stepsChange } = useSteps();
const { groupChange } = useGroup();
const { user } = useContext(UserContext);
const isNumeric = (n) => (!!Number(n));
const getNewGroup = (formData) => {
const namet = formData.name;
const memberCountt = formData.memberCount;
return {
name: namet,
memberCount: memberCountt,
userId: user.id,
};
};
const makeSubmit = async (data) => {
console.log(isNumeric(data.memberCount));
if (isNumeric(data.memberCount) !== true) {
return false;
}
// eslint-disable-next-line radix
const val = Number.parseInt(data.memberCount);
console.log(val);
stepsChange(val);
const curGroup = await AddGroupApiService.create(getNewGroup(data));
groupChange(curGroup);
return true;
};
return { makeSubmit };
};
export default useSubmit;

View File

@ -3,40 +3,22 @@ import { useNavigate } from 'react-router-dom';
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { AddGroupContext } from '../../providers/AddGroupProvider.jsx'; import { AddGroupContext } from '../../providers/AddGroupProvider.jsx';
import AddMemberApiService from './service/AddMemberApiService/AddMemberApiService.jsx'; import useSubmit from './hooks/AddMemberHook.jsx';
import { CurrentGroupContext } from '../../providers/CurrentGroupProvider.jsx';
import useSteps from '../../providers/hooks/AddGroupHook.jsx';
const AddMember = () => { const AddMember = () => {
const { steps } = useContext(AddGroupContext); const { steps } = useContext(AddGroupContext);
const { stepsChange } = useSteps(); const { onSubmit } = useSubmit();
const { group } = useContext(CurrentGroupContext);
const { const {
register, reset, handleSubmit, formState: { errors }, register, reset, handleSubmit, formState: { errors },
} = useForm(); } = useForm();
console.log(errors); console.log(errors);
const navigate = useNavigate(); const navigate = useNavigate();
console.log(steps); console.log(steps);
const getNewMember = (formData) => { const makeSubmit = async (data) => {
const namet = formData.name; const res = await onSubmit(data);
const handlet = formData.handle; if (res === true) {
console.log(group);
console.log(steps);
return {
name: namet,
handle: handlet,
groupId: group.id,
};
};
const onSubmit = async (data) => {
await AddMemberApiService.create(getNewMember(data));
const cursteps = steps - 1;
stepsChange(cursteps);
if (cursteps !== 0) {
console.log(steps);
navigate('/addmember'); navigate('/addmember');
} else { } else {
console.log(steps);
navigate('/'); navigate('/');
} }
reset(); reset();
@ -48,7 +30,7 @@ const AddMember = () => {
<div className="add-group"> <div className="add-group">
<div className="form-group col-7"> <div className="form-group col-7">
<form className = 'form-horizontal' onSubmit = {handleSubmit(async (data) => { <form className = 'form-horizontal' onSubmit = {handleSubmit(async (data) => {
await onSubmit(data); await makeSubmit(data);
})}> })}>
<label>имя участника </label> <label>имя участника </label>
<input {...register('name', { required: 'Это поле обязательно' })} className="form-control " type="handle" placeholder="имя участника"></input> <input {...register('name', { required: 'Это поле обязательно' })} className="form-control " type="handle" placeholder="имя участника"></input>

View File

@ -0,0 +1,36 @@
import { useContext } from 'react';
import useSteps from '../../../providers/hooks/AddGroupHook.jsx';
import { CurrentGroupContext } from '../../../providers/CurrentGroupProvider.jsx';
import AddMemberApiService from '../service/AddMemberApiService/AddMemberApiService.jsx';
import { AddGroupContext } from '../../../providers/AddGroupProvider.jsx';
const useSubmit = () => {
const { stepsChange } = useSteps();
const { group } = useContext(CurrentGroupContext);
const { steps } = useContext(AddGroupContext);
const getNewMember = (formData) => {
const namet = formData.name;
const handlet = formData.handle;
console.log(group);
console.log(steps);
return {
name: namet,
handle: handlet,
groupId: group.id,
};
};
const onSubmit = async (data) => {
await AddMemberApiService.create(getNewMember(data));
const cursteps = steps - 1;
stepsChange(cursteps);
if (cursteps !== 0) {
return true;
}
return false;
};
return { onSubmit };
};
export default useSubmit;

View File

@ -0,0 +1,20 @@
import LoginApiService from '../service/loginapiservice';
import useUser from '../../../../providers/hooks/UserHook.jsx';
const useSubmit = () => {
const { userLogin } = useUser();
const onSubmit = async (data) => {
const res1 = await LoginApiService.getByHandle(data.handle);
if (res1.length === 0) {
return 1;
}
if (res1[0].password !== data.password) {
return 2;
}
userLogin(res1[0]);
return 3;
};
return { onSubmit };
};
export default useSubmit;

View File

@ -2,25 +2,23 @@ import './login.css';
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import LoginApiService from '../service/loginapiservice'; import useSubmit from '../hooks/LoginHook.jsx';
import useUser from '../../../../providers/hooks/UserHook.jsx';
const Login = () => { const Login = () => {
const { userLogin } = useUser();
const { register, handleSubmit, formState: { errors } } = useForm(); const { register, handleSubmit, formState: { errors } } = useForm();
const { onSubmit } = useSubmit();
console.log(errors); console.log(errors);
const navigate = useNavigate(); const navigate = useNavigate();
const makeSubmit = async (data) => { const makeSubmit = async (data) => {
const res1 = await LoginApiService.getByHandle(data.handle); const res1 = await onSubmit(data);
if (res1.length === 0) { if (res1 === 1) {
alert('Пользователся с таким хэндлом не существует'); alert('Пользователся с таким хэндлом не существует');
return; return;
} }
if (res1[0].password !== data.password) { if (res1 === 2) {
alert('Введен неверный пароль'); alert('Введен неверный пароль');
return; return;
} }
userLogin(res1[0]);
navigate('/'); navigate('/');
}; };
return ( return (

View File

@ -0,0 +1,34 @@
import useUser from '../../../../providers/hooks/UserHook.jsx';
import RegApiService from '../service/regapiservice';
const useSubmit = () => {
const { userLogin } = useUser();
const getNewUser = (formData) => {
const emailt = formData.email;
const handlet = formData.handle;
const passwordt = formData.password;
return {
email: emailt,
handle: handlet,
password: passwordt,
};
};
const onSubmit = async (data) => {
console.log(data);
const res1 = await RegApiService.getByHandle(data.handle);
if (res1.length !== 0) {
return 1;
}
const res2 = await RegApiService.getByEmail(data.email);
if (res2.length !== 0) {
return 2;
}
const newUser = getNewUser(data);
const curUser = await RegApiService.create(newUser);
userLogin(curUser);
return 3;
};
return { onSubmit };
};
export default useSubmit;

View File

@ -2,39 +2,23 @@ import './reg.css';
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import RegApiService from '../service/regapiservice'; import useSubmit from '../hooks/RegHook.jsx';
import useUser from '../../../../providers/hooks/UserHook.jsx';
const Reg = () => { const Reg = () => {
const { register, handleSubmit, formState: { errors } } = useForm(); const { register, handleSubmit, formState: { errors } } = useForm();
console.log(errors); console.log(errors);
const navigate = useNavigate(); const navigate = useNavigate();
const { userLogin } = useUser(); const { onSubmit } = useSubmit();
const getNewUser = (formData) => {
const emailt = formData.email;
const handlet = formData.handle;
const passwordt = formData.password;
return {
email: emailt,
handle: handlet,
password: passwordt,
};
};
const makeSubmit = async (data) => { const makeSubmit = async (data) => {
console.log(data); const res = await onSubmit(data);
const res1 = await RegApiService.getByHandle(data.handle); if (res === 1) {
if (res1.length !== 0) {
alert('Пользователь с таким хэндлом уже существует'); alert('Пользователь с таким хэндлом уже существует');
return; return;
} }
const res2 = await RegApiService.getByEmail(data.email); if (res === 2) {
if (res2.length !== 0) {
alert('Пользователь с таким email уже существует'); alert('Пользователь с таким email уже существует');
return; return;
} }
const newUser = getNewUser(data);
const curUser = await RegApiService.create(newUser);
userLogin(curUser);
navigate('/registersuccess'); navigate('/registersuccess');
}; };
return ( return (