From 1a3fcd98a50fcf7f04090f2a0400d9dce587d9ae Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 2 Oct 2020 03:26:39 +0200 Subject: [PATCH] feat: Save last filters as next default filters. Signed-off-by: Jorropo --- web/src/hooks/queryHooks.js | 2 +- web/src/store/GlobalStore.js | 4 +++- .../ui/components/FilterModal/FilterModal.js | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/web/src/hooks/queryHooks.js b/web/src/hooks/queryHooks.js index 639b1b76..04025755 100644 --- a/web/src/hooks/queryHooks.js +++ b/web/src/hooks/queryHooks.js @@ -76,7 +76,7 @@ export const useSetFiltersOnQueryChange = () => { const { search: locationSearch } = useLocation() useEffect(() => { const updateFilters = () => { - const { artifact_kinds, build_driver, build_state } = getFiltersFromUrlQuery({ locationSearch }) || {} + const { artifact_kinds, build_driver, build_state } = JSON.parse(window.localStorage.getItem("uiFilters")) || getFiltersFromUrlQuery({ locationSearch }) || {} dispatch({ type: actions.UPDATE_UI_FILTERS, payload: { artifact_kinds, build_driver, build_state }, diff --git a/web/src/store/GlobalStore.js b/web/src/store/GlobalStore.js index 2c437020..abc51272 100644 --- a/web/src/store/GlobalStore.js +++ b/web/src/store/GlobalStore.js @@ -35,7 +35,7 @@ export const INITIAL_STATE = { build_state: [], }, calculatedFilters: { - projects: [PROJECT.messenger], + projects: JSON.parse(window.localStorage.getItem("projects")) || [PROJECT.messenger], order: 'created_at', }, showingFilterModal: false, @@ -58,6 +58,8 @@ function reducer(state, action) { } } case actions.LOGOUT: + window.localStorage.removeItem("projects") + window.localStorage.removeItem("uiFilters") return { ...state, autoRefreshOn: false, diff --git a/web/src/ui/components/FilterModal/FilterModal.js b/web/src/ui/components/FilterModal/FilterModal.js index 86f8149b..adae0901 100644 --- a/web/src/ui/components/FilterModal/FilterModal.js +++ b/web/src/ui/components/FilterModal/FilterModal.js @@ -131,6 +131,7 @@ const FilterModal = ({ closeAction }) => { className="btn btn-primary" data-dismiss="modal" onClick={() => { + window.localStorage.setItem("projects", JSON.stringify([...selectedProjects])) updateState({ isLoaded: false, needsRefresh: true, @@ -139,16 +140,20 @@ const FilterModal = ({ closeAction }) => { }, }) closeAction() + const uiFilters = { + build_driver: selectedDrivers, + build_state: selectedBuildStates, + artifact_kinds: selectedArtifactKinds, + } + window.localStorage.setItem("uiFilters", JSON.stringify(uiFilters)) history.push({ path: '/', search: queryString .stringify( - _.pickBy({ - build_driver: selectedDrivers, - build_state: selectedBuildStates, - artifact_kinds: selectedArtifactKinds, - }, - (val) => getIsArrayWithN(val, 1)), + _.pickBy( + uiFilters, + (val) => getIsArrayWithN(val, 1), + ), ), }) }} @@ -163,8 +168,8 @@ const FilterModal = ({ closeAction }) => { { removeAuthCookie() - dispatch({ type: actions.LOGOUT }) dispatch({ type: actions.UPDATE_UI_FILTERS, payload: {} }) + dispatch({ type: actions.LOGOUT }) closeAction() redirectHome() }}