diff --git a/data.json b/data.json index 547a891..8a86b0a 100644 --- a/data.json +++ b/data.json @@ -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 - } - ] -} \ No newline at end of file + "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 + } + ] +} diff --git a/src/hooks/GroupHook.js b/src/hooks/GroupHook.js new file mode 100644 index 0000000..5aa35a2 --- /dev/null +++ b/src/hooks/GroupHook.js @@ -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, + }; +}; diff --git a/src/pages/RegisterPage/RegisterPage.jsx b/src/pages/RegisterPage/RegisterPage.jsx index b1a0cea..ad77d03 100644 --- a/src/pages/RegisterPage/RegisterPage.jsx +++ b/src/pages/RegisterPage/RegisterPage.jsx @@ -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 = () => { + { + inputFields.isTeacher != true ?