Skip to content

Commit

Permalink
chore: fix filter initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Feb 3, 2024
1 parent 8340e6b commit 374f3f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 1 addition & 4 deletions web/src/store/module/filter.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -9,9 +9,6 @@ export const useFilterStore = () => {
getState: () => {
return store.getState().filter;
},
setFilter: (filter: Filter) => {
store.dispatch(setFilter(filter));
},
clearFilter: () => {
store.dispatch(
setFilter({
Expand Down
16 changes: 15 additions & 1 deletion web/src/store/reducer/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<State>>) => {
if (JSON.stringify(action.payload) === state) {
Expand Down

0 comments on commit 374f3f7

Please sign in to comment.