/
sectionProgressRedux.js
146 lines (135 loc) · 4.08 KB
/
sectionProgressRedux.js
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
import {SET_SCRIPT} from '@cdo/apps/redux/unitSelectionRedux';
import firehoseClient from '../../lib/util/firehose';
import {ViewType} from './sectionProgressConstants';
const SET_CURRENT_VIEW = 'sectionProgress/SET_CURRENT_VIEW';
const SET_LESSON_OF_INTEREST = 'sectionProgress/SET_LESSON_OF_INTEREST';
const START_LOADING_PROGRESS = 'sectionProgress/START_LOADING_PROGRESS';
const FINISH_LOADING_PROGRESS = 'sectionProgress/FINISH_LOADING_PROGRESS';
const START_REFRESHING_PROGRESS = 'sectionProgress/START_REFRESHING_PROGRESS';
const FINISH_REFRESHING_PROGRESS = 'sectionProgress/FINISH_REFRESHING_PROGRESS';
const ADD_DATA_BY_UNIT = 'sectionProgress/ADD_DATA_BY_UNIT';
// Action creators
export const startLoadingProgress = () => ({type: START_LOADING_PROGRESS});
export const finishLoadingProgress = () => ({type: FINISH_LOADING_PROGRESS});
export const startRefreshingProgress = () => ({
type: START_REFRESHING_PROGRESS,
});
export const finishRefreshingProgress = () => ({
type: FINISH_REFRESHING_PROGRESS,
});
export const setLessonOfInterest = lessonOfInterest => ({
type: SET_LESSON_OF_INTEREST,
lessonOfInterest,
});
export const setCurrentView = viewType => ({type: SET_CURRENT_VIEW, viewType});
export const addDataByUnit = data => ({
type: ADD_DATA_BY_UNIT,
data,
});
const INITIAL_LESSON_OF_INTEREST = 1;
const initialState = {
section: {},
currentView: ViewType.SUMMARY,
unitDataByUnit: {},
studentLevelProgressByUnit: {},
studentLessonProgressByUnit: {},
studentLastUpdateByUnit: {},
lessonOfInterest: INITIAL_LESSON_OF_INTEREST,
isLoadingProgress: false,
isRefreshingProgress: false,
};
export default function sectionProgress(state = initialState, action) {
if (action.type === SET_SCRIPT) {
return {
...state,
lessonOfInterest: INITIAL_LESSON_OF_INTEREST,
};
}
if (action.type === SET_CURRENT_VIEW) {
return {
...state,
currentView: action.viewType,
};
}
if (action.type === START_LOADING_PROGRESS) {
return {
...state,
isLoadingProgress: true,
};
}
if (action.type === FINISH_LOADING_PROGRESS) {
return {
...state,
isLoadingProgress: false,
};
}
if (action.type === START_REFRESHING_PROGRESS) {
return {
...state,
isRefreshingProgress: true,
};
}
if (action.type === FINISH_REFRESHING_PROGRESS) {
return {
...state,
isRefreshingProgress: false,
};
}
if (action.type === SET_LESSON_OF_INTEREST) {
return {
...state,
lessonOfInterest: action.lessonOfInterest,
};
}
if (action.type === ADD_DATA_BY_UNIT) {
return {
...state,
unitDataByUnit: {
...state.unitDataByUnit,
...action.data.unitDataByUnit,
},
studentLevelProgressByUnit: {
...state.studentLevelProgressByUnit,
...action.data.studentLevelProgressByUnit,
},
studentLessonProgressByUnit: {
...state.studentLessonProgressByUnit,
...action.data.studentLessonProgressByUnit,
},
studentLastUpdateByUnit: {
...state.studentLastUpdateByUnit,
...action.data.studentLastUpdateByUnit,
},
};
}
return state;
}
export const jumpToLessonDetails = lessonOfInterest => {
return (dispatch, getState) => {
const state = getState();
dispatch(setLessonOfInterest(lessonOfInterest));
dispatch(setCurrentView(ViewType.DETAIL));
firehoseClient.putRecord(
{
study: 'teacher_dashboard_actions',
study_group: 'progress',
event: 'view_change_toggle',
data_json: JSON.stringify({
section_id: state.teacherSections.selectedSectionId,
old_view: ViewType.SUMMARY,
new_view: ViewType.DETAIL,
script_id: state.unitSelection.scriptId,
}),
},
{includeUserId: true}
);
};
};
// Selector functions
/**
* Retrieves the unit data for the section in the selected unit
* @returns {unitDataPropType} object containing metadata about the unit structure
*/
export const getCurrentUnitData = state => {
return state.sectionProgress.unitDataByUnit[state.unitSelection.scriptId];
};