Configurator->POST /api/orders, deposit fetches order + PATCH in_production, header nav reaches the new pages (no orphans). New pages: katalog, katalog/[id], kallor, portal, shared states. tsc 0, build green (13 routes). Co-Authored-By: Claude Opus 4.8 <[email protected]>
351 lines
14 KiB
TypeScript
351 lines
14 KiB
TypeScript
"use client";
|
||
|
||
import { useRouter } from "next/navigation";
|
||
import { useMemo, useState, useEffect } from "react";
|
||
import { OrderStepper } from "@/components/order-stepper";
|
||
import { ProvenanceBadge } from "@/components/provenance-badge";
|
||
import {
|
||
MARKETS,
|
||
ENTITY_TYPES,
|
||
FIELDS,
|
||
ENRICHMENTS,
|
||
FREQUENCIES,
|
||
DELIVERY_FORMATS,
|
||
computeEstimate,
|
||
formatSEK,
|
||
type EntityType,
|
||
type Frequency,
|
||
type ProvLevel,
|
||
} from "@/lib/data";
|
||
|
||
function confidenceColor(v: number) {
|
||
if (v >= 92) return "var(--prov-3)";
|
||
if (v >= 87) return "var(--prov-2)";
|
||
return "var(--prov-1)";
|
||
}
|
||
|
||
export default function ConfiguratorPage() {
|
||
const [markets, setMarkets] = useState<string[]>(["SE", "NO", "DK", "FI"]);
|
||
const [type, setType] = useState<EntityType>("Bolag");
|
||
const [fieldIds, setFieldIds] = useState<string[]>(["lei", "financials", "contact"]);
|
||
const [enrichmentIds, setEnrichmentIds] = useState<string[]>(["nace"]);
|
||
const [volume, setVolume] = useState(5000);
|
||
const [frequency, setFrequency] = useState<Frequency>("Engångs");
|
||
const [confidence, setConfidence] = useState(92);
|
||
const [formats, setFormats] = useState<string[]>(["CSV", "Tabellvy (NocoDB)"]);
|
||
|
||
const est = useMemo(
|
||
() => computeEstimate({ markets, type, fieldIds, enrichmentIds, volume, frequency, confidence }),
|
||
[markets, type, fieldIds, enrichmentIds, volume, frequency, confidence],
|
||
);
|
||
|
||
// Persist so the deposit screen shows the same numbers.
|
||
useEffect(() => {
|
||
try {
|
||
sessionStorage.setItem(
|
||
"0pt-order",
|
||
JSON.stringify({ markets, type, fieldIds, enrichmentIds, volume, frequency, confidence, formats, est }),
|
||
);
|
||
} catch {}
|
||
}, [markets, type, fieldIds, enrichmentIds, volume, frequency, confidence, formats, est]);
|
||
|
||
const router = useRouter();
|
||
const [creating, setCreating] = useState(false);
|
||
async function submit() {
|
||
if (creating) return;
|
||
setCreating(true);
|
||
const config = { markets, type, fieldIds, enrichmentIds, volume, frequency, confidence, formats };
|
||
try {
|
||
const res = await fetch("/api/orders", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ flow: frequency === "Engångs" ? "custom" : "subscription", config }),
|
||
});
|
||
const data = await res.json();
|
||
if (data?.order_id) {
|
||
router.push(`/bestall/forskott?order=${encodeURIComponent(data.order_id)}`);
|
||
return;
|
||
}
|
||
} catch {}
|
||
setCreating(false);
|
||
router.push("/bestall/forskott");
|
||
}
|
||
|
||
const toggle = (arr: string[], v: string, set: (x: string[]) => void) =>
|
||
set(arr.includes(v) ? arr.filter((x) => x !== v) : [...arr, v]);
|
||
|
||
const Chip = ({
|
||
active,
|
||
disabled,
|
||
onClick,
|
||
children,
|
||
}: {
|
||
active?: boolean;
|
||
disabled?: boolean;
|
||
onClick?: () => void;
|
||
children: React.ReactNode;
|
||
}) => (
|
||
<button
|
||
type="button"
|
||
disabled={disabled}
|
||
onClick={onClick}
|
||
className="border px-3 py-2 font-mono text-[11px] uppercase tracking-[0.08em] transition-colors disabled:cursor-not-allowed disabled:opacity-35"
|
||
style={{
|
||
borderColor: active ? "var(--foreground)" : "var(--border)",
|
||
backgroundColor: active ? "var(--foreground)" : "transparent",
|
||
color: active ? "var(--background)" : "var(--foreground)",
|
||
}}
|
||
>
|
||
{children}
|
||
</button>
|
||
);
|
||
|
||
const Block = ({ index, label, children }: { index: string; label: string; children: React.ReactNode }) => (
|
||
<div className="group border border-border transition-colors hover:border-foreground">
|
||
<div className="flex items-center gap-3 border-b border-border px-4 py-2.5">
|
||
<span className="font-mono text-[11px] tabular-nums text-muted-foreground transition-colors group-hover:text-foreground">
|
||
{index}
|
||
</span>
|
||
<span className="h-3 w-px bg-border" />
|
||
<span className="font-mono text-[11px] uppercase tracking-[0.12em] text-foreground">{label}</span>
|
||
</div>
|
||
<div className="p-4">{children}</div>
|
||
</div>
|
||
);
|
||
|
||
return (
|
||
<main className="min-h-screen bg-background text-foreground">
|
||
<OrderStepper active={1} />
|
||
|
||
<div className="mx-auto max-w-[80rem] px-6 py-16">
|
||
{/* Editorial masthead — oversized index as graphic texture */}
|
||
<div className="reveal relative border-b border-border pb-10">
|
||
<span
|
||
aria-hidden
|
||
className="pointer-events-none absolute -top-6 right-0 select-none font-mono text-[7rem] leading-none tracking-[-0.04em] text-muted-foreground/10 md:text-[10rem]"
|
||
>
|
||
01
|
||
</span>
|
||
<p className="font-mono text-[11px] uppercase tracking-[0.18em] text-muted-foreground">
|
||
Steg 01 / Konfigurera
|
||
</p>
|
||
<h1 className="mt-4 max-w-[14ch] text-5xl font-medium leading-[0.95] tracking-[-0.035em] md:text-7xl">
|
||
Bygg jobbet.
|
||
</h1>
|
||
<p className="mt-5 max-w-[34ch] font-mono text-[12px] uppercase tracking-[0.06em] text-muted-foreground">
|
||
Sju val. Ett pris. Inget gissat.
|
||
</p>
|
||
</div>
|
||
|
||
<div className="mt-12 grid grid-cols-1 gap-6 lg:grid-cols-[1fr_360px]">
|
||
{/* LEFT — builder */}
|
||
<div className="reveal-left flex flex-col gap-4">
|
||
<Block index="01" label="Marknader">
|
||
<div className="flex flex-wrap gap-2">
|
||
{MARKETS.map((m) => (
|
||
<Chip
|
||
key={m.code}
|
||
active={markets.includes(m.code)}
|
||
disabled={!m.live}
|
||
onClick={() => toggle(markets, m.code, setMarkets)}
|
||
>
|
||
{m.code}
|
||
{!m.live ? " ·" : ""}
|
||
</Chip>
|
||
))}
|
||
</div>
|
||
<p className="mt-3 font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">
|
||
Norden live. Övriga kommer.
|
||
</p>
|
||
</Block>
|
||
|
||
<Block index="02" label="Typ av poster">
|
||
<div className="flex gap-2">
|
||
{ENTITY_TYPES.map((e) => (
|
||
<Chip key={e.id} active={type === e.id} onClick={() => setType(e.id)}>
|
||
{e.id}
|
||
</Chip>
|
||
))}
|
||
</div>
|
||
</Block>
|
||
|
||
<Block index="03" label="Fält att inkludera">
|
||
<div className="flex flex-col">
|
||
{FIELDS.map((f, i) => {
|
||
const on = f.locked || fieldIds.includes(f.id);
|
||
return (
|
||
<label
|
||
key={f.id}
|
||
className="flex items-center gap-3 border-border py-3 [&:not(:last-child)]:border-b"
|
||
style={{ cursor: f.locked ? "default" : "pointer" }}
|
||
>
|
||
<span
|
||
className="flex h-4 w-4 shrink-0 items-center justify-center border"
|
||
style={{
|
||
borderColor: on ? "var(--foreground)" : "var(--border)",
|
||
backgroundColor: on ? "var(--foreground)" : "transparent",
|
||
color: "var(--background)",
|
||
}}
|
||
>
|
||
{on ? "✓" : ""}
|
||
</span>
|
||
<input
|
||
type="checkbox"
|
||
className="sr-only"
|
||
checked={on}
|
||
disabled={f.locked}
|
||
onChange={() => toggle(fieldIds, f.id, setFieldIds)}
|
||
/>
|
||
<span className="flex-1">
|
||
<span className="text-sm">{f.name}</span>
|
||
<span className="ml-2 text-xs text-muted-foreground">{f.sub}</span>
|
||
{f.locked ? (
|
||
<span className="ml-2 font-mono text-[10px] uppercase text-muted-foreground">låst</span>
|
||
) : null}
|
||
</span>
|
||
<ProvenanceBadge level={f.level as ProvLevel} source={f.source} freshness={f.freshness} />
|
||
</label>
|
||
);
|
||
})}
|
||
</div>
|
||
</Block>
|
||
|
||
<Block index="04" label="Berikning & matchning">
|
||
<div className="flex flex-wrap gap-2">
|
||
{ENRICHMENTS.map((e) => (
|
||
<Chip
|
||
key={e.id}
|
||
active={enrichmentIds.includes(e.id)}
|
||
onClick={() => toggle(enrichmentIds, e.id, setEnrichmentIds)}
|
||
>
|
||
{e.label}
|
||
</Chip>
|
||
))}
|
||
</div>
|
||
</Block>
|
||
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||
<Block index="05" label="Volym">
|
||
<input
|
||
type="number"
|
||
min={100}
|
||
step={100}
|
||
value={volume}
|
||
onChange={(e) => setVolume(Math.max(100, Number(e.target.value) || 0))}
|
||
className="w-full border border-border bg-input px-3 py-2 font-mono text-sm outline-none focus:border-foreground"
|
||
/>
|
||
<div className="mt-3 flex gap-2">
|
||
{FREQUENCIES.map((fr) => (
|
||
<Chip key={fr} active={frequency === fr} onClick={() => setFrequency(fr)}>
|
||
{fr}
|
||
</Chip>
|
||
))}
|
||
</div>
|
||
</Block>
|
||
|
||
<Block index="06" label="Konfidensmål">
|
||
<div className="flex items-baseline justify-between">
|
||
<span className="font-mono text-2xl" style={{ color: confidenceColor(confidence) }}>
|
||
{confidence}%
|
||
</span>
|
||
<span className="font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">
|
||
80 – 99
|
||
</span>
|
||
</div>
|
||
<input
|
||
type="range"
|
||
min={80}
|
||
max={99}
|
||
value={confidence}
|
||
onChange={(e) => setConfidence(Number(e.target.value))}
|
||
className="mt-3 w-full"
|
||
style={{ accentColor: confidenceColor(confidence) }}
|
||
/>
|
||
<div className="mt-2 h-1.5 w-full bg-secondary">
|
||
<div
|
||
className="h-full transition-all"
|
||
style={{ width: `${((confidence - 80) / 19) * 100}%`, backgroundColor: confidenceColor(confidence) }}
|
||
/>
|
||
</div>
|
||
</Block>
|
||
</div>
|
||
|
||
<Block index="07" label="Leveransformat">
|
||
<div className="flex flex-wrap gap-2">
|
||
{DELIVERY_FORMATS.map((f) => (
|
||
<Chip key={f} active={formats.includes(f)} onClick={() => toggle(formats, f, setFormats)}>
|
||
{f}
|
||
</Chip>
|
||
))}
|
||
</div>
|
||
</Block>
|
||
</div>
|
||
|
||
{/* RIGHT — sticky summary */}
|
||
<aside className="reveal-right lg:sticky lg:top-6 lg:self-start">
|
||
<div className="border border-border">
|
||
<div className="flex items-center gap-3 border-b border-border px-4 py-2.5">
|
||
<span className="font-mono text-[11px] tabular-nums text-muted-foreground">∑</span>
|
||
<span className="h-3 w-px bg-border" />
|
||
<span className="font-mono text-[11px] uppercase tracking-[0.12em] text-foreground">Order</span>
|
||
</div>
|
||
<dl className="divide-y divide-border">
|
||
{[
|
||
["Marknader", markets.join(" · ") || "—"],
|
||
["Typ", type],
|
||
["Fält", String(FIELDS.filter((f) => f.locked || fieldIds.includes(f.id)).length)],
|
||
["Berikning", String(enrichmentIds.length)],
|
||
["Volym", new Intl.NumberFormat("sv-SE").format(volume)],
|
||
["Frekvens", frequency],
|
||
["Leverans", `≈ ${est.timelineDays} dgr`],
|
||
["Konfidens", `${est.estConfidence}%`],
|
||
].map(([k, v]) => (
|
||
<div key={k} className="flex items-center justify-between px-4 py-2.5">
|
||
<dt className="font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">{k}</dt>
|
||
<dd className="max-w-[60%] truncate text-right font-mono text-xs">{v}</dd>
|
||
</div>
|
||
))}
|
||
</dl>
|
||
|
||
{/* price — oversized numeral */}
|
||
<div className="border-t border-border p-4">
|
||
<p className="font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">
|
||
Arvode{est.recurring ? " /mån" : ""}
|
||
</p>
|
||
<p className="mt-1 text-[40px] font-medium leading-none tracking-[-0.03em] tabular-nums">
|
||
{formatSEK(est.price)}
|
||
</p>
|
||
</div>
|
||
|
||
{/* deposit — the only red on this screen */}
|
||
<div className="m-4 mt-0 border p-4" style={{ borderColor: "var(--accent-red)" }}>
|
||
<p className="font-mono text-[10.5px] uppercase tracking-[0.1em]" style={{ color: "var(--accent-red)" }}>
|
||
Förskott · 50 %
|
||
</p>
|
||
<p className="mt-1 font-mono text-2xl font-medium tabular-nums">{formatSEK(est.deposit)}</p>
|
||
<p className="mt-2 text-xs text-muted-foreground">
|
||
Resten i depå. Frigörs vid godkänd leverans.
|
||
</p>
|
||
</div>
|
||
|
||
<div className="p-4 pt-0">
|
||
<button
|
||
type="button"
|
||
onClick={submit}
|
||
disabled={creating}
|
||
className="block w-full bg-foreground py-3.5 text-center font-mono text-[12px] uppercase tracking-[0.1em] text-background transition-colors hover:bg-[var(--accent-red)] disabled:opacity-50"
|
||
>
|
||
{creating ? "Skapar…" : "Betala förskott →"}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<p className="mt-3 text-center font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground">
|
||
Källa per fält · GDPR-native · inget gissat
|
||
</p>
|
||
</aside>
|
||
</div>
|
||
</div>
|
||
</main>
|
||
);
|
||
}
|