UNPKG

@twreporter/redux

Version:

redux actions and reducers for twreporter website

105 lines (101 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.searchedAuthorsList = exports.authorsList = void 0; var _actionTypes = _interopRequireDefault(require("../constants/action-types")); var _authorsList = require("../constants/authors-list"); var _assign = _interopRequireDefault(require("lodash/assign")); var _get = _interopRequireDefault(require("lodash/get")); var _uniq = _interopRequireDefault(require("lodash/uniq")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } // lodash var _ = { assign: _assign["default"], get: _get["default"], uniq: _uniq["default"] }; var initSearchedAuthorsListStates = { keywords: '', isFetching: false, currentPage: _authorsList.NUMBER_OF_FIRST_RESPONSE_PAGE - 1, items: [], error: null, lastUpdated: 0 }; /* Algolia set hitsPerPage limit up to 1000 items per search. So if number of authors grows over 1000, it will need to save hasMore in state to add loadmore function as case LIST_ALL_AUTHORS_SUCCESS. */ var searchedAuthorsList = exports.searchedAuthorsList = function searchedAuthorsList() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initSearchedAuthorsListStates; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var keywords = _.get(action, 'payload.keywords', ''); switch (action.type) { case _actionTypes["default"].SEARCH_AUTHORS_REQUEST: return _.assign({}, initSearchedAuthorsListStates, { keywords: keywords, isFetching: true }); case _actionTypes["default"].SEARCH_AUTHORS_SUCCESS: return _.assign({}, state, { keywords: keywords, isFetching: false, currentPage: _.get(action, 'payload.currentPage', _authorsList.NUMBER_OF_FIRST_RESPONSE_PAGE - 1), items: _.get(action, 'payload.normalizedData.result', []), error: null, lastUpdated: action.payload.receivedAt }); case _actionTypes["default"].SEARCH_AUTHORS_FAILURE: return _.assign({}, state, { keywords: keywords, isFetching: false, error: action.payload.error, lastUpdated: action.payload.failedAt }); default: return state; } }; var initAuthorsListStates = { isFetching: false, currentPage: _authorsList.NUMBER_OF_FIRST_RESPONSE_PAGE - 1, hasMore: false, items: [], error: null, lastUpdated: 0 }; var authorsList = exports.authorsList = function authorsList() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initAuthorsListStates; var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; switch (action.type) { case _actionTypes["default"].LIST_ALL_AUTHORS_REQUEST: return _.assign({}, state, { isFetching: true }); case _actionTypes["default"].LIST_ALL_AUTHORS_SUCCESS: { var currentPage = _.get(action, 'payload.currentPage', _authorsList.NUMBER_OF_FIRST_RESPONSE_PAGE - 1); var totalPages = _.get(action, 'payload.totalPages', 0); var previousAuthorsInList = _.get(state, 'items', []); var nextAuthorsInList = previousAuthorsInList.concat(_.get(action, 'payload.normalizedData.result', [])); // Concat array if list all return _.assign({}, state, { isFetching: false, currentPage: currentPage, hasMore: currentPage - _authorsList.NUMBER_OF_FIRST_RESPONSE_PAGE + 1 < totalPages, items: nextAuthorsInList, error: null, lastUpdated: action.payload.receivedAt }); } case _actionTypes["default"].LIST_ALL_AUTHORS_FAILURE: return _.assign({}, state, { isFetching: false, error: action.payload.error, lastUpdated: action.payload.failedAt }); default: return state; } };