1
1
import React , { FC , useEffect , useState } from 'react' ;
2
- import { HomeworkViewModel , AccountDataDto } from '../../api' ;
2
+ import { HomeworkViewModel , AccountDataDto , MentorToAssignedStudentsDTO } from '../../api' ;
3
3
import Grid from "@material-ui/core/Grid" ;
4
- import { Autocomplete } from "@mui/material" ;
4
+ import { Autocomplete , Chip } from "@mui/material" ;
5
5
import TextField from "@material-ui/core/TextField" ;
6
6
import ApiSingleton from "../../api/ApiSingleton" ;
7
7
import ErrorsHandler from "../Utils/ErrorsHandler" ;
@@ -22,6 +22,8 @@ interface ICourseFilterState {
22
22
courseStudents : AccountDataDto [ ] ;
23
23
selectedHomeworks : HomeworkViewModel [ ] ;
24
24
selectedStudents : AccountDataDto [ ] ;
25
+ mentors : AccountDataDto [ ] ;
26
+ assignedStudents : MentorToAssignedStudentsDTO [ ]
25
27
}
26
28
27
29
// Если преподаватель не выбрал ни одного студента, по умолчанию регистрируем всех. Аналогично с выбором домашних работ
@@ -30,7 +32,9 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
30
32
courseHomeworks : [ ] ,
31
33
courseStudents : [ ] ,
32
34
selectedHomeworks : [ ] ,
33
- selectedStudents : [ ]
35
+ selectedStudents : [ ] ,
36
+ assignedStudents : [ ] ,
37
+ mentors : [ ]
34
38
} ) ;
35
39
36
40
// Состояние для отображения элемента загрузки
@@ -39,35 +43,36 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
39
43
// Состояние для отображения поля выбора студентов
40
44
const [ isStudentsSelectionHidden , setIsStudentsSelectionHidden ] = useState < boolean > ( props . isStudentsSelectionHidden ) ;
41
45
42
- // Если у преподавателя в workspace все студенты, отображаем "Все" в компоненте, значений при этом не выбрано.
43
- // Функция, необходимые для корректного отображения выбранных элементов.
44
- function getItemsView < T > ( selected : T [ ] , all : T [ ] ) : T [ ] {
45
- return selected . length === all . length ? [ ] : selected ;
46
- }
47
-
48
46
useEffect ( ( ) => {
49
47
const fetchCourseDataForMentor = async ( ) => {
50
48
try {
51
- const courseViewModel = await ApiSingleton . coursesApi . coursesGetAllCourseData ( props . courseId ) ;
49
+ const {
50
+ course,
51
+ assignedStudents
52
+ } = await ApiSingleton . coursesApi . coursesGetAllCourseData ( props . courseId ) ;
53
+ if ( course === undefined || assignedStudents === undefined ) return
54
+
52
55
const mentorWorkspace =
53
56
await ApiSingleton . coursesApi . coursesGetMentorWorkspace ( props . courseId , props . mentorId ) ;
54
57
55
58
props . onSelectedStudentsChange ( mentorWorkspace . students ?? [ ] )
56
59
props . onSelectedHomeworksChange ( mentorWorkspace . homeworks ?? [ ] )
57
60
58
61
// Для корректного отображения "Все" при инцициализации (получении данных с бэкенда)
59
- const allCourseStudentsCount = ( courseViewModel . acceptedStudents ?. length ?? 0 ) + ( courseViewModel . newStudents ?. length ?? 0 ) ;
62
+ const allCourseStudentsCount = ( course . acceptedStudents ?. length ?? 0 ) + ( course . newStudents ?. length ?? 0 ) ;
60
63
const initSelectedStudentsView = mentorWorkspace . students ?. length === allCourseStudentsCount ?
61
64
[ ] : ( mentorWorkspace . students ) ?? [ ] ;
62
- const initSelectedHomeworksView = mentorWorkspace . homeworks ?. length === courseViewModel . homeworks ?. length ?
65
+ const initSelectedHomeworksView = mentorWorkspace . homeworks ?. length === course . homeworks ?. length ?
63
66
[ ] : ( mentorWorkspace . homeworks ?? [ ] ) ;
64
67
65
68
setState ( prevState => ( {
66
69
...prevState ,
67
- courseHomeworks : courseViewModel . homeworks ?? [ ] ,
68
- courseStudents : courseViewModel . acceptedStudents ?? [ ] ,
70
+ courseHomeworks : course . homeworks ?? [ ] ,
71
+ courseStudents : course . acceptedStudents ?? [ ] ,
69
72
selectedStudents : initSelectedStudentsView ,
70
73
selectedHomeworks : initSelectedHomeworksView ,
74
+ mentors : course . mentors ! ,
75
+ assignedStudents : assignedStudents . filter ( x => x . mentorId !== props . mentorId )
71
76
} ) )
72
77
73
78
setIsLoading ( false ) ;
@@ -94,6 +99,14 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
94
99
props . onSelectedHomeworksChange ( state . selectedHomeworks )
95
100
} , [ state . selectedHomeworks ] ) ;
96
101
102
+ //TODO: memoize?
103
+ const getAssignedMentors = ( studentId : string ) =>
104
+ state . assignedStudents
105
+ . filter ( x => x . selectedStudentsIds ! . includes ( studentId ) )
106
+ . map ( x => state . mentors . find ( m => m . userId === x . mentorId ) )
107
+ . filter ( x => x !== undefined )
108
+ . map ( x => x . name + ' ' + x . surname )
109
+
97
110
return (
98
111
< div >
99
112
{ isLoading ? (
@@ -150,7 +163,11 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
150
163
multiple
151
164
fullWidth
152
165
options = { state . courseStudents }
153
- getOptionLabel = { ( option : AccountDataDto ) => option . surname + ' ' + option . name }
166
+ getOptionLabel = { ( option : AccountDataDto ) => {
167
+ const assignedMentors = getAssignedMentors ( option . userId ! )
168
+ const suffix = assignedMentors . length > 0 ? " (преподаватель " + assignedMentors [ 0 ] + ")" : ""
169
+ return option . surname + ' ' + option . name + suffix ;
170
+ } }
154
171
getOptionKey = { ( option : AccountDataDto ) => option . userId ?? "" }
155
172
filterSelectedOptions
156
173
isOptionEqualToValue = { ( option , value ) => option . userId === value . userId }
@@ -161,7 +178,19 @@ const CourseFilter: FC<ICourseFilterProps> = (props) => {
161
178
label = { state . selectedStudents . length === 0 ? "" : `Студенты (${ state . selectedStudents . length } )` }
162
179
placeholder = { state . selectedStudents . length === 0 ? "Все студенты" : "" }
163
180
/> ) }
164
- noOptionsText = { 'На курсе больше нет студентов' }
181
+ renderTags = { ( value , getTagProps ) =>
182
+ value . map ( ( option , index ) => {
183
+ const hasAssignedStudents = getAssignedMentors ( option . userId ! ) . length > 0
184
+ return (
185
+ < Chip
186
+ label = { option . surname + ' ' + option . name }
187
+ { ...getTagProps ( { index} ) }
188
+ color = { hasAssignedStudents ? "info" : "default" }
189
+ />
190
+ ) ;
191
+ } )
192
+ }
193
+ noOptionsText = { 'На курсе нет студентов' }
165
194
value = { state . selectedStudents }
166
195
onChange = { ( _ , values ) => {
167
196
setState ( ( prevState ) => ( {
0 commit comments