-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathDropdownCourses.tsx
63 lines (55 loc) · 1.8 KB
/
DropdownCourses.tsx
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
import { Dialog, DialogBody, HTMLSelect } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import React, { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { useNavigate } from 'react-router';
import SessionActions from '../application/actions/SessionActions';
import { Role } from '../application/ApplicationTypes';
import { UserCourse } from '../application/types/SessionTypes';
import { useTypedSelector } from '../utils/Hooks';
type Props = {
isOpen: boolean;
onClose: () => void;
courses: UserCourse[];
courseId?: number;
};
const DropdownCourses: React.FC<Props> = ({ isOpen, onClose, courses, courseId }) => {
const navigate = useNavigate();
const dispatch = useDispatch();
const latestCourse = useTypedSelector(state => state.session.courseId);
const options = courses.map(course => ({
value: course.courseId,
label: course.courseName.concat(!course.viewable ? ' - disabled' : ''),
disabled: !course.viewable && course.role !== Role.Admin
}));
const onChangeHandler = async (e: React.ChangeEvent<HTMLSelectElement>) => {
await dispatch(SessionActions.updateLatestViewedCourse(Number(e.currentTarget.value)));
onClose();
};
useEffect(() => {
if (latestCourse) {
navigate(`/courses/${latestCourse}`);
}
}, [latestCourse, navigate]);
return (
<Dialog
icon={IconNames.PROPERTIES}
isCloseButtonShown={true}
isOpen={isOpen}
onClose={onClose}
title="My Courses"
>
<DialogBody>
<p>Select a course to switch to:</p>
<HTMLSelect
value={courseId}
options={options}
fill
onChange={onChangeHandler}
disabled={courses.length <= 1}
/>
</DialogBody>
</Dialog>
);
};
export default DropdownCourses;