"use client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import Link from "next/link"; import api from "@app/api"; import { toast } from "@app/hooks/use-toast"; import { useCallback, useEffect, useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@app/components/ui/card"; type Step = "org" | "site" | "resources"; export default function StepperForm() { const [currentStep, setCurrentStep] = useState("org"); const [orgName, setOrgName] = useState(""); const [orgId, setOrgId] = useState(""); const [siteName, setSiteName] = useState(""); const [resourceName, setResourceName] = useState(""); const [orgCreated, setOrgCreated] = useState(false); const [orgIdTaken, setOrgIdTaken] = useState(false); const checkOrgIdAvailability = useCallback(async (value: string) => { try { const res = await api.get(`/org/checkId`, { params: { orgId: value, }, }); setOrgIdTaken(res.status !== 404); } catch (error) { console.error("Error checking org ID availability:", error); setOrgIdTaken(false); } }, []); const debouncedCheckOrgIdAvailability = useCallback( debounce(checkOrgIdAvailability, 300), [checkOrgIdAvailability], ); useEffect(() => { if (orgId) { debouncedCheckOrgIdAvailability(orgId); } }, [orgId, debouncedCheckOrgIdAvailability]); const showOrgIdError = () => { if (orgIdTaken) { return (

This ID is already taken. Please choose another.

); } return null; }; const generateId = (name: string) => { return name.toLowerCase().replace(/\s+/g, "-"); }; const handleNext = async () => { if (currentStep === "org") { const res = await api .put(`/org`, { orgId: orgId, name: orgName, }) .catch((e) => { toast({ title: "Error creating org...", }); }); if (res && res.status === 201) { setCurrentStep("site"); setOrgCreated(true); } } else if (currentStep === "site") setCurrentStep("resources"); }; const handlePrevious = () => { if (currentStep === "site") setCurrentStep("org"); else if (currentStep === "resources") setCurrentStep("site"); }; return ( <> Setup Your Environment Create your organization, site, and resources.
1
Create Org
2
Create Site
3
Create Resources
{currentStep === "org" && (
{ setOrgName(e.target.value); setOrgId(generateId(e.target.value)); }} placeholder="Enter organization name" required />
setOrgId(e.target.value)} /> {showOrgIdError()}

This ID is automatically generated from the organization name and must be unique.

)} {currentStep === "site" && (
setSiteName(e.target.value) } placeholder="Enter site name" required />
)} {currentStep === "resources" && (
setResourceName(e.target.value) } placeholder="Enter resource name" required />
)}
{currentStep !== "org" ? ( Skip for now ) : null}
); } function debounce any>( func: T, wait: number, ): (...args: Parameters) => void { let timeout: NodeJS.Timeout | null = null; return (...args: Parameters) => { if (timeout) clearTimeout(timeout); timeout = setTimeout(() => { func(...args); }, wait); }; }