UNPKG

twreporter-react

Version:

React-Redux site for The Reporter Foundation in Taiwan

158 lines (137 loc) 5.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.articlesByIds = articlesByIds; exports.articlesByTopics = articlesByTopics; exports.articlesByTags = articlesByTags; exports.articlesByCats = articlesByCats; var _actionTypes = require('../constants/action-types'); var types = _interopRequireWildcard(_actionTypes); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 articlesByIds() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments[1]; switch (action.type) { case types.FETCH_ARTICLES_REQUEST: return Object.assign({}, state, { isFetching: true, ids: action.ids, error: null }); case types.FETCH_ARTICLES_FAILURE: return Object.assign({}, state, { isFetching: false, ids: action.ids, error: action.error, lastUpdated: action.failedAt }); case types.FETCH_ARTICLES_SUCCESS: return Object.assign({}, state, { isFetching: false, error: null, ids: action.ids, lastUpdated: action.receivedAt }); default: return state; } } function _groupArticles() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments[1]; var _state = {}; var id = action.id; switch (action.type) { case types.FETCH_ARTICLES_BY_TOPIC_ID_REQUEST: case types.FETCH_ARTICLES_BY_CAT_ID_REQUEST: case types.FETCH_ARTICLES_BY_TAG_ID_REQUEST: if (state.hasOwnProperty(id)) { _lodash2.default.merge(_state, { isFetching: true }); } else { _lodash2.default.merge(_state, { isFetching: true, error: null, hasMore: false, items: [] }); } return _lodash2.default.merge({}, state, _defineProperty({}, id, _state)); case types.FETCH_ARTICLES_BY_TOPIC_ID_FAILURE: case types.FETCH_ARTICLES_BY_CAT_ID_FAILURE: case types.FETCH_ARTICLES_BY_TAG_ID_FAILURE: _lodash2.default.merge(_state, { isFetching: false, error: action.error, lastUpdated: action.failedAt }); return _lodash2.default.merge({}, state, _defineProperty({}, id, _state)); case types.FETCH_ARTICLES_BY_TOPIC_ID_SUCCESS: case types.FETCH_ARTICLES_BY_CAT_ID_SUCCESS: case types.FETCH_ARTICLES_BY_TAG_ID_SUCCESS: _state = _lodash2.default.get(state, id); var total = _lodash2.default.get(_state, 'total'); var items = _lodash2.default.get(_state, 'items'); var hasMore = _lodash2.default.get(_state, 'hasMore'); if (!total) { total = _lodash2.default.get(action, 'response.meta.total'); } items = items.concat(_lodash2.default.get(action, 'response.result')); if (items.length >= total) { hasMore = false; } else { hasMore = true; } return _lodash2.default.merge({}, state, _defineProperty({}, id, { isFetching: false, hasMore: hasMore, error: null, items: items, lastUpdated: action.receivedAt })); default: return state; } } function articlesByTopics() { var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var action = arguments[1]; switch (action.type) { case types.FETCH_ARTICLES_BY_TOPIC_ID_REQUEST: case types.FETCH_ARTICLES_BY_TOPIC_ID_FAILURE: case types.FETCH_ARTICLES_BY_TOPIC_ID_SUCCESS: return _groupArticles(state, action); 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_TAG_ID_REQUEST: case types.FETCH_ARTICLES_BY_TAG_ID_FAILURE: case types.FETCH_ARTICLES_BY_TAG_ID_SUCCESS: return _groupArticles(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_CAT_ID_REQUEST: case types.FETCH_ARTICLES_BY_CAT_ID_FAILURE: case types.FETCH_ARTICLES_BY_CAT_ID_SUCCESS: return _groupArticles(state, action); default: return state; } }