diff --git a/backend/app/coordinator/routes/groups.py b/backend/app/coordinator/routes/groups.py index 4bb42e1..72b4035 100644 --- a/backend/app/coordinator/routes/groups.py +++ b/backend/app/coordinator/routes/groups.py @@ -65,9 +65,9 @@ def create_group(data: dict) -> dict: db.session.commit() students = db.session.query(Student).filter(Student.index.in_(students_indexes)).all() + project_supervisor.count_groups += 1 for student in students: student.group_id = group.id - project_supervisor.count_groups += 1 db.session.commit() @@ -89,6 +89,14 @@ def delete_group(id: int) -> dict: group = Group.query.filter_by(id=id).first() if group is None: abort(400, f"Group with id {id} doesn't exist!") + + project_supervisor = ProjectSupervisor.query.filter_by(id=group.project_supervisor_id).first() + project_supervisor.count_groups -= 1 + + students = db.session.query(Student).filter_by(group_id=id).all() + for student in students: + student.group_id = None + db.session.delete(group) db.session.commit() return {"message": "Group was deleted!"} diff --git a/backend/app/coordinator/routes/project_supervisor.py b/backend/app/coordinator/routes/project_supervisor.py index ddeeda7..4605d0c 100644 --- a/backend/app/coordinator/routes/project_supervisor.py +++ b/backend/app/coordinator/routes/project_supervisor.py @@ -69,8 +69,9 @@ def delete_project_supervisor(id: int) -> dict: abort(400, f"Project Supervisor with id {id} doesn't exist!") count_groups = db.session.query(db.func.count(ProjectSupervisor.id)).join(Group).\ - filter(ProjectSupervisor.id == id).group_by(ProjectSupervisor.id).scalar() - if count_groups > 0: + filter(ProjectSupervisor.id == id).group_by(ProjectSupervisor.id) + + if count_groups is not None: abort(400, f"Project Supervisor with id {id} has gropus!") db.session.delete(project_supervisor) diff --git a/frontend/src/api/groups.ts b/frontend/src/api/groups.ts index faa6b0b..8119472 100644 --- a/frontend/src/api/groups.ts +++ b/frontend/src/api/groups.ts @@ -34,3 +34,8 @@ export const getGroups = ( export const createGroup = (payload: CreateGroup) => axiosInstance.post('http://127.0.0.1:5000/api/coordinator/groups/', payload) + + export const deleteGroup = (id: number) => + axiosInstance.delete( + `http://127.0.0.1:5000/api/coordinator/groups/${id}/`, + ) \ No newline at end of file diff --git a/frontend/src/views/coordinator/AddGroup.tsx b/frontend/src/views/coordinator/AddGroup.tsx index a92d154..2f70125 100644 --- a/frontend/src/views/coordinator/AddGroup.tsx +++ b/frontend/src/views/coordinator/AddGroup.tsx @@ -29,7 +29,8 @@ const AddGroup = () => { { onSuccess: (data) => { setStudentOptions( - data?.data.students.map(({ first_name, last_name, index }) => { + data?.data.students.filter(st => st.group === null) + .map(({ first_name, last_name, index }) => { return { value: index, label: `${first_name} ${last_name} (${index})`, @@ -45,7 +46,7 @@ const AddGroup = () => { { onSuccess: (data) => { setSupervisorOptions( - data?.data.project_supervisors.map( + data?.data.project_supervisors.filter(ld => ld.count_groups < ld.limit_group).map( ({ id, first_name, last_name }) => ({ value: id, label: `${first_name} ${last_name}`, diff --git a/frontend/src/views/coordinator/Groups.tsx b/frontend/src/views/coordinator/Groups.tsx index e1579f3..8894d15 100644 --- a/frontend/src/views/coordinator/Groups.tsx +++ b/frontend/src/views/coordinator/Groups.tsx @@ -1,26 +1,75 @@ -import { useQuery } from 'react-query' +import classNames from 'classnames' +import { useEffect, useState } from 'react' +import { useMutation, useQuery } from 'react-query' import { useNavigate } from 'react-router-dom' -import { getGroups } from '../../api/groups' +import { deleteGroup, getGroups } from '../../api/groups' const Groups = () => { let navigate = useNavigate() + const [page, setPage] = useState(1) + const [perPage, setPerPage] = useState(10) - const { isLoading: areGroupsLoading, data: groups } = useQuery( - ['groups'], - () => getGroups({ per_page: 1000 }), + const perPageOptions = [ + { + value: 10, + label: '10 rekordów', + }, + { + value: 20, + label: '20 rekordów', + }, + { + value: 50, + label: '50 rekordów', + }, + { + value: 1000, + label: 'Pokaż wszystkie', + }, + ] + + const { + isLoading: areGroupsLoading, + data: groups, + refetch: refetchGroups, + } = useQuery(['groups', page, perPage], () => + getGroups({ page, per_page: perPage }), ) + const { mutate: mutateDelete } = useMutation( + 'deleteGroup', + (index: number) => deleteGroup(index), + { + onSuccess: () => refetchGroups(), + }, + ) + + useEffect(() => { + setPage(1) + }, [perPage]) + if (areGroupsLoading) { return
Ładowanie
} return (
- +
+ +
+ +
+
@@ -29,6 +78,7 @@ const Groups = () => { + @@ -47,12 +97,50 @@ const Groups = () => { + ), )}
Opiekun Semestr 1 Semestr 2
{points_for_first_term} {points_for_second_term} + +
+
+
+ + {[ + ...Array( + groups?.data?.max_pages && groups?.data?.max_pages + 1, + ).keys(), + ] + .slice(1) + .map((p) => ( + + ))} + +
+
) }