-
Notifications
You must be signed in to change notification settings - Fork 216
/
challenge.js
86 lines (76 loc) · 2.66 KB
/
challenge.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
/**
* Reducer for state.challenge
*/
import _ from 'lodash';
import actions, { DETAIL_TABS } from 'actions/challenge';
import { getAuthTokens } from 'utils/tc';
import { updateQuery } from 'utils/url';
import { reducers } from 'topcoder-react-lib';
import { factory as smpFactory } from './my-submissions-management';
/**
* Handles challengeActions.toggleCheckpointFeedback action.
* @param {Object} state Previous state.
* @param {Object} action Action.
*/
function onToggleCheckpointFeedback(state, action) {
const { payload: { id, open } } = action;
const newCheckpointResults = _.clone(state.checkpoints.checkpointResults);
newCheckpointResults[id].expanded = _.isUndefined(open)
? !newCheckpointResults[id].expanded : open;
const newCheckpoints = {
...state.checkpoints,
checkpointResults: newCheckpointResults,
};
return {
...state,
checkpoints: newCheckpoints,
};
}
/**
* Handles CHALLENGE/SELECT_TAB action.
* @param {Object} state
* @param {Object} action
* @return {Object}
*/
function onSelectTab(state, { payload }) {
updateQuery({ tab: payload });
return { ...state, selectedTab: payload };
}
/**
* Factory which creates a new reducer with its initial state tailored to the
* ExpressJS HTTP request, if specified (for server-side rendering). If HTTP
* request is not specified, it creates just the default reducer.
* @param {Object} req Optional. ExpressJS HTTP request.
* @return Promise which resolves to the new reducer.
*/
export function factory(req) {
/* Server-side rendering of Submission Management Page. */
const options = {
auth: getAuthTokens(req),
initialState: {},
mergeReducers: {
[actions.challenge.toggleCheckpointFeedback]: onToggleCheckpointFeedback,
[actions.challenge.selectTab]: onSelectTab,
},
};
if (req && req.query && req.query.tab) {
options.initialState.selectedTab = req.query.tab;
} else {
options.initialState.selectedTab = DETAIL_TABS.DETAILS;
}
if (req && req.url.match(/^\/challenges\/\d+\/my-submissions/)) {
const challengeId = req.url.match(/\d+/)[0];
_.set(options, 'challenge.challengeDetails.id', challengeId);
_.set(options, 'challenge.challengeDetails.mySubmission', true);
} else if (req && req.url.match(/\/challenges\/\d+([?/].*)?$/)) {
const challengeId = req.url.match(/\d+/)[0];
_.set(options, 'challenge.challengeDetails.id', challengeId);
}
/* Otherwise this part of Redux state is initialized empty. */
return smpFactory().then((smp) => {
options.mySubmissionsManagement = smp;
return reducers.challenge.factory(options);
});
}
/* Default reducer with empty initial state. */
export default factory();