Skip to content

Commit 9c53980

Browse files
Matthew Sessionssdb1228
authored andcommitted
Adds redux actions/reducers for collaborations
Fixes PLAT-1568 Test Plan: This is currently unused code. As long as /courses/:course_id/collaborations doesnt die then everything is working properly Change-Id: I832a7e32d486ba37c746e733762521aed9d8536f Reviewed-on: https://gerrit.instructure.com/80699 Tested-by: Jenkins Reviewed-by: Steven Burnett <sburnett@instructure.com> Product-Review: Steven Burnett <sburnett@instructure.com> QA-Review: Steven Burnett <sburnett@instructure.com>
1 parent 216ab21 commit 9c53980

6 files changed

Lines changed: 163 additions & 11 deletions

File tree

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
define([
22
'react'
3-
], function (React) {
4-
return React.createClass({
5-
displayName: 'CollaborationsApp',
3+
], (React) => {
4+
class CollaborationsApp extends React.Component {
5+
static propTypes: {
6+
applicationState: React.PropTypes.object,
7+
actions: React.PropTypes.object
8+
}
69

710
render () {
811
return (
912
<div className='CollaborationsApp'>
1013
We've got ourselves a placeholder
1114
</div>
12-
)
15+
);
1316
}
14-
})
15-
})
17+
};
18+
19+
return CollaborationsApp;
20+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
define([
2+
], () => {
3+
const actions = {}
4+
5+
actions.LIST_COLLABORATIONS_START = 'LIST_COLLABORATIONS_START'
6+
actions.listCollaborationsStart = () => ({ type: actions.LIST_COLLABORATIONS_START }),
7+
8+
actions.LIST_COLLABORATIONS_SUCCESSFUL = 'LIST_COLLABORATIONS_SUCCESSFUL'
9+
actions.listCollaborationsSuccessful = (collaborations) => ({ type: actions.LIST_COLLABORATIONS_SUCCESSFUL, payload: collaborations }),
10+
11+
actions.LIST_COLLABORATIONS_FAILED = 'LIST_COLLABORATIONS_FAILED'
12+
actions.listCollaborationsFailed = (error) => ({ type: actions.LIST_COLLABORATIONS_FAILED, error: true, payload: error })
13+
14+
actions.getCollaborations = () => {
15+
actions.listCollaborationsStart();
16+
17+
//do some async work
18+
19+
//on success
20+
actions.listCollaborationsSuccessful(collaborations);
21+
22+
//on failure
23+
actions.listCollaborationsFailed(error)
24+
}
25+
26+
return actions
27+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
define([
2+
'redux',
3+
'../actions/collaborationsActions'
4+
], (redux, ACTION_NAMES) => {
5+
let initialState = {
6+
listCollaborationsPending: false,
7+
listCollaborationsSuccessful: false,
8+
listCollaborationsError: null,
9+
collaborations: [],
10+
}
11+
12+
let collaborationsHandlers = {
13+
[ACTION_NAMES.LIST_COLLABORATIONS_START]: (state, action) => {
14+
return {
15+
...state,
16+
listCollaborationsPending: true,
17+
listCollaborationsSuccessful: false,
18+
listCollaborationsError: null
19+
};
20+
},
21+
[ACTION_NAMES.LIST_COLLABORATIONS_SUCCESSFUL]: (state, action) => {
22+
return {
23+
...state,
24+
listCollaborationsPending: false,
25+
listCollaborationsSuccessful: true,
26+
collaborations: action.payload
27+
}
28+
},
29+
[ACTION_NAMES.LIST_COLLABORATIONS_FAILED]: (state, action) => {
30+
return {
31+
...state,
32+
listCollaborationsPending: false,
33+
listCollaborationsError: action.payload
34+
}
35+
}
36+
};
37+
38+
return (state = initialState, action) => {
39+
if (collaborationsHandlers[action.type]) {
40+
return collaborationsHandlers[action.type](state, action)
41+
} else {
42+
return state
43+
}
44+
}
45+
})

app/jsx/collaborations/router.jsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ define([
22
'react',
33
'page',
44
'qs',
5-
'jsx/collaborations/CollaborationsApp'
6-
], function (React, page, qs, CollaborationsApp) {
5+
'redux',
6+
'jsx/collaborations/CollaborationsApp',
7+
'jsx/collaborations/actions/collaborationsActions',
8+
'jsx/collaborations/store/store'
9+
], function (React, page, qs, redux, CollaborationsApp, collaborationsActions, store) {
10+
let unsubscribe
11+
let actions = redux.bindActionCreators(collaborationsActions, store.dispatch)
712

813
/**
914
* Route Handlers
1015
*/
1116
function renderShowCollaborations (ctx) {
12-
React.render(
13-
<CollaborationsApp />
14-
, document.getElementById('content'));
17+
let view = () => {
18+
let state = store.getState();
19+
React.render(<CollaborationsApp applicationState={state} actions={actions} />, document.getElementById('content'));
20+
};
21+
unsubscribe = store.subscribe(view);
22+
view();
1523
}
1624

1725
/**
@@ -28,6 +36,7 @@ define([
2836
*/
2937
page('*', parseQueryString); // Middleware to parse querystring to object
3038
page('/:context(courses|groups)/:contextId/lti_collaborations', renderShowCollaborations);
39+
page.exit('*', unsubscribe)
3140

3241
return {
3342
start () {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
define([
2+
'redux',
3+
'../reducers/collaborationsReducer'
4+
], ({createStore, combineReducers}, collaborationsReducer) => {
5+
return createStore(combineReducers({
6+
collaborationsReducer
7+
}))
8+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
define([
2+
'jsx/collaborations/reducers/collaborationsReducer',
3+
'jsx/collaborations/actions/collaborationsActions'
4+
], (reducer, actions) => {
5+
module('collaborationsReducer');
6+
7+
const defaults = reducer(undefined, {})
8+
9+
test('there are defaults', () => {
10+
equal(Array.isArray(defaults.collaborations), true);
11+
equal(defaults.collaborations.length, 0);
12+
equal(defaults.listCollaborationsPending, false);
13+
equal(defaults.listCollaborationsSuccessful, false);
14+
equal(defaults.listCollaborationsError, null);
15+
});
16+
17+
test('responds to listCollaborationsStart', () => {
18+
let state = {
19+
listCollaborationsPending: false,
20+
listCollaborationsSuccessful: true,
21+
listCollaborationsError: {}
22+
};
23+
24+
let action = actions.listCollaborationsStart();
25+
let newState = reducer(state, action);
26+
equal(newState.listCollaborationsPending, true);
27+
equal(newState.listCollaborationsSuccessful, false);
28+
equal(newState.listCollaborationsError, null);
29+
});
30+
31+
test('responds to listCollaborationsSuccessful', () => {
32+
let state = {
33+
listCollaborationsPending: true,
34+
listCollaborationsSuccessful: false,
35+
collaborations: []
36+
};
37+
let collaborations = [{}];
38+
39+
let action = actions.listCollaborationsSuccessful(collaborations);
40+
let newState = reducer(state, action);
41+
equal(newState.listCollaborationsPending, false);
42+
equal(newState.listCollaborationsSuccessful, true);
43+
equal(newState.collaborations, collaborations);
44+
});
45+
46+
test('responds to listCollaborationsFailed', () => {
47+
let state = {
48+
listCollaborationsPending: true,
49+
listCollaborationsError: null
50+
};
51+
let error = {};
52+
53+
let action = actions.listCollaborationsFailed(error);
54+
let newState = reducer(state, action);
55+
equal(newState.listCollaborationsPending, false);
56+
equal(newState.listCollaborationsError, error);
57+
});
58+
});

0 commit comments

Comments
 (0)