web: add e500 error

web: refac prefix for lang url
This commit is contained in:
Dmitriy Pleshevskiy 2022-05-29 01:56:16 +03:00
parent 9912188db1
commit 2822a45dee
6 changed files with 46 additions and 20 deletions

View File

@ -1,6 +1,6 @@
import { AnyNode, Attrs, E, Elem } from "ren/node.ts";
import { classNames } from "ren/attrs.ts";
import { Context, iterLangs, Lang } from "../context.ts";
import { Context, getLangHref, iterLangs, Lang } from "../context.ts";
export function PageLayout(ctx: Context, children: AnyNode[]): Elem {
return E("div", { id: "main" }, [
@ -63,6 +63,5 @@ export function ChangeLang(ctx: Context): AnyNode {
}
export function ChangeLangBtn(ctx: Context, lang: Lang): AnyNode {
const prefix = lang === Lang.Rus ? "" : `/${lang}`;
return E("a", { "href": prefix + ctx.locPath }, lang);
return E("a", { "href": getLangHref(lang, ctx.locPath) }, lang);
}

View File

@ -6,6 +6,14 @@ export interface Context {
tr: Translations;
}
export function getLangHref(lang: Lang, url: string): string {
return getLangUrlPrefix(lang) + url;
}
export function getLangUrlPrefix(lang: Lang): string {
return lang === Lang.Rus ? "" : `/${lang}`;
}
export function iterLangs(): Lang[] {
return [Lang.Eng, Lang.Rus];
}

View File

@ -1,6 +1,6 @@
import { StrRenderer } from "ren/str.ts";
import { Layout } from "./comp/layout.ts";
import { Context, Lang } from "./context.ts";
import { Context, getLangHref, Lang } from "./context.ts";
import { E404Page } from "./views/e404.ts";
import * as log from "./log.ts";
import { HomePage } from "./views/home.ts";
@ -9,6 +9,7 @@ import { IngredientsPage } from "./views/ingredients.ts";
import { RestIngredientRepo } from "./repo/ingredient/rest.ts";
import rusTranslates from "./translates/rus.ts";
import type { Translations } from "./translates/rus.ts";
import { E500Page } from "./views/e500.ts";
if (import.meta.main) {
await main();
@ -66,29 +67,30 @@ async function handleGet(req: Request) {
wrapNode: Layout.bind(null, ctx),
onVisitAttr: ([key, value]) => {
if (key === "lhref") {
const prefix = ctx.lang === Lang.Rus || !value.startsWith("/")
? ""
: `/${ctx.lang}`;
return ["href", prefix + value];
return ["href", getLangHref(ctx.lang, value)];
} else {
return [key, value];
}
},
});
if (ctx.locPath === "/") {
return createHtmlResponse(ren.render(HomePage(ctx)));
} else if (ctx.locPath === "/recipes") {
return createHtmlResponse(ren.render(RecipesPage(ctx)));
} else if (ctx.locPath === "/ingredients") {
const repo = new RestIngredientRepo();
const ingredients = await repo.fetchIngredients(ctx.lang);
try {
if (ctx.locPath === "/") {
return createHtmlResponse(ren.render(HomePage(ctx)));
} else if (ctx.locPath === "/recipes") {
return createHtmlResponse(ren.render(RecipesPage(ctx)));
} else if (ctx.locPath === "/ingredients") {
const repo = new RestIngredientRepo();
const ingredients = await repo.fetchIngredients(ctx.lang);
return createHtmlResponse(
ren.render(IngredientsPage(ctx, { ingredients })),
);
} else {
return createHtmlResponse(ren.render(E404Page(ctx)), 404);
return createHtmlResponse(
ren.render(IngredientsPage(ctx, { ingredients })),
);
} else {
return createHtmlResponse(ren.render(E404Page(ctx)), 404);
}
} catch (_) {
return createHtmlResponse(ren.render(E500Page(ctx)), 500);
}
}
}

View File

@ -6,4 +6,5 @@ export default {
Ingredients: "Ingredients",
Source_code: "Source code",
Page_not_found: "Page not found",
Internal_server_error: "Internal server error",
} as Translations;

View File

@ -4,6 +4,7 @@ export const rus = {
Ingredients: "Ингредиенты",
Source_code: "Исходный код",
Page_not_found: "Страница не найдена",
Internal_server_error: "Внутренняя ошибка сервера",
};
export default rus;

15
web/views/e500.ts Normal file
View File

@ -0,0 +1,15 @@
import { PageLayout } from "../comp/page_layout.ts";
import { AnyNode, E } from "ren/node.ts";
import { classNames } from "ren/attrs.ts";
import { Context } from "../context.ts";
import { H3 } from "../uikit/typo.ts";
export function E500Page(ctx: Context): AnyNode {
return PageLayout(ctx, [E500(ctx)]);
}
export function E500(ctx: Context): AnyNode {
return E("div", classNames("content-width"), [
H3(ctx.tr.Internal_server_error),
]);
}