fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { useT } from "@/components/locale-provider";
|
||||
import { PortalShell, StatusPill } from "@/components/portal-shell";
|
||||
import { LoadingSkeleton, EmptyState, ErrorState } from "@/components/states";
|
||||
|
||||
interface Sub {
|
||||
sub_id: string;
|
||||
order_id: string;
|
||||
cadence: string;
|
||||
status: string;
|
||||
webhook_url: string | null;
|
||||
}
|
||||
|
||||
export default function SubscriptionsPage() {
|
||||
const t = useT();
|
||||
const [subs, setSubs] = useState<Sub[] | null>(null);
|
||||
const [error, setError] = useState(false);
|
||||
const [orderId, setOrderId] = useState("");
|
||||
const [cadence, setCadence] = useState("weekly");
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setError(false);
|
||||
try { setSubs(await (await fetch("/api/subscriptions", { cache: "no-store" })).json()); }
|
||||
catch { setError(true); }
|
||||
}, []);
|
||||
useEffect(() => { void load(); }, [load]);
|
||||
|
||||
async function create() {
|
||||
if (!orderId.trim() || busy) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
const r = await fetch("/api/subscriptions", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ order_id: orderId.trim(), cadence }) });
|
||||
if (r.ok) {
|
||||
toast.success(t({ en: "Flow created.", sv: "Flöde skapat." }));
|
||||
setOrderId("");
|
||||
void load();
|
||||
} else {
|
||||
toast.error(t({ en: "Could not create flow. Check the order ID.", sv: "Kunde inte skapa flöde. Kontrollera order-ID." }));
|
||||
}
|
||||
} catch {
|
||||
toast.error(t({ en: "Network error. Try again.", sv: "Nätverksfel. Försök igen." }));
|
||||
}
|
||||
setBusy(false);
|
||||
}
|
||||
|
||||
return (
|
||||
<PortalShell variant="customer" title={t({ en: "Subscriptions & flows", sv: "Prenumerationer & flöden" })}>
|
||||
<div className="border border-border p-5">
|
||||
<p className="font-mono text-[11px] uppercase tracking-[0.12em] text-muted-foreground">
|
||||
{t({ en: "Convert an order into a flow", sv: "Gör om en order till ett flöde" })}
|
||||
</p>
|
||||
<div className="mt-4 flex flex-wrap items-end gap-3">
|
||||
<input
|
||||
value={orderId}
|
||||
onChange={(e) => setOrderId(e.target.value)}
|
||||
placeholder="order_id"
|
||||
className="border border-border bg-input px-3 py-2 font-mono text-xs outline-none focus:border-foreground"
|
||||
/>
|
||||
<select
|
||||
value={cadence}
|
||||
onChange={(e) => setCadence(e.target.value)}
|
||||
className="border border-border bg-input px-3 py-2 font-mono text-xs outline-none focus:border-foreground"
|
||||
>
|
||||
<option value="weekly">{t({ en: "Weekly", sv: "Veckovis" })}</option>
|
||||
<option value="daily">{t({ en: "Daily", sv: "Dagligt" })}</option>
|
||||
</select>
|
||||
<button
|
||||
onClick={create}
|
||||
disabled={busy || !orderId}
|
||||
className="bg-foreground px-5 py-2 font-mono text-[11px] uppercase tracking-[0.1em] text-background transition-colors hover:bg-[var(--accent-red)] disabled:opacity-50"
|
||||
>
|
||||
{busy ? t({ en: "Creating…", sv: "Skapar…" }) : t({ en: "Create flow", sv: "Skapa flöde" })}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-8">
|
||||
{error ? (
|
||||
<ErrorState onRetry={load} />
|
||||
) : !subs ? (
|
||||
<LoadingSkeleton rows={4} />
|
||||
) : subs.length === 0 ? (
|
||||
<EmptyState
|
||||
title={t({ en: "No flows yet.", sv: "Inga flöden än." })}
|
||||
line={t({ en: "Convert an order into a recurring flow.", sv: "Gör om en order till ett återkommande flöde." })}
|
||||
/>
|
||||
) : (
|
||||
<div className="border border-border">
|
||||
<div className="grid grid-cols-[1.6fr_1fr_1fr_1fr] border-b border-border bg-secondary/50 px-4 py-2.5 font-mono text-[10px] uppercase tracking-[0.1em] text-muted-foreground">
|
||||
<span>{t({ en: "Order", sv: "Order" })}</span>
|
||||
<span>{t({ en: "Cadence", sv: "Kadens" })}</span>
|
||||
<span>{t({ en: "Status", sv: "Status" })}</span>
|
||||
<span>{t({ en: "Webhook", sv: "Webhook" })}</span>
|
||||
</div>
|
||||
{subs.map((s) => (
|
||||
<div key={s.sub_id} className="grid grid-cols-[1.6fr_1fr_1fr_1fr] items-center border-b border-border px-4 py-3 text-sm last:border-b-0">
|
||||
<span className="font-mono text-[12px] tabular-nums">{s.order_id.slice(0, 8)}…</span>
|
||||
<span className="font-mono text-xs">{s.cadence}</span>
|
||||
<StatusPill status={s.status} />
|
||||
<span className="truncate font-mono text-[11px] text-muted-foreground">{s.webhook_url ?? "—"}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</PortalShell>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user