UNPKG

@collectionspace/cspace-public-browser

Version:
143 lines (142 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPending = exports.isInstSearchPending = exports.getParams = exports.getInstitutionHits = exports.getHoldingInstitutions = exports.getError = exports.getData = exports.getAdjacents = exports.default = void 0; var _immutable = _interopRequireDefault(require("immutable")); var _get = _interopRequireDefault(require("lodash/get")); var _actionCodes = require("../constants/actionCodes"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const findAdjacents = (csid, hits) => { // eslint-disable-next-line no-underscore-dangle const csidIndex = hits ? hits.findIndex(hit => hit._source['ecm:name'] === csid) : -1; if (csidIndex < 0) { return { prev: undefined, next: undefined }; } return { // eslint-disable-next-line no-underscore-dangle prev: csidIndex > 0 ? hits[csidIndex - 1]._source : undefined, // eslint-disable-next-line no-underscore-dangle next: csidIndex < hits.length - 1 ? hits[csidIndex + 1]._source : undefined }; }; const handleDetailReadStarted = (state, action) => { const { params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } return state.set('pending', true).delete('error'); }; const handleDetailReadFulfilled = (state, action) => { const { params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } const response = action.payload; const data = (0, _get.default)(response, ['responses', 0, 'hits', 'hits', 0, '_source']); const adjacents = findAdjacents(readParams.get('csid'), (0, _get.default)(response, ['responses', 1, 'hits', 'hits'])); return state.set('adjacents', adjacents).delete('pending').set('data', data).delete('error'); }; const handleDetailReadRejected = (state, action) => { const { params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } return state.delete('pending').set('error', action.payload); }; const handleInstSearchStarted = (state, action) => { const { institutionId, params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } return state.setIn(['inst', institutionId, 'pending'], true).deleteIn(['inst', institutionId, 'error']); }; const handleInstSearchFulfilled = (state, action) => { const { institutionId, params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } const response = action.payload; const hits = (0, _get.default)(response, ['hits', 'hits']); return state.deleteIn(['inst', institutionId, 'pending']).setIn(['inst', institutionId, 'hits'], hits).deleteIn(['inst', institutionId, 'error']); }; const handleInstSearchRejected = (state, action) => { const { institutionId, params: readParams } = action.meta; const currentParams = state.get('params'); if (readParams !== currentParams) { return state; } return state.deleteIn(['inst', institutionId, 'pending']).setIn(['inst', institutionId, 'error'], action.payload); }; var _default = (state = _immutable.default.Map(), action) => { switch (action.type) { case _actionCodes.CLEAR_DETAIL: return state.clear(); case _actionCodes.DETAIL_READ_STARTED: return handleDetailReadStarted(state, action); case _actionCodes.DETAIL_READ_FULFILLED: return handleDetailReadFulfilled(state, action); case _actionCodes.DETAIL_READ_REJECTED: return handleDetailReadRejected(state, action); case _actionCodes.INST_SEARCH_STARTED: return handleInstSearchStarted(state, action); case _actionCodes.INST_SEARCH_FULFILLED: return handleInstSearchFulfilled(state, action); case _actionCodes.INST_SEARCH_REJECTED: return handleInstSearchRejected(state, action); case _actionCodes.SET_DETAIL_PARAMS: return state.set('params', action.payload); default: return state; } }; exports.default = _default; const getAdjacents = state => state.get('adjacents'); exports.getAdjacents = getAdjacents; const getError = state => state.get('error'); exports.getError = getError; const getParams = state => state.get('params'); exports.getParams = getParams; const getData = state => state.get('data'); exports.getData = getData; const isPending = state => !!state.get('pending'); exports.isPending = isPending; const getHoldingInstitutions = state => { const inst = state.get('inst'); if (!inst) { return _immutable.default.Set(); } const institutionIds = inst.keySeq().filter(institutionId => { const hits = inst.getIn([institutionId, 'hits']); return hits && hits.length > 0; }); return _immutable.default.Set(institutionIds); }; exports.getHoldingInstitutions = getHoldingInstitutions; const getInstitutionHits = (state, institutionId) => state.getIn(['inst', institutionId, 'hits']); exports.getInstitutionHits = getInstitutionHits; const isInstSearchPending = (state, institutionId) => state.getIn(['inst', institutionId, 'pending']); exports.isInstSearchPending = isInstSearchPending;