11import { Dialog , DialogBody , HTMLSelect } from '@blueprintjs/core' ;
22import { IconNames } from '@blueprintjs/icons' ;
3- import React from 'react' ;
3+ import React , { useEffect } from 'react' ;
4+ import { useDispatch } from 'react-redux' ;
45import { useNavigate } from 'react-router' ;
56
7+ import SessionActions from '../application/actions/SessionActions' ;
68import { Role } from '../application/ApplicationTypes' ;
79import { UserCourse } from '../application/types/SessionTypes' ;
10+ import { useTypedSelector } from '../utils/Hooks' ;
811
912type Props = {
1013 isOpen : boolean ;
@@ -15,18 +18,26 @@ type Props = {
1518
1619const DropdownCourses : React . FC < Props > = ( { isOpen, onClose, courses, courseId } ) => {
1720 const navigate = useNavigate ( ) ;
21+ const dispatch = useDispatch ( ) ;
22+ const latestCourse = useTypedSelector ( state => state . session . courseId ) ;
1823
1924 const options = courses . map ( course => ( {
2025 value : course . courseId ,
2126 label : course . courseName . concat ( ! course . viewable ? ' - disabled' : '' ) ,
2227 disabled : ! course . viewable && course . role !== Role . Admin
2328 } ) ) ;
2429
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 ) ) ) ;
2732 onClose ( ) ;
2833 } ;
2934
35+ useEffect ( ( ) => {
36+ if ( latestCourse ) {
37+ navigate ( `/courses/${ latestCourse } ` ) ;
38+ }
39+ } , [ latestCourse , navigate ] ) ;
40+
3041 return (
3142 < Dialog
3243 icon = { IconNames . PROPERTIES }
0 commit comments