Skip to content
This repository was archived by the owner on Mar 5, 2019. It is now read-only.

Commit 4cea67d

Browse files
committed
ui: use redux-promise-middleware to request lists of entities
This dispatches the FETCH_ENTITIES action whose payload is an axios request object. redux-promise-middleware will handle this by executing the request's promise, and then magically dispatching FETCH_ENTITIES_{PENDING,FULFILLED,REJECTED} actions according to the 3 possible stages of the conversation with the API endpoint. Currently these 3 actions are not handled by any reducer; that will come in subsequent commits.
1 parent 12c0ca1 commit 4cea67d

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

src/js/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@n3dst4/browser-bundle": "^1.1.0",
2222
"@n3dst4/build-stylesheets": "^1.0.1",
23+
"axios": "^0.15.3",
2324
"babel-polyfill": "6.9.1",
2425
"browser-sync": "^2.12.3",
2526
"d3": "^4.3.0",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
export const TOGGLE_ABOUT = "TOGGLE_ABOUT"
2+
23
export const ADD_ENTITY = "ADD_ENTITY"
4+
5+
export const FETCH_ENTITIES = "FETCH_ENTITIES"
6+
export const FETCH_ENTITIES_PENDING = "FETCH_ENTITIES_PENDING"
7+
export const FETCH_ENTITIES_FULFILLED = "FETCH_ENTITIES_FULFILLED"
8+
export const FETCH_ENTITIES_REJECTED = "FETCH_ENTITIES_REJECTED"

src/js/frontend/src/Redux/actions.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as actionTypes from "./action-types"
2+
import entityData from "../lib/entityData"
23

34
// These are action creators
45
// http://redux.js.org/docs/basics/Actions.html#action-creators
@@ -17,3 +18,17 @@ export const addEntity = (entityType, id, data) => {
1718
data,
1819
}
1920
}
21+
22+
export const fetchEntities = (entityType) => {
23+
const request = entityData.fetchEntitiesRequest(entityType);
24+
25+
// When dispatched, redux-promise-middleware will handle this and
26+
// magically dispatch FETCH_ENTITIES_{PENDING,FULFILLED,REJECTED}
27+
// actions according to the 3 possible stages of the conversation
28+
// with the API endpoint.
29+
return {
30+
type: actionTypes.FETCH_ENTITIES,
31+
meta: { entityType },
32+
payload: request
33+
}
34+
}

src/js/frontend/src/containers/EntityListContainer.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import Radium from "radium"
33
import { connect } from 'react-redux';
44

55
import * as actions from "../Redux/actions"
6+
import entityData from "../lib/entityData"
67

78
class EntityListContainer extends React.Component {
89
componentWillMount () {
10+
this.props.fetchEntities(this.props.route.entityType);
911
}
1012

1113
render () {
@@ -29,8 +31,14 @@ class EntityListContainer extends React.Component {
2931
// entities: state.get("entities"),
3032
// };
3133
// };
32-
//
33-
// export default connect(mapStateToProps)(Radium(EntityListContainer));
3434

35-
export default Radium(EntityListContainer);
35+
const mapDispatchToProps = (dispatch) => {
36+
return {
37+
fetchEntities: (entityType) => {
38+
dispatch(actions.fetchEntities(entityType));
39+
}
40+
}
41+
}
42+
43+
export default connect(null, mapDispatchToProps)(Radium(EntityListContainer));
3644

src/js/frontend/src/lib/api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ let api = {
88
URL: function (path) {
99
return this.protocol + "://" + this.host + ":" + this.port +
1010
this.apiPathPrefix + path;
11+
},
12+
13+
fetchEntitiesURL: function (entityType) {
14+
return this.URL(entityType);
1115
}
1216
}
1317

src/js/frontend/src/lib/entityData.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let d3 = require('d3');
22
let api = require('./api');
3+
import axios from "axios"
34

45
let entityData = {
56
apiURL: function(entityType, entityId) {
@@ -14,6 +15,7 @@ let entityData = {
1415
return this.validTypes.indexOf(entityType) >= 0;
1516
},
1617

18+
// FIXME: retire this in favour of fetchEntitiesRequest() below
1719
fetch: function(entityType, entityId, onSuccess) {
1820
let url = this.apiURL(entityType, entityId);
1921
console.debug("Fetching from " + url);
@@ -47,6 +49,15 @@ let entityData = {
4749
onSuccess(json);
4850
};
4951
},
52+
53+
fetchEntitiesRequest: function (entityType) {
54+
return axios.get(api.fetchEntitiesURL(entityType), {
55+
headers: {
56+
"Content-Type": "application/json",
57+
"Accept": "application/vnd.api+json"
58+
},
59+
});
60+
}
5061
}
5162

5263
module.exports = entityData;

0 commit comments

Comments
 (0)