UNPKG

@collectionspace/cspace-public-browser

Version:
177 lines (175 loc) 5.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setDetailParams = exports.readDetail = exports.findInstitutionHoldings = exports.findAllInstitutionHoldings = exports.clearDetail = void 0; var _config = _interopRequireDefault(require("../config")); var _urlHelpers = require("../helpers/urlHelpers"); var _reducers = require("../reducers"); var _actionCodes = require("../constants/actionCodes"); var _esQueryHelpers = require("../helpers/esQueryHelpers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /* global fetch */ const clearDetail = () => ({ type: _actionCodes.CLEAR_DETAIL }); exports.clearDetail = clearDetail; const setDetailParams = (location, match) => { const params = (0, _urlHelpers.locationToDetailParams)(location, match); return { type: _actionCodes.SET_DETAIL_PARAMS, payload: params }; }; exports.setDetailParams = setDetailParams; const findInstitutionHoldings = institutionId => (dispatch, getState) => { const params = (0, _reducers.getDetailParams)(getState()); const data = (0, _reducers.getDetailData)(getState()); if (!params || !data || (0, _reducers.isDetailPending)(getState()) || (0, _reducers.isDetailInstSearchPending)(getState(), institutionId)) { return Promise.resolve(); } const holdingsConfig = _config.default.get('institutionHoldings') || {}; const { sortField, sortOrder, query: queryBuilder } = holdingsConfig; const institutionsConfig = _config.default.get('institutions') || {}; const institutionConfig = institutionsConfig[institutionId]; if (!queryBuilder || !institutionConfig) { return Promise.resolve(); } const { gatewayUrl } = institutionConfig; const url = `${gatewayUrl}/es/doc/_search`; const query = queryBuilder(data); const payload = { query, from: 0, size: 500 }; if (sortField) { payload.sort = { [sortField]: { order: sortOrder || 'asc' } }; } dispatch({ type: _actionCodes.INST_SEARCH_STARTED, meta: { institutionId, params } }); return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }).then(response => { if (!response.ok) { const error = new Error(); error.response = response; return Promise.reject(error); } return response.json(); }).then(responseData => { dispatch({ type: _actionCodes.INST_SEARCH_FULFILLED, payload: responseData, meta: { institutionId, params } }); }).catch(error => { dispatch({ type: _actionCodes.INST_SEARCH_REJECTED, payload: error, meta: { institutionId, params } }); }); }; exports.findInstitutionHoldings = findInstitutionHoldings; const findAllInstitutionHoldings = () => dispatch => { const institutionsConfig = _config.default.get('institutions'); if (!institutionsConfig) { return Promise.resolve(); } return Promise.all(Object.keys(institutionsConfig).map(institutionId => dispatch(findInstitutionHoldings(institutionId)))); }; exports.findAllInstitutionHoldings = findAllInstitutionHoldings; const readDetail = () => (dispatch, getState) => { const params = (0, _reducers.getDetailParams)(getState()); if (!params || (0, _reducers.isDetailPending)(getState())) { return Promise.resolve(); } const gatewayUrl = _config.default.get('gatewayUrl'); const url = `${gatewayUrl}/es/doc/_msearch`; const csid = params.get('csid'); const index = params.get('index'); const searchParams = params.get('searchParams'); const detailPayload = { query: { term: { 'ecm:name': csid } }, from: 0, size: 1 }; const bodyParts = [JSON.stringify({ preference: 'detail' }), JSON.stringify(detailPayload)]; if (searchParams && typeof index !== 'undefined') { const adjacentResultsPayload = (0, _esQueryHelpers.getSearchResultPayload)(searchParams, 3, Math.max(0, index - 1)); bodyParts.push(JSON.stringify({ preference: 'adjacent' })); bodyParts.push(JSON.stringify(adjacentResultsPayload)); } dispatch({ type: _actionCodes.DETAIL_READ_STARTED, meta: { params } }); return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/x-ndjson' }, body: bodyParts.join('\n') }).then(response => { if (!response.ok) { const error = new Error(); error.response = response; return Promise.reject(error); } return response.json(); }).then(data => { dispatch({ type: _actionCodes.DETAIL_READ_FULFILLED, payload: data, meta: { params } }); dispatch(findAllInstitutionHoldings()); }).catch(error => { dispatch({ type: _actionCodes.DETAIL_READ_REJECTED, payload: error, meta: { params } }); }); }; exports.readDetail = readDetail;