-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.ts
59 lines (52 loc) · 1.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Flow } from "@vaadin/flow-frontend/Flow";
import { Route, Router } from "@vaadin/router";
import { tsRecipeRoutes } from "./ts-routes";
import RecipeInfo from "./generated/com/vaadin/recipes/data/RecipeInfo";
import * as RecipeEndpoint from "./generated/RecipeEndpoint";
import "./views/recipes-list-view";
import "./views/recipe-view";
const { serverSideRoutes } = new Flow({
imports: () => import("Frontend/generated/flow/generated-flow-imports.js"),
});
const routes: Route[] = [
{
path: "/",
component: "recipes-list-view",
action: async (_context, _commands) => {
await initRecipes();
},
},
{
path: "",
component: "recipe-view",
action: async (_context, _commands) => {
await initRecipes();
},
children: [
...tsRecipeRoutes,
...serverSideRoutes, // IMPORTANT: this must be the last entry in the array
],
},
];
export const router = new Router(document.querySelector("#outlet"));
router.setRoutes(routes);
export const recipes: RecipeInfo[] = [];
async function initRecipes() {
if (recipes.length === 0) {
recipes.push(...tsRecipeRoutes.map((route) => route.info));
recipes.push(...(await RecipeEndpoint.list()));
recipes.sort((a, b) =>
a.howDoI < b.howDoI ? -1 : a.howDoI == b.howDoI ? 0 : 1
);
}
}
function sendPageview() {
if (location.hostname !== "localhost" && location.hostname !== "127.0.0.1") {
// Let vaadin.com HaaS know that the page has changed
window.dispatchEvent(new Event("on-location-change"));
}
}
window.addEventListener(
"vaadin-router-location-changed",
sendPageview as EventListener
);