22
33import { type ReduxState } from '../index'
44import { getBalances , type BalancesShapeType } from '../../lib/financials'
5+ import {
6+ loadCachedCourses ,
7+ updateStoredCourses ,
8+ areAnyTermsCached ,
9+ } from '../../lib/course-search'
10+ import type { CourseType } from '../../lib/course-search'
511
612const UPDATE_BALANCES_SUCCESS = 'sis/UPDATE_BALANCES_SUCCESS'
713const UPDATE_BALANCES_FAILURE = 'sis/UPDATE_BALANCES_FAILURE'
14+ const LOAD_CACHED_COURSES = 'sis/LOAD_CACHED_COURSES'
15+ const COURSES_LOADED = 'sis/COURSES_LOADED'
816
917type UpdateBalancesSuccessAction = { |
1018 type : 'sis/UPDATE_BALANCES_SUCCESS' ,
@@ -39,7 +47,51 @@ export function updateBalances(
3947 }
4048}
4149
42- type Action = UpdateBalancesActions
50+ type LoadCachedCoursesAction = { |
51+ type : 'sis/LOAD_CACHED_COURSES' ,
52+ payload : Array < CourseType > ,
53+ | }
54+ type CoursesLoadedAction = { |
55+ type : 'sis/COURSES_LOADED' ,
56+ | }
57+
58+ export type LoadCourseDataActionType = ThunkAction <
59+ LoadCachedCoursesAction | CoursesLoadedAction ,
60+ >
61+ export type UpdateCourseDataActionType = ThunkAction <
62+ LoadCachedCoursesAction | CoursesLoadedAction ,
63+ >
64+
65+ export function loadCourseDataIntoMemory ( ) : LoadCourseDataActionType {
66+ return async dispatch => {
67+ const areAnyCached = await areAnyTermsCached ( )
68+
69+ if ( ! areAnyCached ) {
70+ return
71+ }
72+
73+ const cachedCourses = await loadCachedCourses ( )
74+ dispatch ( { type : LOAD_CACHED_COURSES , payload : cachedCourses } )
75+ dispatch ( { type : COURSES_LOADED } )
76+ }
77+ }
78+
79+ export function updateCourseData ( ) : UpdateCourseDataActionType {
80+ return async dispatch => {
81+ const updateNeeded = await updateStoredCourses ( )
82+
83+ if ( updateNeeded ) {
84+ const cachedCourses = await loadCachedCourses ( )
85+ dispatch ( { type : LOAD_CACHED_COURSES , payload : cachedCourses } )
86+ dispatch ( { type : COURSES_LOADED } )
87+ }
88+ }
89+ }
90+
91+ type Action =
92+ | UpdateBalancesActions
93+ | LoadCachedCoursesAction
94+ | CoursesLoadedAction
4395
4496export type State = { |
4597 balancesErrorMessage : ?string ,
@@ -49,7 +101,10 @@ export type State = {|
49101 mealsRemainingToday : ?string ,
50102 mealsRemainingThisWeek : ?string ,
51103 mealPlanDescription : ?string ,
104+ allCourses : Array < CourseType > ,
105+ courseDataState : 'not-loaded' | 'ready' ,
52106| }
107+
53108const initialState = {
54109 balancesErrorMessage : null ,
55110 flexBalance : null ,
@@ -58,7 +113,10 @@ const initialState = {
58113 mealsRemainingToday : null ,
59114 mealsRemainingThisWeek : null ,
60115 mealPlanDescription : null ,
116+ allCourses : [ ] ,
117+ courseDataState : 'not-loaded' ,
61118}
119+
62120export function sis ( state : State = initialState , action : Action ) {
63121 switch ( action . type ) {
64122 case UPDATE_BALANCES_FAILURE :
@@ -77,6 +135,11 @@ export function sis(state: State = initialState, action: Action) {
77135 }
78136 }
79137
138+ case LOAD_CACHED_COURSES :
139+ return { ...state , allCourses : action . payload }
140+ case COURSES_LOADED :
141+ return { ...state , courseDataState : 'ready' }
142+
80143 default :
81144 return state
82145 }
0 commit comments