Skip to content

Commit 084dc0d

Browse files
authored
refactor(page): Move urlParams to beforeShow (@fehmer) (monkeytypegame#6687)
1 parent b9bb113 commit 084dc0d

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

frontend/src/ts/controllers/page-controller.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as PageAccountSettings from "../pages/account-settings";
1515
import * as PageTransition from "../states/page-transition";
1616
import * as AdController from "../controllers/ad-controller";
1717
import * as Focus from "../test/focus";
18-
import { PageName, PageWithUrlParams } from "../pages/page";
18+
import { PageName } from "../pages/page";
1919

2020
type ChangeOptions = {
2121
force?: boolean;
@@ -111,9 +111,6 @@ export async function change(
111111

112112
await previousPage?.afterHide();
113113

114-
if (nextPage instanceof PageWithUrlParams) {
115-
nextPage.readUrlParams();
116-
}
117114
await nextPage?.beforeShow({
118115
params: options.params,
119116
// @ts-expect-error for the future (i think)

frontend/src/ts/pages/leaderboards.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,8 @@ function updateGetParameters(): void {
11361136
selectorLS.set(state);
11371137
}
11381138

1139-
function readGetParameters(params: UrlParameter | null): void {
1140-
if (params === null) {
1139+
function readGetParameters(params?: UrlParameter): void {
1140+
if (params === undefined) {
11411141
Object.assign(state, selectorLS.get());
11421142
return;
11431143
}
@@ -1271,17 +1271,16 @@ export const page = new PageWithUrlParams({
12711271
id: "leaderboards",
12721272
element: $(".page.pageLeaderboards"),
12731273
path: "/leaderboards",
1274-
urlParams: {
1275-
schema: UrlParameterSchema,
1276-
onUrlParamUpdate: readGetParameters,
1277-
},
1274+
urlParamsSchema: UrlParameterSchema,
1275+
12781276
afterHide: async (): Promise<void> => {
12791277
Skeleton.remove("pageLeaderboards");
12801278
stopTimer();
12811279
},
1282-
beforeShow: async (): Promise<void> => {
1280+
beforeShow: async (options): Promise<void> => {
12831281
Skeleton.append("pageLeaderboards", "main");
12841282
// await appendLanguageButtons(); //todo figure out this race condition
1283+
readGetParameters(options.urlParams);
12851284
startTimer();
12861285
updateTypeButtons();
12871286
updateTitle();

frontend/src/ts/pages/page.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class Page<T> {
4444

4545
public beforeHide: () => Promise<void>;
4646
public afterHide: () => Promise<void>;
47-
public beforeShow: (options: Options<T>) => Promise<void>;
47+
protected _beforeShow: (options: Options<T>) => Promise<void>;
4848
public afterShow: () => Promise<void>;
4949

5050
constructor(props: PageProperties<T>) {
@@ -54,36 +54,41 @@ export default class Page<T> {
5454
this.pathname = props.path;
5555
this.beforeHide = props.beforeHide ?? empty;
5656
this.afterHide = props.afterHide ?? empty;
57-
this.beforeShow = props.beforeShow ?? empty;
57+
this._beforeShow = props.beforeShow ?? empty;
5858
this.afterShow = props.afterShow ?? empty;
5959
}
60+
61+
public async beforeShow(options: Options<T>): Promise<void> {
62+
await this._beforeShow?.(options);
63+
}
6064
}
6165

66+
type OptionsWithUrlParams<T, U extends UrlParamsSchema> = Options<T> & {
67+
urlParams?: z.infer<U>;
68+
};
69+
6270
type UrlParamsSchema = z.ZodObject<Record<string, z.ZodTypeAny>>;
63-
type PagePropertiesWithUrlParams<
64-
T,
65-
U extends UrlParamsSchema
66-
> = PageProperties<T> & {
67-
urlParams: {
68-
schema: U;
69-
onUrlParamUpdate?: (params: z.infer<U> | null) => void;
70-
};
71+
type PagePropertiesWithUrlParams<T, U extends UrlParamsSchema> = Omit<
72+
PageProperties<T>,
73+
"beforeShow"
74+
> & {
75+
urlParamsSchema: U;
76+
beforeShow?: (options: OptionsWithUrlParams<T, U>) => Promise<void>;
7177
};
7278

7379
export class PageWithUrlParams<T, U extends UrlParamsSchema> extends Page<T> {
7480
private urlSchema: U;
75-
private onUrlParamUpdate?: (params: z.infer<U> | null) => void;
81+
protected override _beforeShow: (
82+
options: OptionsWithUrlParams<T, U>
83+
) => Promise<void>;
7684

7785
constructor(props: PagePropertiesWithUrlParams<T, U>) {
7886
super(props);
79-
this.urlSchema = props.urlParams.schema;
80-
this.onUrlParamUpdate = props.urlParams.onUrlParamUpdate;
87+
this.urlSchema = props.urlParamsSchema;
88+
this._beforeShow = props.beforeShow ?? empty;
8189
}
8290

83-
public readUrlParams(): void {
84-
if (this.onUrlParamUpdate === undefined) {
85-
return;
86-
}
91+
private readUrlParams(): z.infer<U> | undefined {
8792
const urlParams = new URLSearchParams(window.location.search);
8893

8994
const parsed = parseUrlSearchParams({
@@ -92,12 +97,11 @@ export class PageWithUrlParams<T, U extends UrlParamsSchema> extends Page<T> {
9297
});
9398

9499
if (!parsed.success) {
95-
this.onUrlParamUpdate?.(null);
96-
return;
100+
return undefined;
97101
}
98-
99-
this.onUrlParamUpdate?.(parsed.data);
102+
return parsed.data;
100103
}
104+
101105
public setUrlParams(params: z.infer<U>): void {
102106
const urlParams = serializeUrlSearchParams({
103107
schema: this.urlSchema,
@@ -106,4 +110,9 @@ export class PageWithUrlParams<T, U extends UrlParamsSchema> extends Page<T> {
106110
const newUrl = `${window.location.pathname}?${urlParams.toString()}`;
107111
window.history.replaceState({}, "", newUrl);
108112
}
113+
114+
public override async beforeShow(options: Options<T>): Promise<void> {
115+
const urlParams = this.readUrlParams();
116+
await this._beforeShow?.({ ...options, urlParams: urlParams });
117+
}
109118
}

0 commit comments

Comments
 (0)