74 lines
3.1 KiB
TypeScript
74 lines
3.1 KiB
TypeScript
import type { FastifyInstance } from "fastify";
|
|
import { escapeHtml } from "../../html.ts";
|
|
import { PRODUCTS, type Product } from "./products.ts";
|
|
|
|
export interface CatalogOptions {
|
|
products?: readonly Product[];
|
|
}
|
|
|
|
const STYLES = `
|
|
:root{--bg:#f7f6f2;--fg:#1d1d1b;--muted:#5d5d58;--card:#fff;--line:#e2e0d8;--accent:#2f5d50}
|
|
@media (prefers-color-scheme:dark){:root{--bg:#141413;--fg:#ecebe6;--muted:#a3a29b;--card:#1e1e1c;--line:#33332f;--accent:#8cc1b0}}
|
|
*{box-sizing:border-box}
|
|
body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif}
|
|
main{max-width:960px;margin:0 auto;padding:48px 16px}
|
|
h1{font-size:2.25rem;margin:0 0 8px}
|
|
.lead{color:var(--muted);margin:0 0 32px;max-width:60ch}
|
|
.grid{display:grid;gap:16px;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));list-style:none;padding:0;margin:0}
|
|
.card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:20px}
|
|
.card h2{font-size:1.125rem;margin:0 0 8px}
|
|
.card p{margin:0 0 12px;color:var(--muted)}
|
|
.price{font-size:.875rem;color:var(--fg)}
|
|
.badge{display:inline-block;font-size:.75rem;padding:2px 8px;border-radius:999px;border:1px solid var(--accent);color:var(--accent);margin-left:8px;vertical-align:middle}
|
|
`;
|
|
|
|
function renderCard(p: Product): string {
|
|
const badge = p.status === "coming_soon" ? `<span class="badge">Скоро</span>` : "";
|
|
return `<li class="card" id="${escapeHtml(p.id)}">
|
|
<h2>${escapeHtml(p.title)}${badge}</h2>
|
|
<p>${escapeHtml(p.summary)}</p>
|
|
<div class="price">${escapeHtml(p.pricing)}</div>
|
|
</li>`;
|
|
}
|
|
|
|
export function renderLanding(products: readonly Product[]): string {
|
|
return `<!doctype html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Клиентология</title>
|
|
<link rel="stylesheet" href="/assets/site.css">
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<h1>Клиентология</h1>
|
|
<p class="lead">Инструменты для контакт-центров и отделов продаж по методологии «Клиентологии»: цифровые комплекты, автоматические аудиты, тренажёры и тестирование кандидатов.</p>
|
|
<ul class="grid">
|
|
${products.map(renderCard).join("\n")}
|
|
</ul>
|
|
</main>
|
|
</body>
|
|
</html>`;
|
|
}
|
|
|
|
export async function catalogModule(app: FastifyInstance, opts: CatalogOptions): Promise<void> {
|
|
const products = opts.products ?? PRODUCTS;
|
|
const byId = new Map(products.map((p) => [p.id, p]));
|
|
const landing = renderLanding(products);
|
|
|
|
app.get("/api/products", async () => products);
|
|
|
|
app.get<{ Params: { id: string } }>("/api/products/:id", async (req, reply) => {
|
|
const product = byId.get(req.params.id);
|
|
if (!product) return reply.code(404).send({ error: "not_found" });
|
|
return product;
|
|
});
|
|
|
|
app.get("/", async (_req, reply) => reply.type("text/html; charset=utf-8").send(landing));
|
|
|
|
app.get("/assets/site.css", async (_req, reply) =>
|
|
reply.type("text/css; charset=utf-8").header("cache-control", "public, max-age=3600").send(STYLES),
|
|
);
|
|
}
|