UNPKG

twreporter-redux

Version:

redux actions and reducers for twreporter website

123 lines (92 loc) 3.23 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.posts = exports.post = undefined; var _actionTypes = require('../constants/action-types'); var _actionTypes2 = _interopRequireDefault(_actionTypes); var _concat = require('lodash/concat'); var _concat2 = _interopRequireDefault(_concat); var _get = require('lodash/get'); var _get2 = _interopRequireDefault(_get); var _map = require('lodash/map'); var _map2 = _interopRequireDefault(_map); var _merge = require('lodash/merge'); var _merge2 = _interopRequireDefault(_merge); var _set = require('lodash/set'); var _set2 = _interopRequireDefault(_set); 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; } // lodash var _ = { concat: _concat2.default, get: _get2.default, map: _map2.default, merge: _merge2.default, set: _set2.default }; function post() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; switch (action.type) { case _actionTypes2.default.GET_A_FULL_POST: { return { slug: _.get(action, 'payload.slug'), error: null, isFetching: false }; } case _actionTypes2.default.START_TO_GET_A_FULL_POST: return { isFetching: true, url: action.url }; case _actionTypes2.default.ERROR_TO_GET_A_FULL_POST: return { error: action.error }; default: return state; } } function posts() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; switch (action.type) { case _actionTypes2.default.GET_LISTED_POSTS: { var items = _.get(action, 'payload.items', []); var total = _.get(action, 'payload.total', 0); var listID = _.get(action, 'payload.listID', ''); var list = _.get(state, listID, { items: [], total: 0, error: null }); list.items = _.concat(list.items, _.map(items, function (item) { return item.slug; })); list.total = total; list.error = null; return _.merge({}, state, _defineProperty({}, listID, list)); } case _actionTypes2.default.ERROR_TO_GET_LISTED_POSTS: { var _listID = _.get(action, 'listID'); var _list = _.get(state, _listID, {}); _list.error = _.get(action, 'error'); return _.merge({}, state, _defineProperty({}, _listID, _list)); } case _actionTypes2.default.START_TO_GET_POSTS: console.log('url to fetch:', action.url); return state; case _actionTypes2.default.ERROR_TO_GET_POSTS: console.warn(action.type + ' : ' + action.errorMsg.toString()); return state; default: return state; } } exports.post = post; exports.posts = posts;