UNPKG

cspace-ui

Version:
129 lines (128 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setSearchPageKeyword = exports.setSearchPageAdvancedSearchTerms = exports.setSearchPageAdvancedLimitBy = exports.setSearchPageAdvanced = exports.initiateSearch = exports.clearSearchPage = exports.buildAdvancedSearchCondition = void 0; var _get = _interopRequireDefault(require("lodash/get")); var _qs = _interopRequireDefault(require("qs")); var _immutable = _interopRequireDefault(require("immutable")); var _reducers = require("../reducers"); var _searchHelpers = require("../helpers/searchHelpers"); var _actionCodes = require("../constants/actionCodes"); var _searchOperators = require("../constants/searchOperators"); var _searchNames = require("../constants/searchNames"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const clearSearchPage = () => ({ type: _actionCodes.CLEAR_SEARCH_PAGE }); exports.clearSearchPage = clearSearchPage; const setSearchPageKeyword = value => ({ type: _actionCodes.SET_SEARCH_PAGE_KEYWORD, payload: value }); exports.setSearchPageKeyword = setSearchPageKeyword; const setSearchPageAdvanced = condition => (dispatch, getState) => { const recordType = (0, _reducers.getSearchPageRecordType)(getState()); dispatch({ type: _actionCodes.SET_SEARCH_PAGE_ADVANCED, payload: condition, meta: { recordType } }); }; exports.setSearchPageAdvanced = setSearchPageAdvanced; const setSearchPageAdvancedLimitBy = condition => (dispatch, getState) => { const recordType = (0, _reducers.getSearchPageRecordType)(getState()); dispatch({ type: _actionCodes.SET_SEARCH_PAGE_ADVANCED_LIMIT_BY, payload: condition, meta: { searchTermsGroup: _searchNames.SEARCH_TERMS_GROUP_LIMIT_BY, recordType } }); }; exports.setSearchPageAdvancedLimitBy = setSearchPageAdvancedLimitBy; const setSearchPageAdvancedSearchTerms = condition => (dispatch, getState) => { const recordType = (0, _reducers.getSearchPageRecordType)(getState()); dispatch({ type: _actionCodes.SET_SEARCH_PAGE_ADVANCED_SEARCH_TERMS, payload: condition, meta: { searchTermsGroup: _searchNames.SEARCH_TERMS_GROUP_SEARCH_TERMS, recordType } }); }; /** * Builds the advanced search condition, conditionally. * @param {boolean} useNewSearch whether new search is being used * @param {Map} advancedLimitBy the condition of the limit by panel * @param {Map} advancedSearchTerms the condition of the searchTerms panel * @param {Map} advanced the condition of the classic search form * @returns {Map} the built advanced search condition; */ exports.setSearchPageAdvancedSearchTerms = setSearchPageAdvancedSearchTerms; const buildAdvancedSearchCondition = (useNewSearch, advancedLimitBy, advancedSearchTerms, advanced) => { // When using new search if (useNewSearch || typeof useNewSearch === 'undefined') { // and only search terms panel is populated, if (advancedLimitBy == null) { // then it returns the advancedSearchTerms condition return advancedSearchTerms; } // and both search panels are populated, // then it combines advancedLimitBy and advancedSearchTerms using AND operator. return _immutable.default.Map({ op: _searchOperators.OP_AND, value: _immutable.default.List.of(advancedSearchTerms, advancedLimitBy) }); } // When not using new search, it returns the advanced condition. return advanced; }; exports.buildAdvancedSearchCondition = buildAdvancedSearchCondition; const initiateSearch = (config, push) => (dispatch, getState) => { const state = getState(); const recordType = (0, _reducers.getSearchPageRecordType)(state); const vocabulary = (0, _reducers.getSearchPageVocabulary)(state, recordType); const keyword = (0, _reducers.getSearchPageKeyword)(state); const useNewSearch = (0, _reducers.getUseNewSearch)(state); const query = {}; const vocabularyPath = vocabulary ? `/${vocabulary}` : ''; const pathname = `/list/${recordType}${vocabularyPath}`; const kw = keyword ? keyword.trim() : ''; if (kw) { query.kw = kw; } const advancedSearchTerms = (0, _reducers.getSearchPageAdvancedSearchTerms)(state); const advancedLimitBy = (0, _reducers.getSearchPageAdvancedLimitBy)(state); const advanced = (0, _reducers.getSearchPageAdvanced)(state); const advancedSearchCondition = buildAdvancedSearchCondition(useNewSearch, advancedLimitBy, advancedSearchTerms, advanced); const fields = (0, _get.default)(config, ['recordTypes', recordType, 'fields']); const condition = (0, _searchHelpers.normalizeCondition)(fields, advancedSearchCondition); if (condition) { query.as = JSON.stringify(condition.toJS()); } const queryString = _qs.default.stringify(query); push({ pathname, search: `?${queryString}`, state: { originSearchPage: { // Keep the search descriptor a plain object, not an Immutable, so that it can be stored in // location state. searchDescriptor: { recordType, vocabulary, searchQuery: { as: advancedSearchCondition && advancedSearchCondition.toJS(), kw: keyword } } } } }); }; exports.initiateSearch = initiateSearch;