"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { ShieldHalf } from "lucide-react"; import { api, clearToken } from "@/lib/api"; export default function Onboarding() { const r = useRouter(); const [form, setForm] = useState({ name: "", gstin: "", phone: "",address: "" }); const [err, setErr] = useState(""); const [busy, setBusy] = useState(false); const [ready, setReady] = useState(false); // If the user already has an org (or is a product admin), don't show onboarding. useEffect(() => { api("/me") .then((me) => { if (me.is_product_admin) r.replace("/admin"); else if (me.has_org) r.replace("/dashboard"); else setReady(true); }) .catch(() => { clearToken(); r.replace("/login"); }); }, [r]); async function submit(e: React.FormEvent) { e.preventDefault(); if (!form.name.trim()) { setErr("Organization name is required."); return; } setErr(""); setBusy(true); try { await api("/organization", { method: "POST", body: JSON.stringify(form) }); r.push("/dashboard"); } catch (e: any) { // Already has an org → just go to the dashboard. if (e.status === 409) { r.replace("/dashboard"); return; } setErr(typeof e.detail === "string" ? e.detail : "Failed to create organization"); } finally { setBusy(false); } } const set = (k: string) => (e: any) => setForm({ ...form, [k]: e.target.value }); if (!ready) return
Set up your brand workspace to get started.