Skip to content

Commit fa3bac3

Browse files
committed
refactor: move navigate options to the navigation event
also update the event params
1 parent 8932661 commit fa3bac3

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ import { isAuthAvailable, isAuthenticated } from "../firebase";
55
import { isFunboxActive } from "../test/funbox/list";
66
import * as TestState from "../test/test-state";
77
import * as Notifications from "../elements/notifications";
8-
import { LoadingOptions } from "../pages/page";
98
import * as NavigationEvent from "../observables/navigation-event";
109

1110
//source: https://www.youtube.com/watch?v=OstALBk-jTc
1211
// https://www.youtube.com/watch?v=OstALBk-jTc
1312

14-
//this will be used in tribe
15-
type NavigateOptions = {
16-
force?: boolean;
17-
empty?: boolean;
18-
data?: unknown;
19-
loadingOptions?: LoadingOptions;
20-
};
21-
2213
function pathToRegex(path: string): RegExp {
2314
return new RegExp(
2415
"^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$",
@@ -42,7 +33,7 @@ type Route = {
4233
path: string;
4334
load: (
4435
params: Record<string, string>,
45-
navigateOptions: NavigateOptions,
36+
navigateOptions: NavigationEvent.NavigateOptions,
4637
) => Promise<void>;
4738
};
4839

@@ -166,7 +157,7 @@ export async function navigate(
166157
url = window.location.pathname +
167158
window.location.search +
168159
window.location.hash,
169-
options = {} as NavigateOptions,
160+
options = {} as NavigationEvent.NavigateOptions,
170161
): Promise<void> {
171162
if (
172163
!options.force &&
@@ -211,7 +202,9 @@ export async function navigate(
211202
await router(options);
212203
}
213204

214-
async function router(options = {} as NavigateOptions): Promise<void> {
205+
async function router(
206+
options = {} as NavigationEvent.NavigateOptions,
207+
): Promise<void> {
215208
const matches = routes.map((r) => {
216209
return {
217210
route: r,
@@ -251,6 +244,6 @@ document.addEventListener("DOMContentLoaded", () => {
251244
});
252245
});
253246

254-
NavigationEvent.subscribe((it) => {
255-
void navigate(it.url, { data: it.data });
247+
NavigationEvent.subscribe((url, options) => {
248+
void navigate(url, options);
256249
});

frontend/src/ts/observables/navigation-event.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
type NavigationEvent = {
2-
url: string;
1+
import { LoadingOptions } from "../pages/page";
2+
3+
export type NavigateOptions = {
4+
force?: boolean;
5+
empty?: boolean;
6+
//this will be used in tribe
37
data?: unknown;
8+
loadingOptions?: LoadingOptions;
49
};
510

6-
type SubscribeFunction = (event: NavigationEvent) => void;
11+
type SubscribeFunction = (url: string, options: NavigateOptions) => void;
712

813
const subscribers: SubscribeFunction[] = [];
914

1015
export function subscribe(fn: SubscribeFunction): void {
1116
subscribers.push(fn);
1217
}
1318

14-
export function dispatch(event: NavigationEvent): void {
19+
export function dispatch(url: string, options: NavigateOptions): void {
1520
subscribers.forEach((fn) => {
1621
try {
17-
fn(event);
22+
fn(url, options);
1823
} catch (e) {
1924
console.error("Navigation event subscriber threw an error");
2025
console.error(e);

frontend/src/ts/pages/profile-search.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ export const page = new Page({
6464
$(".page.pageProfileSearch form").on("submit", (e) => {
6565
e.preventDefault();
6666
if (lastProfile === null) return;
67-
NavigationEvent.dispatch({
68-
url: `/profile/${lastProfile.name}`,
67+
NavigationEvent.dispatch(`/profile/${lastProfile.name}`, {
6968
data: lastProfile,
7069
});
7170
});

0 commit comments

Comments
 (0)