UNPKG

cspace-ui

Version:
258 lines (211 loc) 5.8 kB
/* global window */ import Immutable from 'immutable'; import get from 'lodash/get'; import { getStickyFieldValues } from '../helpers/recordDataHelpers'; import { getUserUsername, getPrefs, getRecordData, getSearchPageRecordType, getSearchPageVocabulary, getSearchPageKeyword, getSearchPageAdvancedLimitBy, getSearchPageAdvancedSearchTerms, } from '../reducers'; import { PREFS_LOADED, COLLAPSE_PANEL, SET_ADMIN_TAB, SET_TOOL_TAB, SET_RECORD_BROWSER_NAV_BAR_ITEMS, SET_SEARCH_PAGE_RECORD_TYPE, SET_SEARCH_PAGE_VOCABULARY, SET_QUICK_SEARCH_RECORD_TYPE, SET_QUICK_SEARCH_VOCABULARY, SET_SEARCH_PANEL_PAGE_SIZE, SET_SEARCH_RESULT_PAGE_PAGE_SIZE, SET_SEARCH_RESULT_PAGE_VIEW, SET_SEARCH_TO_SELECT_PAGE_SIZE, SET_FORM, SET_UPLOAD_TYPE, TOGGLE_RECORD_SIDEBAR, TOGGLE_SEARCH_RESULT_SIDEBAR, SET_STICKY_FIELDS, TOGGLE_USE_NEW_SEARCH, SET_NEW_SEARCH_SHOWN, PIN_QUERY, DELETE_PINNED_QUERY, } from '../constants/actionCodes'; export const storageKey = 'cspace-ui'; export const collapsePanel = (recordType, name, collapsed) => ({ type: COLLAPSE_PANEL, payload: collapsed, meta: { recordType, name, }, }); export const setAdminTab = (tabName) => ({ type: SET_ADMIN_TAB, payload: tabName, }); export const setToolTab = (tabName) => ({ type: SET_TOOL_TAB, payload: tabName, }); export const setRecordBrowserNavBarItems = (recordType, navBarItems) => ({ type: SET_RECORD_BROWSER_NAV_BAR_ITEMS, payload: navBarItems, meta: { recordType, }, }); export const setSearchPageRecordType = (value) => (dispatch, getState) => { if (value !== getSearchPageRecordType(getState())) { dispatch({ type: SET_SEARCH_PAGE_RECORD_TYPE, payload: value, }); } }; export const setSearchPageVocabulary = (value) => ({ type: SET_SEARCH_PAGE_VOCABULARY, payload: value, }); export const setQuickSearchRecordType = (value) => ({ type: SET_QUICK_SEARCH_RECORD_TYPE, payload: value, }); export const setQuickSearchVocabulary = (value) => ({ type: SET_QUICK_SEARCH_VOCABULARY, payload: value, }); export const setSearchPanelPageSize = (recordType, name, pageSize) => ({ type: SET_SEARCH_PANEL_PAGE_SIZE, payload: pageSize, meta: { recordType, name, }, }); export const setSearchResultPagePageSize = (pageSize) => ({ type: SET_SEARCH_RESULT_PAGE_PAGE_SIZE, payload: pageSize, }); export const setSearchResultPageView = (view) => ({ type: SET_SEARCH_RESULT_PAGE_VIEW, payload: view, }); export const setSearchToSelectPageSize = (pageSize) => ({ type: SET_SEARCH_TO_SELECT_PAGE_SIZE, payload: pageSize, }); export const setForm = (recordType, formName) => ({ type: SET_FORM, payload: formName, meta: { recordType, }, }); export const setUploadType = (type) => ({ type: SET_UPLOAD_TYPE, payload: type, }); export const toggleRecordSidebar = () => ({ type: TOGGLE_RECORD_SIDEBAR, }); export const toggleSearchResultSidebar = () => ({ type: TOGGLE_SEARCH_RESULT_SIDEBAR, }); export const setStickyFields = (recordTypeConfig, csid) => (dispatch, getState) => { const data = getRecordData(getState(), csid); if (data) { const stickyData = getStickyFieldValues(recordTypeConfig, data); dispatch({ type: SET_STICKY_FIELDS, payload: stickyData, meta: { recordTypeConfig, }, }); } }; export const toggleUseNewSearch = () => ({ type: TOGGLE_USE_NEW_SEARCH, }); export const setNewSearchShown = () => ({ type: SET_NEW_SEARCH_SHOWN, }); export const loadPrefs = (config, username) => (dispatch) => { // TODO: Load prefs from server (requires adding services layer support). // For now, just load from local storage. let userPrefs = null; if (username) { const serializedPrefs = window.localStorage.getItem(storageKey); if (serializedPrefs) { try { userPrefs = Immutable.fromJS((JSON.parse(serializedPrefs))[username]); } catch (error) { userPrefs = null; } } } const defaultUserPrefs = get(config, 'defaultUserPrefs'); if (defaultUserPrefs) { userPrefs = (Immutable.fromJS(defaultUserPrefs)).mergeDeep(userPrefs); } dispatch({ type: PREFS_LOADED, payload: userPrefs, }); }; export const savePrefs = () => (dispatch, getState) => { // TODO: Save prefs to server (requires adding services layer support). // For now, just save to local storage. const username = getUserUsername(getState()); let prefs; if (username) { const serializedPrefs = window.localStorage.getItem(storageKey); if (serializedPrefs) { try { prefs = JSON.parse(serializedPrefs); } catch (error) { prefs = null; } } if (!prefs) { prefs = {}; } prefs[username] = getPrefs(getState()); window.localStorage.setItem(storageKey, JSON.stringify(prefs)); } }; const createPinnedQueryId = () => ( `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` ); export const pinQuery = (name, description) => (dispatch, getState) => { const state = getState(); const recordType = getSearchPageRecordType(state); dispatch({ type: PIN_QUERY, payload: Immutable.Map({ id: createPinnedQueryId(), name: name.trim(), description, recordType, vocabulary: getSearchPageVocabulary(state, recordType), keyword: getSearchPageKeyword(state), advancedLimitBy: getSearchPageAdvancedLimitBy(state), advancedSearchTerms: getSearchPageAdvancedSearchTerms(state), createdTime: (new Date()).toISOString(), }), }); dispatch(savePrefs()); }; export const deletePinnedQuery = (id) => (dispatch) => { dispatch({ type: DELETE_PINNED_QUERY, payload: id, }); dispatch(savePrefs()); };