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

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": [ "users": [
{ {
"id": 1, "id": 1,
"name": "Никита", "name": "Никита",
"surname": "Потапов", "surname": "Потапов",
"username": "nspotapov", "username": "nspotapov",
"password": "123456", "password": "123456",
"isTeacher": false, "isTeacher": false,
"groupId": 1 "groupId": 1
}, },
{ {
"id": 2, "id": 2,
"name": "Алексей", "name": "Алексей",
"surname": "Филиппов", "surname": "Филиппов",
"username": "afilippov", "username": "afilippov",
"password": "123456", "password": "123456",
"isTeacher": true, "isTeacher": true,
"groupId": null "groupId": null
}, },
{ {
"id": 3, "id": 3,
"name": "Елена", "name": "Елена",
"surname": "Бакальская", "surname": "Бакальская",
"username": "ekallin", "username": "ekallin",
"password": "123456", "password": "123456",
"isTeacher": false, "isTeacher": false,
"groupId": 1 "groupId": 1
}, },
{ {
"name": "Алексей", "name": "Алексей",
"surname": "Крюков", "surname": "Крюков",
"username": "akrukov", "username": "akrukov",
"password": "123", "password": "123",
"isTeacher": false, "isTeacher": false,
"groupId": 1, "groupId": 1,
"id": 4 "id": 4
} }
], ],
"groups": [ "groups": [
{ {
"id": 1, "id": 1,
"name": "пибд-21" "name": "пибд-21"
} }
], ],
"subjects": [ "subjects": [
{ {
"id": 1, "id": 1,
"name": "Интернет программирование" "name": "Интернет программирование"
}, },
{ {
"id": 2, "id": 2,
"name": "РПП" "name": "РПП"
}, },
{ {
"id": 3, "id": 3,
"name": "Физическая культура" "name": "Физическая культура"
}, },
{ {
"id": 4, "id": 4,
"name": "Системный анализ" "name": "Системный анализ"
}, },
{ {
"id": 5, "id": 5,
"name": "Системное администрирование" "name": "Системное администрирование"
}, },
{ {
"id": 6, "id": 6,
"name": "Основы теории систем" "name": "Основы теории систем"
}, },
{ {
"id": 7, "id": 7,
"name": "Иностранный язык" "name": "Иностранный язык"
}, },
{ {
"id": 8, "id": 8,
"name": "Философия" "name": "Философия"
}, },
{ {
"id": 9, "id": 9,
"name": "Теория вероятностей" "name": "Теория вероятностей"
}, },
{ {
"id": 10, "id": 10,
"name": "Операционные системы" "name": "Операционные системы"
}, },
{ {
"id": 11, "id": 11,
"name": "Основы ЭВМ и систем" "name": "Основы ЭВМ и систем"
}, },
{ {
"id": 12, "id": 12,
"name": "Базы данных" "name": "Базы данных"
} }
], ],
"marks": [ "marks": [
{ {
"id": 2, "id": 2,
"name": "неуд", "name": "неуд",
"controltypeId": 1 "controltypeId": 1
}, },
{ {
"id": 3, "id": 3,
"name": "удов", "name": "удов",
"controltypeId": 1 "controltypeId": 1
}, },
{ {
"id": 4, "id": 4,
"name": "хорошо", "name": "хорошо",
"controltypeId": 1 "controltypeId": 1
}, },
{ {
"id": 5, "id": 5,
"name": "отлично", "name": "отлично",
"controltypeId": 1 "controltypeId": 1
}, },
{ {
"id": 6, "id": 6,
"name": "зачтено", "name": "зачтено",
"controltypeId": 2 "controltypeId": 2
}, },
{ {
"id": 7, "id": 7,
"name": "незачет", "name": "незачет",
"controltypeId": 2 "controltypeId": 2
} }
], ],
"statements": [ "statements": [
{ {
"id": 1, "id": 1,
"subjectId": 1, "subjectId": 1,
"groupId": 1, "groupId": 1,
"controlTypeId": 2, "controlTypeId": 2,
"date": "Dec 23 2023" "date": "Dec 23 2023"
} }
], ],
"controltypes": [ "controltypes": [
{ {
"id": 1, "id": 1,
"name": "Экзамен" "name": "Экзамен"
}, },
{ {
"id": 2, "id": 2,
"name": "Зачет" "name": "Зачет"
} }
], ],
"usermarks": [ "usermarks": [
{ {
"id": 1, "id": 1,
"userId": 1, "userId": 1,
"markId": 1, "markId": 1,
"statementId": 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 { getUserByUsername } from "../../utils/UserUtils";
import UsersApiService from "../../services/UsersApiService"; import UsersApiService from "../../services/UsersApiService";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { useGroups } from "../../hooks/GroupHook";
const RegisterPage = () => { const RegisterPage = () => {
const { currentUser, setCurrentUser } = useContext( const { currentUser } = useContext(
CurrentUserContext CurrentUserContext
); );
const navigate = useNavigate(); const navigate = useNavigate();
const { groups } = useGroups();
if (currentUser) { if (currentUser) {
navigate('/'); navigate('/');
} }
const [inputFields, setInputFields] = useState({ let [inputFields, setInputFields] = useState({
username: "", username: "",
password: "", password: "",
name: "", name: "",
surname: "", surname: "",
confirmPassword: "", confirmPassword: "",
isTeacher: false isTeacher: false,
groupId: null
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
@ -46,11 +50,18 @@ const RegisterPage = () => {
if (inputValues.password != inputValues.confirmPassword) { if (inputValues.password != inputValues.confirmPassword) {
errors.confirmPassword = "Пароли не совпадают"; errors.confirmPassword = "Пароли не совпадают";
} }
if (!inputValues.isTeacher && inputValues.groupId == null) {
errors.groupId = "Выберите группу";
}
return errors; return errors;
}; };
const handleChange = (e) => { const handleChange = (e) => {
setInputFields({ ...inputFields, [e.target.name]: e.target.value }); 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) => { const handleSubmit = async (event) => {
@ -71,7 +82,7 @@ const RegisterPage = () => {
username: inputFields.username, username: inputFields.username,
password: inputFields.password, password: inputFields.password,
isTeacher: inputFields.isTeacher, isTeacher: inputFields.isTeacher,
groupId: 1 groupId: parseInt(inputFields.groupId)
}); });
toast.success('Пользователь зарегистрирован!'); toast.success('Пользователь зарегистрирован!');
} }
@ -157,6 +168,17 @@ const RegisterPage = () => {
</input> </input>
<label htmlFor="isTeacher">Я преподаватель</label> <label htmlFor="isTeacher">Я преподаватель</label>
</div> </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> <button id="btn-register-submit" type="submit" className="btn btn-primary" >Зарегистрироваться</button>
</ form> </ form>
</div> </div>

View File

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