UNPKG

@twreporter/redux

Version:

redux actions and reducers for twreporter website

263 lines (260 loc) 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _reduxStateFieldNames = _interopRequireDefault(require("../constants/redux-state-field-names")); var _actionTypes = _interopRequireDefault(require("../constants/action-types")); var _concat = _interopRequireDefault(require("lodash/concat")); var _forEach = _interopRequireDefault(require("lodash/forEach")); var _get = _interopRequireDefault(require("lodash/get")); var _values = _interopRequireDefault(require("lodash/values")); var _snakeCase = _interopRequireDefault(require("lodash/snakeCase")); var _find = _interopRequireDefault(require("lodash/find")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /* eslint no-param-reassign: ["error", { "props": false }] */ // lodash var _ = { concat: _concat["default"], forEach: _forEach["default"], get: _get["default"], values: _values["default"], snakeCase: _snakeCase["default"], find: _find["default"] }; var defaultState = { posts: { allIds: [], byId: {}, slugToId: {} }, topics: { allIds: [], byId: {}, slugToId: {} } }; /** This function will check each entity in `entities` argument, * if the `entity.id` is not in `allIds` argument array or * `overwriteExisted` argument is true , then we will put this entity in the returned object. * * @param {string[]} allIds - array of entity ids * @param {{id: string, slug: string, full: boolean}[]} entities - array of entities * @param {boolean} [overwriteExisted=false] - not to overwrite existed entity if false, otherwise overwrite * @return {{byId: Object, slugToId: Object, allIds: string[]}} */ function _buildState(allIds, entities) { var overwriteExisted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var _entities = []; if (!Array.isArray(entities) && _typeof(entities) === 'object') { _entities.push(entities); } else { _entities = entities; } var byId = {}; var ids = _.concat(allIds); var slugToId = {}; _.forEach(_entities, function (entity) { var id = _.get(entity, 'id', ''); var slug = _.get(entity, 'slug', ''); if (ids.indexOf(id) === -1) { ids.push(id); slugToId[slug] = id; byId[id] = entity; } else if (overwriteExisted) { slugToId[slug] = id; byId[id] = entity; } }); return { byId: byId, slugToId: slugToId, allIds: ids }; } /** * @param {import('../typedef').Entities} [state=defaultState] * @param {Object} [action={}] * @param {string} [action.type] * @param {Object} [action.payload] * @return {import('../typedef').Entities} */ function entities() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultState; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; switch (action.type) { case _actionTypes["default"].indexPage.read.success: { var allPostIds = state.posts.allIds; var allTopicIds = state.topics.allIds; var fieldKeys = _.concat(_.values(_reduxStateFieldNames["default"].sections), _.values(_reduxStateFieldNames["default"].categories)); var posts = []; var topics = []; _.forEach(fieldKeys, function (fieldKey) { var key = _.snakeCase(fieldKey); var entities = _.get(action, ['payload', 'items', key]); if (Array.isArray(entities)) { if (key !== _reduxStateFieldNames["default"].sections.latestTopicSection && key !== _reduxStateFieldNames["default"].sections.topicsSection) { posts = _.concat(posts, entities); } else { topics = _.concat(topics, entities); } } }); var newStateForPosts = _buildState(allPostIds, posts); var newStateForTopics = _buildState(allTopicIds, topics); return { posts: { allIds: newStateForPosts.allIds, byId: Object.assign({}, state.posts.byId, newStateForPosts.byId), slugToId: Object.assign({}, state.posts.slugToId, newStateForPosts.slugToId) }, topics: { allIds: newStateForTopics.allIds, byId: Object.assign({}, state.topics.byId, newStateForTopics.byId), slugToId: Object.assign({}, state.topics.slugToId, newStateForTopics.slugToId) } }; } case _actionTypes["default"].topics.read.success: { var _allTopicIds = state.topics.allIds; var _topics = _.get(action, 'payload.items', []); var _buildState2 = _buildState(_allTopicIds, _topics), allIds = _buildState2.allIds, byId = _buildState2.byId, slugToId = _buildState2.slugToId; return _objectSpread(_objectSpread({}, state), {}, { topics: { allIds: allIds, byId: Object.assign({}, state.topics.byId, byId), slugToId: Object.assign({}, state.topics.slugToId, slugToId) } }); } case _actionTypes["default"].selectedPost.read.success: { var _allPostIds = state.posts.allIds; var post = _.get(action, 'payload.post', {}); var _buildState3 = _buildState(_allPostIds, post, true), _allIds = _buildState3.allIds, _byId = _buildState3.byId, _slugToId = _buildState3.slugToId; return _objectSpread(_objectSpread({}, state), {}, { posts: { allIds: _allIds, byId: Object.assign({}, state.posts.byId, _byId), slugToId: Object.assign({}, state.posts.slugToId, _slugToId) } }); } case _actionTypes["default"].selectedTopic.read.success: { var _allTopicIds2 = state.topics.allIds; var topic = _.get(action, 'payload.topic', {}); var _buildState4 = _buildState(_allTopicIds2, topic, true), _allIds2 = _buildState4.allIds, _byId2 = _buildState4.byId, _slugToId2 = _buildState4.slugToId; return _objectSpread(_objectSpread({}, state), {}, { topics: { allIds: _allIds2, byId: Object.assign({}, state.topics.byId, _byId2), slugToId: Object.assign({}, state.topics.slugToId, _slugToId2) } }); } case _actionTypes["default"].postsByListId.read.success: { var _allPostIds2 = state.posts.allIds; var _posts = _.get(action, 'payload.items', []); var _buildState5 = _buildState(_allPostIds2, _posts, true), _allIds3 = _buildState5.allIds, _byId3 = _buildState5.byId, _slugToId3 = _buildState5.slugToId; return _objectSpread(_objectSpread({}, state), {}, { posts: { allIds: _allIds3, byId: Object.assign({}, state.posts.byId, _byId3), slugToId: Object.assign({}, state.posts.slugToId, _slugToId3) } }); } case _actionTypes["default"].relatedPosts.read.success: { var _allPostIds3 = state.posts.allIds; var _posts2 = _.get(action, 'payload.items', []); var _buildState6 = _buildState(_allPostIds3, _posts2), _allIds4 = _buildState6.allIds, _byId4 = _buildState6.byId, _slugToId4 = _buildState6.slugToId; return _objectSpread(_objectSpread({}, state), {}, { posts: { allIds: _allIds4, byId: Object.assign({}, state.posts.byId, _byId4), slugToId: Object.assign({}, state.posts.slugToId, _slugToId4) } }); } case _actionTypes["default"].featureTopic.read.success: { var _allPostIds4 = state.posts.allIds; var _allTopicIds3 = state.topics.allIds; var _posts3 = _.get(action, 'payload.lastThreeRelatedPosts', []); var _topics2 = [_.get(action, 'payload.topic', {})]; var _newStateForPosts = _buildState(_allPostIds4, _posts3); var _newStateForTopics = _buildState(_allTopicIds3, _topics2); return { posts: { allIds: _newStateForPosts.allIds, byId: Object.assign({}, state.posts.byId, _newStateForPosts.byId), slugToId: Object.assign({}, state.posts.slugToId, _newStateForPosts.slugToId) }, topics: { allIds: _newStateForTopics.allIds, byId: Object.assign({}, state.topics.byId, _newStateForTopics.byId), slugToId: Object.assign({}, state.topics.slugToId, _newStateForTopics.slugToId) } }; } case _actionTypes["default"].singleBookmark["delete"].success: { var bookmarkId = _.get(action, 'payload.bookmarkID'); if (!bookmarkId) { return state; } var targetPost = _.find(_.get(state, 'posts.byId', {}), function (post) { return post.bookmarkId === bookmarkId; }); if (targetPost && targetPost.bookmarkId) { targetPost.bookmarkId = ''; } return state; } case _actionTypes["default"].singleBookmark.read.success: case _actionTypes["default"].singleBookmark.create.success: { var _bookmarkId = _.get(action, 'payload.data.record.id'); var slug = _.get(action, 'payload.data.record.slug'); if (!_bookmarkId || !slug) { return state; } var _targetPost = _.find(_.get(state, 'posts.byId', {}), function (post) { return post.slug === slug; }); if (_targetPost) { _targetPost.bookmarkId = _bookmarkId; } return state; } default: { return state; } } } var _default = exports["default"] = entities;