From a90acdabb382923d2db2110505bd374394e08976 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Sun, 13 Nov 2022 19:40:16 +0800 Subject: [PATCH] fix: route confusion entering from non-home page (#460) Co-authored-by: boojack --- web/src/services/locationService.ts | 5 ++++- web/src/store/modules/location.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/web/src/services/locationService.ts b/web/src/services/locationService.ts index 2a64fc4..65267c4 100644 --- a/web/src/services/locationService.ts +++ b/web/src/services/locationService.ts @@ -1,8 +1,11 @@ import { stringify } from "qs"; import store from "../store"; -import { setQuery, setPathname, Query, updateStateWithLocation } from "../store/modules/location"; +import { setQuery, setPathname, Query, updateStateWithLocation, updatePathnameStateWithLocation } from "../store/modules/location"; const updateLocationUrl = (method: "replace" | "push" = "replace") => { + // avoid pathname confusion when entering from non-home page + store.dispatch(updatePathnameStateWithLocation()); + const { query, pathname, hash } = store.getState().location; let queryString = stringify(query); if (queryString) { diff --git a/web/src/store/modules/location.ts b/web/src/store/modules/location.ts index dabd4dd..1080141 100644 --- a/web/src/store/modules/location.ts +++ b/web/src/store/modules/location.ts @@ -64,6 +64,13 @@ const locationSlice = createSlice({ updateStateWithLocation: () => { return getStateFromLocation(); }, + updatePathnameStateWithLocation: (state) => { + const { pathname } = window.location; + return { + ...state, + pathname: getValidPathname(pathname), + }; + }, setPathname: (state, action: PayloadAction) => { if (state.pathname === action.payload) { return state; @@ -90,6 +97,6 @@ const locationSlice = createSlice({ }, }); -export const { setPathname, setQuery, updateStateWithLocation } = locationSlice.actions; +export const { setPathname, setQuery, updateStateWithLocation, updatePathnameStateWithLocation } = locationSlice.actions; export default locationSlice.reducer;