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",
"password": "1234",
"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": [

View File

@ -1,42 +1,18 @@
import { useContext } from 'react';
import { useNavigate } from 'react-router-dom';
// eslint-disable-next-line import/no-extraneous-dependencies
import { useForm } from 'react-hook-form';
import AddGroupApiService from './service/AddGroupApiService/AddGroupApiService.jsx';
import { UserContext } from '../../providers/UserProvider.jsx';
import useGroup from '../../providers/hooks/CurrentGroupHook.jsx';
import useSteps from '../../providers/hooks/AddGroupHook.jsx';
import useSubmit from './hooks/AddGroupHook.jsx';
const AddGroupPage = () => {
const { stepsChange } = useSteps();
const { user } = useContext(UserContext);
console.log(user);
const { groupChange } = useGroup();
const { register, handleSubmit, formState: { errors } } = useForm();
console.log(errors);
const navigate = useNavigate();
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) => {
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');
const { makeSubmit } = useSubmit();
const madeSubmit = async (data) => {
const res = await makeSubmit(data);
console.log(res);
if (res === true) navigate('/addmember');
else alert('кол-во участников должно быть числом');
};
return (
<main>
@ -45,7 +21,7 @@ const AddGroupPage = () => {
<div className="add-group">
<div className="form-group col-7">
<form className = "form-horizontal" onSubmit = {handleSubmit(async (data) => {
await makeSubmit(data);
await madeSubmit(data);
})}>
<label>название</label>
<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
import { useForm } from 'react-hook-form';
import { AddGroupContext } from '../../providers/AddGroupProvider.jsx';
import AddMemberApiService from './service/AddMemberApiService/AddMemberApiService.jsx';
import { CurrentGroupContext } from '../../providers/CurrentGroupProvider.jsx';
import useSteps from '../../providers/hooks/AddGroupHook.jsx';
import useSubmit from './hooks/AddMemberHook.jsx';
const AddMember = () => {
const { steps } = useContext(AddGroupContext);
const { stepsChange } = useSteps();
const { group } = useContext(CurrentGroupContext);
const { onSubmit } = useSubmit();
const {
register, reset, handleSubmit, formState: { errors },
} = useForm();
console.log(errors);
const navigate = useNavigate();
console.log(steps);
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) {
console.log(steps);
const makeSubmit = async (data) => {
const res = await onSubmit(data);
if (res === true) {
navigate('/addmember');
} else {
console.log(steps);
navigate('/');
}
reset();
@ -48,7 +30,7 @@ const AddMember = () => {
<div className="add-group">
<div className="form-group col-7">
<form className = 'form-horizontal' onSubmit = {handleSubmit(async (data) => {
await onSubmit(data);
await makeSubmit(data);
})}>
<label>имя участника </label>
<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
import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import LoginApiService from '../service/loginapiservice';
import useUser from '../../../../providers/hooks/UserHook.jsx';
import useSubmit from '../hooks/LoginHook.jsx';
const Login = () => {
const { userLogin } = useUser();
const { register, handleSubmit, formState: { errors } } = useForm();
const { onSubmit } = useSubmit();
console.log(errors);
const navigate = useNavigate();
const makeSubmit = async (data) => {
const res1 = await LoginApiService.getByHandle(data.handle);
if (res1.length === 0) {
const res1 = await onSubmit(data);
if (res1 === 1) {
alert('Пользователся с таким хэндлом не существует');
return;
}
if (res1[0].password !== data.password) {
if (res1 === 2) {
alert('Введен неверный пароль');
return;
}
userLogin(res1[0]);
navigate('/');
};
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
import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import RegApiService from '../service/regapiservice';
import useUser from '../../../../providers/hooks/UserHook.jsx';
import useSubmit from '../hooks/RegHook.jsx';
const Reg = () => {
const { register, handleSubmit, formState: { errors } } = useForm();
console.log(errors);
const navigate = useNavigate();
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 } = useSubmit();
const makeSubmit = async (data) => {
console.log(data);
const res1 = await RegApiService.getByHandle(data.handle);
if (res1.length !== 0) {
const res = await onSubmit(data);
if (res === 1) {
alert('Пользователь с таким хэндлом уже существует');
return;
}
const res2 = await RegApiService.getByEmail(data.email);
if (res2.length !== 0) {
if (res === 2) {
alert('Пользователь с таким email уже существует');
return;
}
const newUser = getNewUser(data);
const curUser = await RegApiService.create(newUser);
userLogin(curUser);
navigate('/registersuccess');
};
return (