UNPKG

twreporter-react

Version:

React-Redux site for The Reporter Foundation in Taiwan

121 lines (105 loc) 4.16 kB
/*eslint no-console: 0*/ /* global console */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.articlesByTags = articlesByTags; exports.articlesByCats = articlesByCats; var _index = require('../utils/index'); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _actionTypes = require('../constants/action-types'); var types = _interopRequireWildcard(_actionTypes); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function generateKey(groups) { groups = Array.isArray(groups) ? groups : [groups]; return groups.sort().join(); } function articles() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments[1]; var key = generateKey(action.groups); var _state = {}; switch (action.type) { case types.FETCH_ARTICLES_BY_CATS_REQUEST: case types.FETCH_ARTICLES_BY_TAGS_REQUEST: if (state.hasOwnProperty(key)) { (0, _lodash.merge)(_state, state[key], { isFetching: true }); } else { (0, _lodash.merge)(_state, { isFetching: true, error: null, nextUrl: null, items: [] }); } return (0, _lodash.merge)({}, state, _defineProperty({}, key, _state)); case types.FETCH_ARTICLES_BY_CATS_FAILURE: case types.FETCH_ARTICLES_BY_TAGS_FAILURE: if (state.hasOwnProperty(key)) { (0, _lodash.merge)(_state, state[key], { isFetching: false, error: action.error, lastUpdated: action.failedAt }); } else { (0, _lodash.merge)(_state, { isFetching: false, error: action.error, nextUrl: null, items: [], lastUpdated: action.failedAt }); } return (0, _lodash.merge)({}, state, _defineProperty({}, key, _state)); case types.FETCH_ARTICLES_BY_CATS_SUCCESS: case types.FETCH_ARTICLES_BY_TAGS_SUCCESS: var nextUrl = null; var response = action.response; var nextHref = _lodash2.default.get(action, 'response.links.next.href'); if (nextHref) { var embedded = JSON.stringify((0, _index.getArticleEmbeddedQuery)()); nextUrl = (0, _index.formatUrl)(nextHref + '&embedded=' + embedded); } var items = state[key] && state[key].items ? state[key].items.concat(response.result) : response.result; (0, _lodash.merge)(_state, state[key], { isFetching: false, nextUrl: nextUrl, error: null, items: items, lastUpdated: action.receivedAt }); return (0, _lodash.merge)({}, state, _defineProperty({}, key, _state)); default: return state; } } function articlesByTags() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; switch (action.type) { case types.FETCH_ARTICLES_BY_TAGS_REQUEST: case types.FETCH_ARTICLES_BY_TAGS_FAILURE: case types.FETCH_ARTICLES_BY_TAGS_SUCCESS: return articles(state, action); default: return state; } } function articlesByCats() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; switch (action.type) { case types.FETCH_ARTICLES_BY_CATS_REQUEST: case types.FETCH_ARTICLES_BY_CATS_FAILURE: case types.FETCH_ARTICLES_BY_CATS_SUCCESS: return articles(state, action); default: return state; } }