refac: update ren interface

This commit is contained in:
Dmitriy Pleshevskiy 2022-03-18 22:59:27 +03:00
parent 9146a6ead8
commit eb506f1f40
7 changed files with 39 additions and 50 deletions

View file

@ -1,21 +1,17 @@
import { AnyNode, Elem } from "ren";
export async function Layout(page: AnyNode): Promise<Elem> {
return new Elem("html")
.withAttr("lang", "ru")
.withChild(
new Elem("head").withChildren([
export function Layout(page: AnyNode): Elem {
return new Elem("html").withAttr("lang", "ru").withChildren(
new Elem("head").withChildren(
new Elem("meta").withAttr("charset", "utf-8"),
new Elem("link").withAttrs({
rel: "stylesheet",
href: "/static/styles.css",
}),
new Elem("title").withText("hello world"),
])
)
.withChild(
new Elem("body").withChild(
new Elem("div").withAttr("id", "root").withChild(page)
new Elem("title").withText("hello world")
),
new Elem("body").withChildren(
new Elem("div").withAttr("id", "root").withChildren(page)
)
);
}

View file

@ -1,24 +1,24 @@
import { AnyNode, Elem } from "ren";
export async function PageLayout(children: AnyNode[]): Promise<Elem> {
export function PageLayout(...children: AnyNode[]): Elem {
return new Elem("div")
.withAttr("id", "main")
.withChildren([
.withChildren(
Header(),
new Elem("div").withAttr("class", "content").withChildren(children),
Footer(),
]);
new Elem("div").withAttrs({ class: "content" }).withChildren(...children),
Footer()
);
}
export function Header(): Elem {
return new Elem("header").withChild(HeaderNav());
return new Elem("header").withChildren(HeaderNav());
}
export function HeaderNav(): Elem {
return new Elem("nav").withChildren([
return new Elem("nav").withChildren(
new Elem("a").withAttr("href", "/").withText("About"),
new Elem("a").withAttr("href", "/works").withText("Works"),
]);
new Elem("a").withAttr("href", "/works").withText("Works")
);
}
export function Footer(): Elem {

View file

@ -43,15 +43,15 @@ async function handleHttpReq(
if (/^[/](?:about[/]?)?$/.test(req.url)) {
httpRes
.writeHead(200, { "content-type": "text/html" })
.end(await ren.render(Layout(AboutPage())));
.end(ren.render(Layout(AboutPage())));
} else if (/^[/]works[/]?/.test(req.url)) {
httpRes
.writeHead(200, { "content-type": "text/html" })
.end(await ren.render(Layout(WorksPage())));
.end(ren.render(Layout(WorksPage())));
} else {
httpRes
.writeHead(404, { "content-type": "text/html" })
.end(await ren.render(Layout(E404())));
.end(ren.render(Layout(E404())));
}
}
} catch (err) {
@ -63,10 +63,7 @@ async function handleHttpReq(
}
}
const mimeTypeByExt = new Map([
[".html", "text/html"],
[".css", "text/css"],
]);
const mimeTypeByExt = new Map([[".css", "text/css"]]);
export function tryIntoAppServerRequest(
req: http.IncomingMessage

View file

@ -1,6 +1,6 @@
import { PageLayout } from "../components/page_layout.mjs";
import { AnyAsyncNode, Elem } from "ren";
import { AnyNode, Elem } from "ren";
export async function AboutPage(): AnyAsyncNode {
return PageLayout([new Elem("p").withText("Привет мир")]);
export function AboutPage(): AnyNode {
return PageLayout(new Elem("p").withText("Привет мир"));
}

View file

@ -1,6 +1,6 @@
import { PageLayout } from "../components/page_layout.mjs";
import { AnyAsyncNode, Elem } from "ren";
import { AnyNode, Elem } from "ren";
export async function E404(): AnyAsyncNode {
return PageLayout([new Elem("p").withText("Page not found")]);
export function E404(): AnyNode {
return PageLayout(new Elem("p").withText("Page not found"));
}

View file

@ -1,6 +1,6 @@
import { PageLayout } from "../components/page_layout.mjs";
import { AnyAsyncNode, Elem } from "ren";
import { AnyNode, Elem } from "ren";
export async function WorksPage(): AnyAsyncNode {
return PageLayout([new Elem("p").withText("Works")]);
export function WorksPage(): AnyNode {
return PageLayout(new Elem("p").withText("Works"));
}

View file

@ -5,27 +5,23 @@
}
html {
height: 100%;
background-color: #eee;
}
body {
html, body {
height: 100%;
}
#root {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: stretch;
}
#main {
#root, #main {
display: flex;
flex: 1 0;
flex-direction: column;
}
.content {
#main, .content {
flex: 1 0;
}