-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAddInstructorsSection.jsx
More file actions
159 lines (151 loc) · 7.14 KB
/
AddInstructorsSection.jsx
File metadata and controls
159 lines (151 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { useState, useEffect, useMemo } from 'react';
import {
Accordion,
AccordionDetails,
AccordionSummary,
Avatar,
Box,
Chip,
FormControl,
InputLabel,
MenuItem,
OutlinedInput,
Select,
Typography,
} from '@mui/material';
import { useAppContext } from '../../../src/render.jsx';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import PlusIcon from '@mui/icons-material/Add';
import CreateInstructorForm from './CreateInstructorForm';
function AddInstructorsSection({ onChangeCallback, activeOrganizationId, initialInstructorIds = [] }) {
const [orgInstructors, setOrgInstructors] = useState([]);
const [selectedIds, setSelectedIds] = useState(initialInstructorIds);
const [expanded, setExpanded] = useState(false);
const { localeMessages, apiBaseUrl } = useAppContext();
const hasInstructors = useMemo(() => orgInstructors.length > 0, [orgInstructors]);
const switchExpanded = () => {
if (hasInstructors) {
setExpanded(!expanded);
}
};
useEffect(() => {
fetch(`${apiBaseUrl}/organizations/${activeOrganizationId}/users/`, {
method: 'GET',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
})
.then((response) => response.json())
.then((data) => {
const instructors = (data.organization_users || []).filter(
(u) => u.can_act_as_instructor
);
setOrgInstructors(instructors);
if (instructors.length === 0) {
setExpanded(true);
}
})
.catch((error) => {
console.error('Error fetching organization users:', error);
});
}, []);
const handleSelectionChange = (event) => {
const value = event.target.value;
setSelectedIds(value);
if (onChangeCallback) {
onChangeCallback(value);
}
};
return (
<div>
{hasInstructors && (
<FormControl sx={{ mb: 2, minWidth: '100%' }}>
<InputLabel id="instructor-select-label">
{localeMessages['select_instructors']}
</InputLabel>
<Select
labelId="instructor-select-label"
multiple
value={selectedIds}
onChange={handleSelectionChange}
input={<OutlinedInput label={localeMessages['select_instructors']} />}
renderValue={(selected) => (
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
{selected.map((id) => {
const instructor = orgInstructors.find((i) => i.id === id);
return instructor ? (
<Chip
key={id}
label={instructor.display_name || instructor.email}
size="small"
avatar={
instructor.photo
? <Avatar src={instructor.photo_url} />
: <Avatar>{(instructor.display_name || instructor.email)[0].toUpperCase()}</Avatar>
}
onDelete={(e) => {
e.stopPropagation();
const updatedIds = selectedIds.filter((i) => i !== id);
setSelectedIds(updatedIds);
if (onChangeCallback) onChangeCallback(updatedIds);
}}
onMouseDown={(e) => e.stopPropagation()}
/>
) : null;
})}
</Box>
)}
>
{orgInstructors.map((instructor) => (
<MenuItem key={instructor.id} value={instructor.id}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5 }}>
{instructor.photo
? <Avatar src={instructor.photo_url} sx={{ width: 28, height: 28 }} />
: <Avatar sx={{ width: 28, height: 28, fontSize: 13 }}>{(instructor.display_name || instructor.email)[0].toUpperCase()}</Avatar>
}
<Box>
<Typography variant="body2" sx={{ fontWeight: 500, lineHeight: 1.2 }}>
{instructor.display_name || instructor.email}
</Typography>
{instructor.display_name && (
<Typography variant="caption" color="text.secondary" sx={{ lineHeight: 1 }}>
{instructor.email}
</Typography>
)}
</Box>
</Box>
</MenuItem>
))}
</Select>
</FormControl>
)}
<Accordion expanded={expanded} onChange={switchExpanded}>
<AccordionSummary
expandIcon={hasInstructors ? <ExpandMoreIcon /> : null}
aria-controls="new-instructor-content"
id="new-instructor-header"
>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<PlusIcon />
<Typography component="span">{localeMessages['new_instructor']}</Typography>
</Box>
</AccordionSummary>
<AccordionDetails>
<CreateInstructorForm
activeOrganizationId={activeOrganizationId}
onSuccess={(newOrgUser) => {
const updatedInstructors = [...orgInstructors, newOrgUser];
setOrgInstructors(updatedInstructors);
const updatedIds = [...selectedIds, newOrgUser.id];
setSelectedIds(updatedIds);
if (onChangeCallback) {
onChangeCallback(updatedIds);
}
setExpanded(false);
}}
/>
</AccordionDetails>
</Accordion>
</div>
);
}
export default AddInstructorsSection;