Skip to content

Commit cab0256

Browse files
committed
#777 frontend: Adjusted w/ league navigation validate search params
1 parent 3781cb9 commit cab0256

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/frontend/src/routes/_layout.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import useTurnstileValidation, {
55
hasCompletedCaptcha,
66
} from "../hooks/validation/turnstileValidation";
77

8-
import { useGraphInputStore } from "../store/GraphInputStore";
9-
108
const security_ip = localStorage.getItem("security_ip");
119

1210
export const Route = createFileRoute("/_layout")({
@@ -17,14 +15,6 @@ export const Route = createFileRoute("/_layout")({
1715
to: "/captcha",
1816
});
1917
}
20-
try {
21-
const searchParams = new URLSearchParams(location.hash.slice(1));
22-
useGraphInputStore.getState().getStoreFromHash(searchParams);
23-
} catch (err) {
24-
throw redirect({
25-
to: "/"
26-
})
27-
}
2818
},
2919
});
3020

src/frontend/src/routes/_layout/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ import { MainPage } from '../../components/Common/MainPage'
55
import { useGraphInputStore } from '../../store/GraphInputStore'
66
import { useGetGroupedModifiers } from '../../hooks/getData/prefetchGroupedModifiers'
77
import { useGetItemBaseTypes } from '../../hooks/getData/getBaseTypeCategories'
8+
import { validateAndSetSearchParams } from '../../utils'
89

910
export const Route = createFileRoute("/_layout/")({
1011
beforeLoad: async () => {
1112
const searchParams = new URLSearchParams(location.hash.slice(1))
12-
const state = useGraphInputStore.getState()
13-
if (searchParams.size > 0) {
14-
state.getStoreFromHash(searchParams)
15-
} else {
16-
// This happens when being redirected from login
17-
state.setHashFromStore()
18-
state.setStateHash(undefined)
19-
}
13+
validateAndSetSearchParams(searchParams)
2014
},
2115
component: Index,
2216
})

src/frontend/src/store/StateInterface.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export interface GraphInputState {
117117

118118
addLeague: (league: string) => void;
119119
removeLeague: (league: string) => void;
120+
removeAllLeagues: () => void;
120121

121122

122123
setItemName: (name: string | undefined) => void;

src/frontend/src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { ApiError } from "./client";
22
import { Toast } from "./hooks/useCustomToast";
3+
import { DEFAULT_LEAGUES } from "./config";
4+
import { useGraphInputStore } from "./store/GraphInputStore";
35

46
export const emailPattern = {
57
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
@@ -82,3 +84,27 @@ export const setupHourlyUpdate = (setCurrentTime: SetDateFunction) => {
8284

8385
return () => clearTimeout(timeoutId);
8486
};
87+
88+
const validateLeagues = (searchParams: URLSearchParams) => {
89+
const leagues = searchParams.get("league");
90+
if (searchParams.size === 1 && leagues) {
91+
if (leagues.length > 1 || !leagues.includes(DEFAULT_LEAGUES[0])) {
92+
throw "default league not in simple url";
93+
}
94+
} else if (!leagues || leagues.length === 0) {
95+
throw "leagues not set in url";
96+
}
97+
};
98+
99+
export const validateAndSetSearchParams = (searchParams: URLSearchParams) => {
100+
try {
101+
validateLeagues(searchParams);
102+
} catch (error) {
103+
const graphState = useGraphInputStore.getState();
104+
graphState.removeAllLeagues();
105+
graphState.addLeague(DEFAULT_LEAGUES[0]);
106+
const searchParams = new URLSearchParams();
107+
searchParams.set("league", DEFAULT_LEAGUES[0]);
108+
location.hash = searchParams.toString();
109+
}
110+
};

0 commit comments

Comments
 (0)