Работает регистрация

This commit is contained in:
Никита Потапов 2024-01-11 15:18:43 +04:00
parent 8639004014
commit 7a7ee8aab4
4 changed files with 207 additions and 158 deletions

22
src/hooks/GroupHook.js Normal file
View File

@ -0,0 +1,22 @@
import { useEffect, useState } from "react";
import GroupsApiService from "../services/GroupsApiService";
export const useGroups = () => {
const [groupsRefresh, setGroupsRefresh] = useState(false);
const [groups, setGroups] = useState([]);
const handleGroupsRefresh = () => setGroupsRefresh(!groupsRefresh);
const getGroups = async () => {
const data = await GroupsApiService.getAll();
setGroups(data ?? []);
};
useEffect(() => {
getGroups();
}, [groupsRefresh]);
return {
groups,
handleGroupsRefresh,
};
};

View File

@ -5,25 +5,29 @@ import { CurrentUserContext } from "../../contexts/CurrentUserContext";
import { getUserByUsername } from "../../utils/UserUtils";
import UsersApiService from "../../services/UsersApiService";
import toast from "react-hot-toast";
import { useGroups } from "../../hooks/GroupHook";
const RegisterPage = () => {
const { currentUser, setCurrentUser } = useContext(
const { currentUser } = useContext(
CurrentUserContext
);
const navigate = useNavigate();
const { groups } = useGroups();
if (currentUser) {
navigate('/');
}
const [inputFields, setInputFields] = useState({
let [inputFields, setInputFields] = useState({
username: "",
password: "",
name: "",
surname: "",
confirmPassword: "",
isTeacher: false
isTeacher: false,
groupId: null
});
const [errors, setErrors] = useState({});
@ -46,11 +50,18 @@ const RegisterPage = () => {
if (inputValues.password != inputValues.confirmPassword) {
errors.confirmPassword = "Пароли не совпадают";
}
if (!inputValues.isTeacher && inputValues.groupId == null) {
errors.groupId = "Выберите группу";
}
return errors;
};
const handleChange = (e) => {
setInputFields({ ...inputFields, [e.target.name]: e.target.value });
if (e.target.type == 'checkbox') {
setInputFields({ ...inputFields, [e.target.name]: e.target.checked });
}
};
const handleSubmit = async (event) => {
@ -71,7 +82,7 @@ const RegisterPage = () => {
username: inputFields.username,
password: inputFields.password,
isTeacher: inputFields.isTeacher,
groupId: 1
groupId: parseInt(inputFields.groupId)
});
toast.success('Пользователь зарегистрирован!');
}
@ -157,6 +168,17 @@ const RegisterPage = () => {
</input>
<label htmlFor="isTeacher">Я преподаватель</label>
</div>
{
inputFields.isTeacher != true ? <div className="mb-3 d-flex">
<select name="groupId" id="groupId" className="me-3" onChange={handleChange}>
<option value={null}>Выберите группу</option>
{
groups.map((group, index) => <option value={group.id} key={index}>{group.name}</option>)
}
</select>
{errors.groupId}
</div> : ''
}
<button id="btn-register-submit" type="submit" className="btn btn-primary" >Зарегистрироваться</button>
</ form>
</div>

View File

@ -0,0 +1,5 @@
import ApiService from "../api/ApiService.js";
const GroupsApiService = new ApiService("groups");
export default GroupsApiService;