2018-01-30 11:53:12 -05:00
|
|
|
const keyMirror = require('keymirror');
|
|
|
|
const api = require('../lib/api');
|
2016-05-19 11:34:59 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
const Types = keyMirror({
|
2016-05-19 11:34:59 -04:00
|
|
|
SET_DETAILS: null,
|
|
|
|
SET_DETAILS_FETCHING: null,
|
|
|
|
SET_DETAILS_ERROR: null
|
|
|
|
});
|
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
module.exports.detailsReducer = (state, action) => {
|
2016-05-19 11:34:59 -04:00
|
|
|
if (typeof state === 'undefined') {
|
|
|
|
state = {};
|
|
|
|
}
|
|
|
|
switch (action.type) {
|
|
|
|
case Types.SET_DETAILS:
|
|
|
|
return action.details;
|
|
|
|
case Types.SET_DETAILS_FETCHING:
|
|
|
|
return {fetching: action.fetching};
|
|
|
|
case Types.SET_DETAILS_ERROR:
|
|
|
|
return {error: action.error};
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
module.exports.setDetailsError = error => ({
|
|
|
|
type: Types.SET_DETAILS_ERROR,
|
|
|
|
error: error
|
|
|
|
});
|
2016-05-19 11:34:59 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
module.exports.setDetails = details => ({
|
|
|
|
type: Types.SET_DETAILS,
|
|
|
|
details: details
|
|
|
|
});
|
2016-05-19 11:34:59 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
module.exports.setDetailsFetching = () => ({
|
|
|
|
type: Types.SET_DETAILS_FETCHING,
|
|
|
|
fetching: true
|
|
|
|
});
|
2016-05-19 11:34:59 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
module.exports.getDetails = id => (dispatch => {
|
|
|
|
api({
|
|
|
|
uri: `/conference/${id}/details`
|
|
|
|
}, (err, body) => {
|
|
|
|
if (err) {
|
|
|
|
dispatch(module.exports.setDetailsError(err));
|
|
|
|
return;
|
|
|
|
}
|
2018-01-19 14:06:26 -05:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
if (typeof body !== 'undefined') {
|
|
|
|
const columns = body.columns;
|
|
|
|
if (body.rows) {
|
|
|
|
const details = body.rows[0];
|
|
|
|
const detailsObject = details.reduce((prev, cur, index) => {
|
|
|
|
prev[columns[index]] = cur;
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
dispatch(module.exports.setDetails(detailsObject));
|
2018-01-30 09:54:45 -05:00
|
|
|
} else {
|
2018-01-30 11:53:12 -05:00
|
|
|
dispatch(module.exports.setDetailsError('Not Found'));
|
2018-01-30 09:54:45 -05:00
|
|
|
}
|
2018-01-30 11:53:12 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dispatch(module.exports.setDetailsError('An unexpected error occurred'));
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports.startGetDetails = id => (dispatch => {
|
|
|
|
dispatch(module.exports.setDetailsFetching());
|
|
|
|
dispatch(module.exports.getDetails(id));
|
|
|
|
});
|