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

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

308
data.json
View File

@ -1,155 +1,155 @@
{
"users": [
{
"id": 1,
"name": "Никита",
"surname": "Потапов",
"username": "nspotapov",
"password": "123456",
"isTeacher": false,
"groupId": 1
},
{
"id": 2,
"name": "Алексей",
"surname": "Филиппов",
"username": "afilippov",
"password": "123456",
"isTeacher": true,
"groupId": null
},
{
"id": 3,
"name": "Елена",
"surname": "Бакальская",
"username": "ekallin",
"password": "123456",
"isTeacher": false,
"groupId": 1
},
{
"name": "Алексей",
"surname": "Крюков",
"username": "akrukov",
"password": "123",
"isTeacher": false,
"groupId": 1,
"id": 4
}
],
"groups": [
{
"id": 1,
"name": "пибд-21"
}
],
"subjects": [
{
"id": 1,
"name": "Интернет программирование"
},
{
"id": 2,
"name": "РПП"
},
{
"id": 3,
"name": "Физическая культура"
},
{
"id": 4,
"name": "Системный анализ"
},
{
"id": 5,
"name": "Системное администрирование"
},
{
"id": 6,
"name": "Основы теории систем"
},
{
"id": 7,
"name": "Иностранный язык"
},
{
"id": 8,
"name": "Философия"
},
{
"id": 9,
"name": "Теория вероятностей"
},
{
"id": 10,
"name": "Операционные системы"
},
{
"id": 11,
"name": "Основы ЭВМ и систем"
},
{
"id": 12,
"name": "Базы данных"
}
],
"marks": [
{
"id": 2,
"name": "неуд",
"controltypeId": 1
},
{
"id": 3,
"name": "удов",
"controltypeId": 1
},
{
"id": 4,
"name": "хорошо",
"controltypeId": 1
},
{
"id": 5,
"name": "отлично",
"controltypeId": 1
},
{
"id": 6,
"name": "зачтено",
"controltypeId": 2
},
{
"id": 7,
"name": "незачет",
"controltypeId": 2
}
],
"statements": [
{
"id": 1,
"subjectId": 1,
"groupId": 1,
"controlTypeId": 2,
"date": "Dec 23 2023"
}
],
"controltypes": [
{
"id": 1,
"name": "Экзамен"
},
{
"id": 2,
"name": "Зачет"
}
],
"usermarks": [
{
"id": 1,
"userId": 1,
"markId": 1,
"statementId": 1
}
]
}
"users": [
{
"id": 1,
"name": "Никита",
"surname": "Потапов",
"username": "nspotapov",
"password": "123456",
"isTeacher": false,
"groupId": 1
},
{
"id": 2,
"name": "Алексей",
"surname": "Филиппов",
"username": "afilippov",
"password": "123456",
"isTeacher": true,
"groupId": null
},
{
"id": 3,
"name": "Елена",
"surname": "Бакальская",
"username": "ekallin",
"password": "123456",
"isTeacher": false,
"groupId": 1
},
{
"name": "Алексей",
"surname": "Крюков",
"username": "akrukov",
"password": "123",
"isTeacher": false,
"groupId": 1,
"id": 4
}
],
"groups": [
{
"id": 1,
"name": "пибд-21"
}
],
"subjects": [
{
"id": 1,
"name": "Интернет программирование"
},
{
"id": 2,
"name": "РПП"
},
{
"id": 3,
"name": "Физическая культура"
},
{
"id": 4,
"name": "Системный анализ"
},
{
"id": 5,
"name": "Системное администрирование"
},
{
"id": 6,
"name": "Основы теории систем"
},
{
"id": 7,
"name": "Иностранный язык"
},
{
"id": 8,
"name": "Философия"
},
{
"id": 9,
"name": "Теория вероятностей"
},
{
"id": 10,
"name": "Операционные системы"
},
{
"id": 11,
"name": "Основы ЭВМ и систем"
},
{
"id": 12,
"name": "Базы данных"
}
],
"marks": [
{
"id": 2,
"name": "неуд",
"controltypeId": 1
},
{
"id": 3,
"name": "удов",
"controltypeId": 1
},
{
"id": 4,
"name": "хорошо",
"controltypeId": 1
},
{
"id": 5,
"name": "отлично",
"controltypeId": 1
},
{
"id": 6,
"name": "зачтено",
"controltypeId": 2
},
{
"id": 7,
"name": "незачет",
"controltypeId": 2
}
],
"statements": [
{
"id": 1,
"subjectId": 1,
"groupId": 1,
"controlTypeId": 2,
"date": "Dec 23 2023"
}
],
"controltypes": [
{
"id": 1,
"name": "Экзамен"
},
{
"id": 2,
"name": "Зачет"
}
],
"usermarks": [
{
"id": 1,
"userId": 1,
"markId": 1,
"statementId": 1
}
]
}

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;