From cfb5161cd7ff7f6a7deb05e403e1217f101360c8 Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Wed, 4 May 2016 18:35:45 +0200 Subject: [PATCH] Store search state in URL --- client/app/scripts/actions/app-actions.js | 26 +++++++++++------------ client/app/scripts/components/search.js | 7 +++--- client/app/scripts/reducers/root.js | 3 +++ client/app/scripts/utils/router-utils.js | 4 +++- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/client/app/scripts/actions/app-actions.js b/client/app/scripts/actions/app-actions.js index 949393517..3b05f13b3 100644 --- a/client/app/scripts/actions/app-actions.js +++ b/client/app/scripts/actions/app-actions.js @@ -70,17 +70,13 @@ export function pinNextMetric(delta) { }; } -export function pinSearch(query) { - return { - type: ActionTypes.PIN_SEARCH, - query - }; -} - export function unpinSearch(query) { - return { - type: ActionTypes.UNPIN_SEARCH, - query + return (dispatch, getState) => { + dispatch({ + type: ActionTypes.UNPIN_SEARCH, + query + }); + updateRoute(getState); }; } @@ -285,9 +281,12 @@ export function doControl(nodeId, control) { } export function doSearch(searchQuery) { - return { - type: ActionTypes.DO_SEARCH, - searchQuery + return (dispatch, getState) => { + dispatch({ + type: ActionTypes.DO_SEARCH, + searchQuery + }); + updateRoute(getState); }; } @@ -327,6 +326,7 @@ export function hitEnter() { type: ActionTypes.PIN_SEARCH, query }); + updateRoute(getState); } } }; diff --git a/client/app/scripts/components/search.js b/client/app/scripts/components/search.js index f35aba549..832dd9352 100644 --- a/client/app/scripts/components/search.js +++ b/client/app/scripts/components/search.js @@ -74,15 +74,16 @@ class Search extends React.Component { render() { const { inputId = 'search', nodes, pinnedSearches, searchFocused, - searchNodeMatches, topologiesLoaded } = this.props; + searchNodeMatches, searchQuery, topologiesLoaded } = this.props; const disabled = this.props.isTopologyEmpty || !topologiesLoaded; const matchCount = searchNodeMatches .reduce((count, topologyMatches) => count + topologyMatches.size, 0); const showPinnedSearches = pinnedSearches.size > 0; + const value = this.state.value || searchQuery || ''; const classNames = cx('search', { 'search-pinned': showPinnedSearches, 'search-matched': matchCount, - 'search-filled': this.state.value, + 'search-filled': value, 'search-focused': searchFocused, 'search-disabled': disabled }); @@ -101,7 +102,7 @@ class Search extends React.Component { .map(query => )} } diff --git a/client/app/scripts/reducers/root.js b/client/app/scripts/reducers/root.js index 062e92e89..2a7eb1a71 100644 --- a/client/app/scripts/reducers/root.js +++ b/client/app/scripts/reducers/root.js @@ -401,6 +401,7 @@ export function rootReducer(state = initialState, action) { case ActionTypes.PIN_SEARCH: { state = state.set('searchQuery', ''); + state = updateNodeMatches(state); const pinnedSearches = state.get('pinnedSearches'); state = state.setIn(['pinnedSearches', pinnedSearches.size], action.query); return applyPinnedSearches(state); @@ -557,6 +558,8 @@ export function rootReducer(state = initialState, action) { case ActionTypes.ROUTE_TOPOLOGY: { state = state.set('routeSet', true); + state = state.set('pinnedSearches', makeList(action.state.pinnedSearches)); + state = state.set('searchQuery', action.state.searchQuery || ''); if (state.get('currentTopologyId') !== action.state.topologyId) { state = state.update('nodes', nodes => nodes.clear()); } diff --git a/client/app/scripts/utils/router-utils.js b/client/app/scripts/utils/router-utils.js index 85cfc7d1f..ffe42c164 100644 --- a/client/app/scripts/utils/router-utils.js +++ b/client/app/scripts/utils/router-utils.js @@ -35,8 +35,10 @@ export function getUrlState(state) { return { controlPipe: cp ? cp.toJS() : null, nodeDetails: nodeDetails.toJS(), - selectedNodeId: state.get('selectedNodeId'), pinnedMetricType: state.get('pinnedMetricType'), + pinnedSearches: state.get('pinnedSearches').toJS(), + searchQuery: state.get('searchQuery'), + selectedNodeId: state.get('selectedNodeId'), topologyId: state.get('currentTopologyId'), topologyOptions: state.get('topologyOptions').toJS() // all options };