1
1
import { Dialog , DialogBody , HTMLSelect } from '@blueprintjs/core' ;
2
2
import { IconNames } from '@blueprintjs/icons' ;
3
- import React from 'react' ;
3
+ import React , { useEffect } from 'react' ;
4
+ import { useDispatch } from 'react-redux' ;
4
5
import { useNavigate } from 'react-router' ;
5
6
7
+ import SessionActions from '../application/actions/SessionActions' ;
6
8
import { Role } from '../application/ApplicationTypes' ;
7
9
import { UserCourse } from '../application/types/SessionTypes' ;
10
+ import { useTypedSelector } from '../utils/Hooks' ;
8
11
9
12
type Props = {
10
13
isOpen : boolean ;
@@ -15,18 +18,26 @@ type Props = {
15
18
16
19
const DropdownCourses : React . FC < Props > = ( { isOpen, onClose, courses, courseId } ) => {
17
20
const navigate = useNavigate ( ) ;
21
+ const dispatch = useDispatch ( ) ;
22
+ const latestCourse = useTypedSelector ( state => state . session . courseId ) ;
18
23
19
24
const options = courses . map ( course => ( {
20
25
value : course . courseId ,
21
26
label : course . courseName . concat ( ! course . viewable ? ' - disabled' : '' ) ,
22
27
disabled : ! course . viewable && course . role !== Role . Admin
23
28
} ) ) ;
24
29
25
- const onChangeHandler = ( e : React . ChangeEvent < HTMLSelectElement > ) => {
26
- navigate ( `/courses/ ${ e . currentTarget . value } ` ) ;
30
+ const onChangeHandler = async ( e : React . ChangeEvent < HTMLSelectElement > ) => {
31
+ await dispatch ( SessionActions . updateLatestViewedCourse ( Number ( e . currentTarget . value ) ) ) ;
27
32
onClose ( ) ;
28
33
} ;
29
34
35
+ useEffect ( ( ) => {
36
+ if ( latestCourse ) {
37
+ navigate ( `/courses/${ latestCourse } ` ) ;
38
+ }
39
+ } , [ latestCourse , navigate ] ) ;
40
+
30
41
return (
31
42
< Dialog
32
43
icon = { IconNames . PROPERTIES }
0 commit comments