From 374f3f7d9684b8a692e9a82204fb6a9cc61b7984 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 3 Feb 2024 22:03:56 +0800 Subject: [PATCH] chore: fix filter initial state --- web/src/store/module/filter.ts | 5 +---- web/src/store/reducer/filter.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/web/src/store/module/filter.ts b/web/src/store/module/filter.ts index 0fc1847f2d267..e07d3ab091841 100644 --- a/web/src/store/module/filter.ts +++ b/web/src/store/module/filter.ts @@ -1,5 +1,5 @@ import store, { useAppSelector } from ".."; -import { Filter, setFilter } from "../reducer/filter"; +import { setFilter } from "../reducer/filter"; export const useFilterStore = () => { const state = useAppSelector((state) => state.filter); @@ -9,9 +9,6 @@ export const useFilterStore = () => { getState: () => { return store.getState().filter; }, - setFilter: (filter: Filter) => { - store.dispatch(setFilter(filter)); - }, clearFilter: () => { store.dispatch( setFilter({ diff --git a/web/src/store/reducer/filter.ts b/web/src/store/reducer/filter.ts index bb4ab3e9892bd..af8c2323877d9 100644 --- a/web/src/store/reducer/filter.ts +++ b/web/src/store/reducer/filter.ts @@ -8,9 +8,23 @@ interface State { export type Filter = State; +const getInitialState = (): State => { + const state: State = {}; + const urlParams = new URLSearchParams(location.search); + const tag = urlParams.get("tag"); + const text = urlParams.get("text"); + if (tag) { + state.tag = tag; + } + if (text) { + state.text = text; + } + return state; +}; + const filterSlice = createSlice({ name: "filter", - initialState: {} as State, + initialState: getInitialState(), reducers: { setFilter: (state, action: PayloadAction>) => { if (JSON.stringify(action.payload) === state) {