Skip to content

Commit 0c9e148

Browse files
committed
fix: groups update after creation
1 parent 6faa9ac commit 0c9e148

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

hwproj.front/src/components/Courses/Course.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ interface ICourseState {
4949
isFound: boolean;
5050
course: CourseViewModel;
5151
courseHomeworks: HomeworkViewModel[];
52-
groups: GroupViewModel[];
5352
mentors: AccountDataDto[];
5453
acceptedStudents: AccountDataDto[];
5554
newStudents: AccountDataDto[];
@@ -71,7 +70,6 @@ const Course: React.FC = () => {
7170
course: {},
7271
courseHomeworks: [],
7372
mentors: [],
74-
groups: [],
7573
acceptedStudents: [],
7674
newStudents: [],
7775
studentSolutions: [],
@@ -90,19 +88,8 @@ const Course: React.FC = () => {
9088
newStudents,
9189
acceptedStudents,
9290
courseHomeworks,
93-
groups
9491
} = courseState
9592

96-
const loadGroups = async (targetCourseId: number = course.id!) => {
97-
if (!targetCourseId) return;
98-
99-
const groups = await ApiSingleton.courseGroupsApi.courseGroupsGetAllCourseGroups(targetCourseId)
100-
setCourseState(prevState => ({
101-
...prevState,
102-
groups: groups
103-
}))
104-
};
105-
10693
const userId = ApiSingleton.authService.getUserId()
10794

10895
const isLecturer = ApiSingleton.authService.isLecturer()
@@ -160,9 +147,17 @@ const Course: React.FC = () => {
160147
}))
161148
}
162149

150+
const updateCourseGroups = async () => {
151+
const course = await ApiSingleton.coursesApi.coursesGetCourseData(+courseId!)
152+
153+
setCourseState(prevState => ({
154+
...prevState,
155+
course: course,
156+
}))
157+
}
158+
163159
useEffect(() => {
164160
setCurrentState()
165-
loadGroups(+courseId!)
166161
}, [courseId])
167162

168163
useEffect(() => {
@@ -189,9 +184,9 @@ const Course: React.FC = () => {
189184
const [lecturerStatsState, setLecturerStatsState] = useState(false);
190185

191186
const studentsWithoutGroup = useMemo(() => {
192-
const inGroupIds = new Set(groups.flatMap(g => g.studentsIds));
187+
const inGroupIds = new Set(course.groups?.flatMap(g => g.studentsIds) || []);
193188
return acceptedStudents.filter(s => !inGroupIds.has(s.userId!));
194-
}, [groups, acceptedStudents]);
189+
}, [course.groups, acceptedStudents]);
195190

196191
const CourseMenu: FC = () => {
197192
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
@@ -321,7 +316,7 @@ const Course: React.FC = () => {
321316
}
322317
</Grid>
323318
</Grid>
324-
{isCourseMentor && groups.length > 0 && studentsWithoutGroup.length > 0 &&
319+
{isCourseMentor && course.groups?.length !== 0 && studentsWithoutGroup.length > 0 &&
325320
<Grid item>
326321
<Tooltip
327322
title={studentsWithoutGroup.length + " " + Utils.pluralizeHelper(["студент", "студента", "студентов"], studentsWithoutGroup.length) + " без группы"}
@@ -405,8 +400,8 @@ const Course: React.FC = () => {
405400
courseHomeworks: homeworks
406401
}))
407402
}}
408-
onGroupsUpdate={loadGroups}
409-
groups={groups}
403+
onGroupsUpdate={updateCourseGroups}
404+
groups={course.groups ?? []}
410405
/>
411406
}
412407
{tabValue === "stats" &&
@@ -418,7 +413,7 @@ const Course: React.FC = () => {
418413
isMentor={isCourseMentor}
419414
course={courseState.course}
420415
solutions={studentSolutions}
421-
groups={groups}
416+
groups={course.groups ?? []}
422417
/>
423418
</Grid>
424419
</Grid>}

hwproj.front/src/components/Courses/CourseFilter.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,16 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
278278
}
279279
}
280280

281+
const selectedGroups = [...newGroups];
281282
setState((prev) => ({
282283
...prev,
283284
selectedStudents: newStudents,
284-
selectedGroups: [...newGroups],
285+
selectedGroups,
286+
selectedHomeworks: prev.selectedHomeworks
287+
.filter(h =>
288+
!h.groupId
289+
|| selectedGroups.length === 0
290+
|| selectedGroups.some(g => g.id === h.groupId)),
285291
}))
286292
}}
287293
/>

0 commit comments

Comments
 (0)