Skip to content

Commit 1402b3a

Browse files
committed
refactor: rework change page function
remove unnecessary wrapping promise use promiseAnimation for easier understanding
1 parent 2b4c3d2 commit 1402b3a

File tree

1 file changed

+81
-65
lines changed

1 file changed

+81
-65
lines changed

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

Lines changed: 81 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ function updateOpenGraphUrl(): void {
3939
}
4040
}
4141

42+
function updateTitle(nextPage: { id: string; display?: string }): void {
43+
if (nextPage.id === "test") {
44+
Misc.updateTitle();
45+
} else {
46+
const titleString =
47+
nextPage.display ?? Strings.capitalizeFirstLetterOfEachWord(nextPage.id);
48+
Misc.updateTitle(`${titleString} | Monkeytype`);
49+
}
50+
}
51+
4252
export async function change(
4353
pageName: PageName,
4454
options = {} as ChangeOptions
@@ -49,77 +59,83 @@ export async function change(
4959

5060
options = { ...defaultOptions, ...options };
5161

52-
return new Promise((resolve) => {
53-
if (PageTransition.get()) {
54-
console.debug(
55-
`change page to ${pageName} stopped, page transition is true`
56-
);
57-
resolve(false);
58-
return;
59-
}
62+
if (PageTransition.get()) {
63+
console.debug(
64+
`change page to ${pageName} stopped, page transition is true`
65+
);
66+
return false;
67+
}
6068

61-
if (!options.force && ActivePage.get() === pageName) {
62-
console.debug(`change page ${pageName} stoped, page already active`);
63-
resolve(false);
64-
return;
65-
} else {
66-
console.log(`changing page ${pageName}`);
67-
}
69+
if (!options.force && ActivePage.get() === pageName) {
70+
console.debug(`change page ${pageName} stoped, page already active`);
71+
return false;
72+
} else {
73+
console.log(`changing page ${pageName}`);
74+
}
6875

69-
const pages = {
70-
loading: PageLoading.page,
71-
test: PageTest.page,
72-
settings: Settings.page,
73-
about: PageAbout.page,
74-
account: Account.page,
75-
login: PageLogin.page,
76-
profile: PageProfile.page,
77-
profileSearch: PageProfileSearch.page,
78-
404: Page404.page,
79-
accountSettings: PageAccountSettings.page,
80-
leaderboards: PageLeaderboards.page,
81-
};
76+
const pages = {
77+
loading: PageLoading.page,
78+
test: PageTest.page,
79+
settings: Settings.page,
80+
about: PageAbout.page,
81+
account: Account.page,
82+
login: PageLogin.page,
83+
profile: PageProfile.page,
84+
profileSearch: PageProfileSearch.page,
85+
404: Page404.page,
86+
accountSettings: PageAccountSettings.page,
87+
leaderboards: PageLeaderboards.page,
88+
};
8289

83-
const previousPage = pages[ActivePage.get()];
84-
const nextPage = pages[pageName];
90+
const previousPage = pages[ActivePage.get()];
91+
const nextPage = pages[pageName];
92+
const totalDuration = Misc.applyReducedMotion(250);
93+
const easingMethod: Misc.JQueryEasing = "swing";
8594

86-
void previousPage?.beforeHide().then(() => {
87-
PageTransition.set(true);
88-
$(".page").removeClass("active");
89-
void Misc.swapElements(
90-
previousPage.element,
91-
nextPage.element,
92-
250,
93-
async () => {
94-
PageTransition.set(false);
95-
nextPage.element.addClass("active");
96-
resolve(true);
97-
await nextPage?.afterShow();
98-
void AdController.reinstate();
99-
},
100-
async () => {
101-
if (nextPage.id === "test") {
102-
Misc.updateTitle();
103-
} else {
104-
const titleString =
105-
nextPage.display ??
106-
Strings.capitalizeFirstLetterOfEachWord(nextPage.id);
107-
Misc.updateTitle(`${titleString} | Monkeytype`);
108-
}
109-
Focus.set(false);
110-
ActivePage.set(nextPage.id);
95+
//start
96+
PageTransition.set(true);
97+
$(".page").removeClass("active");
11198

112-
await previousPage?.afterHide();
99+
//previous page
100+
await previousPage?.beforeHide?.();
101+
previousPage.element.removeClass("hidden").css("opacity", 1);
102+
await Misc.promiseAnimation(
103+
previousPage.element,
104+
{
105+
opacity: "0",
106+
},
107+
totalDuration / 2,
108+
easingMethod
109+
);
110+
previousPage.element.addClass("hidden");
111+
await previousPage?.afterHide();
113112

114-
await nextPage?.beforeShow({
115-
params: options.params,
116-
// @ts-expect-error for the future (i think)
117-
data: options.data,
118-
});
113+
//between
114+
updateTitle(nextPage);
115+
Focus.set(false);
116+
ActivePage.set(nextPage.id);
117+
updateOpenGraphUrl();
119118

120-
updateOpenGraphUrl();
121-
}
122-
);
123-
});
119+
//next page
120+
await nextPage?.beforeShow({
121+
params: options.params,
122+
// @ts-expect-error for the future (i think)
123+
data: options.data,
124124
});
125+
nextPage.element.removeClass("hidden").css("opacity", 0);
126+
await Misc.promiseAnimation(
127+
nextPage.element,
128+
{
129+
opacity: "1",
130+
},
131+
totalDuration / 2,
132+
easingMethod
133+
);
134+
nextPage.element.addClass("active");
135+
await nextPage?.afterShow();
136+
137+
//wrapup
138+
PageTransition.set(false);
139+
void AdController.reinstate();
140+
return true;
125141
}

0 commit comments

Comments
 (0)