2024-10-19 16:37:40 -04:00
|
|
|
"use client";
|
2024-10-14 19:30:38 -04:00
|
|
|
|
2024-10-19 16:37:40 -04:00
|
|
|
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";
|
2024-10-14 19:30:38 -04:00
|
|
|
|
2024-10-19 16:37:40 -04:00
|
|
|
type Step = "org" | "site" | "resources";
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
export default function StepperForm() {
|
2024-10-19 16:37:40 -04:00
|
|
|
const [currentStep, setCurrentStep] = useState<Step>("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);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
const checkOrgIdAvailability = useCallback(async (value: string) => {
|
|
|
|
try {
|
|
|
|
const res = await api.get(`/org/checkId`, {
|
|
|
|
params: {
|
2024-10-19 16:37:40 -04:00
|
|
|
orgId: value,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setOrgIdTaken(res.status !== 404);
|
2024-10-14 19:30:38 -04:00
|
|
|
} catch (error) {
|
2024-10-19 16:37:40 -04:00
|
|
|
console.error("Error checking org ID availability:", error);
|
|
|
|
setOrgIdTaken(false);
|
2024-10-14 19:30:38 -04:00
|
|
|
}
|
2024-10-19 16:37:40 -04:00
|
|
|
}, []);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
const debouncedCheckOrgIdAvailability = useCallback(
|
|
|
|
debounce(checkOrgIdAvailability, 300),
|
2024-10-19 16:37:40 -04:00
|
|
|
[checkOrgIdAvailability],
|
|
|
|
);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (orgId) {
|
2024-10-19 16:37:40 -04:00
|
|
|
debouncedCheckOrgIdAvailability(orgId);
|
2024-10-14 19:30:38 -04:00
|
|
|
}
|
2024-10-19 16:37:40 -04:00
|
|
|
}, [orgId, debouncedCheckOrgIdAvailability]);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
const showOrgIdError = () => {
|
|
|
|
if (orgIdTaken) {
|
|
|
|
return (
|
|
|
|
<p className="text-sm text-red-500">
|
|
|
|
This ID is already taken. Please choose another.
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateId = (name: string) => {
|
2024-10-19 16:37:40 -04:00
|
|
|
return name.toLowerCase().replace(/\s+/g, "-");
|
|
|
|
};
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
const handleNext = async () => {
|
2024-10-19 16:37:40 -04:00
|
|
|
if (currentStep === "org") {
|
2024-10-14 19:30:38 -04:00
|
|
|
const res = await api
|
|
|
|
.put(`/org`, {
|
|
|
|
orgId: orgId,
|
|
|
|
name: orgName,
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
toast({
|
2024-10-19 16:37:40 -04:00
|
|
|
title: "Error creating org...",
|
2024-10-14 19:30:38 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res && res.status === 201) {
|
2024-10-19 16:37:40 -04:00
|
|
|
setCurrentStep("site");
|
|
|
|
setOrgCreated(true);
|
2024-10-14 19:30:38 -04:00
|
|
|
}
|
2024-10-19 16:37:40 -04:00
|
|
|
} else if (currentStep === "site") setCurrentStep("resources");
|
|
|
|
};
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
const handlePrevious = () => {
|
2024-10-19 16:37:40 -04:00
|
|
|
if (currentStep === "site") setCurrentStep("org");
|
|
|
|
else if (currentStep === "resources") setCurrentStep("site");
|
|
|
|
};
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
return (
|
2024-10-19 16:37:40 -04:00
|
|
|
<>
|
|
|
|
<Card className="w-full max-w-2xl mx-auto">
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle>Setup Your Environment</CardTitle>
|
|
|
|
<CardDescription>
|
|
|
|
Create your organization, site, and resources.
|
|
|
|
</CardDescription>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<div className="mb-8">
|
|
|
|
<div className="flex justify-between mb-2">
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<div
|
|
|
|
className={`w-8 h-8 rounded-full flex items-center justify-center mb-2 ${currentStep === "org" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
1
|
|
|
|
</div>
|
|
|
|
<span
|
|
|
|
className={`text-sm font-medium ${currentStep === "org" ? "text-primary" : "text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
Create Org
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<div
|
|
|
|
className={`w-8 h-8 rounded-full flex items-center justify-center mb-2 ${currentStep === "site" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
2
|
|
|
|
</div>
|
|
|
|
<span
|
|
|
|
className={`text-sm font-medium ${currentStep === "site" ? "text-primary" : "text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
Create Site
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<div
|
|
|
|
className={`w-8 h-8 rounded-full flex items-center justify-center mb-2 ${currentStep === "resources" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
3
|
|
|
|
</div>
|
|
|
|
<span
|
|
|
|
className={`text-sm font-medium ${currentStep === "resources" ? "text-primary" : "text-muted-foreground"}`}
|
|
|
|
>
|
|
|
|
Create Resources
|
|
|
|
</span>
|
|
|
|
</div>
|
2024-10-14 19:30:38 -04:00
|
|
|
</div>
|
2024-10-19 16:37:40 -04:00
|
|
|
<div className="flex items-center">
|
|
|
|
<div className="flex-1 h-px bg-border"></div>
|
|
|
|
<div className="flex-1 h-px bg-border"></div>
|
2024-10-14 19:30:38 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-10-19 16:37:40 -04:00
|
|
|
{currentStep === "org" && (
|
|
|
|
<div className="space-y-4">
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="orgName">
|
|
|
|
Organization Name
|
|
|
|
</Label>
|
|
|
|
<Input
|
|
|
|
id="orgName"
|
|
|
|
value={orgName}
|
|
|
|
onChange={(e) => {
|
|
|
|
setOrgName(e.target.value);
|
|
|
|
setOrgId(generateId(e.target.value));
|
|
|
|
}}
|
|
|
|
placeholder="Enter organization name"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="orgId">Organization ID</Label>
|
|
|
|
<Input
|
|
|
|
id="orgId"
|
|
|
|
value={orgId}
|
|
|
|
onChange={(e) => setOrgId(e.target.value)}
|
|
|
|
/>
|
|
|
|
{showOrgIdError()}
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
This ID is automatically generated from the
|
|
|
|
organization name and must be unique.
|
|
|
|
</p>
|
|
|
|
</div>
|
2024-10-14 19:30:38 -04:00
|
|
|
</div>
|
2024-10-19 16:37:40 -04:00
|
|
|
)}
|
|
|
|
{currentStep === "site" && (
|
|
|
|
<div className="space-y-6">
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="siteName">Site Name</Label>
|
|
|
|
<Input
|
|
|
|
id="siteName"
|
|
|
|
value={siteName}
|
|
|
|
onChange={(e) =>
|
|
|
|
setSiteName(e.target.value)
|
|
|
|
}
|
|
|
|
placeholder="Enter site name"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{currentStep === "resources" && (
|
|
|
|
<div className="space-y-6">
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="resourceName">
|
|
|
|
Resource Name
|
|
|
|
</Label>
|
|
|
|
<Input
|
|
|
|
id="resourceName"
|
|
|
|
value={resourceName}
|
|
|
|
onChange={(e) =>
|
|
|
|
setResourceName(e.target.value)
|
|
|
|
}
|
|
|
|
placeholder="Enter resource name"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="flex justify-between pt-4">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant="outline"
|
|
|
|
onClick={handlePrevious}
|
|
|
|
disabled={
|
|
|
|
currentStep === "org" ||
|
|
|
|
(currentStep === "site" && orgCreated)
|
|
|
|
}
|
2024-10-14 19:30:38 -04:00
|
|
|
>
|
2024-10-19 16:37:40 -04:00
|
|
|
Previous
|
|
|
|
</Button>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
{currentStep !== "org" ? (
|
|
|
|
<Link
|
|
|
|
href={`/${orgId}/sites`}
|
|
|
|
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
|
|
|
>
|
|
|
|
Skip for now
|
|
|
|
</Link>
|
|
|
|
) : null}
|
2024-10-14 19:30:38 -04:00
|
|
|
|
2024-10-19 16:37:40 -04:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
id="button"
|
|
|
|
onClick={handleNext}
|
|
|
|
>
|
|
|
|
Create
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
</>
|
|
|
|
);
|
2024-10-14 19:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function debounce<T extends (...args: any[]) => any>(
|
|
|
|
func: T,
|
2024-10-19 16:37:40 -04:00
|
|
|
wait: number,
|
2024-10-14 19:30:38 -04:00
|
|
|
): (...args: Parameters<T>) => void {
|
2024-10-19 16:37:40 -04:00
|
|
|
let timeout: NodeJS.Timeout | null = null;
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
return (...args: Parameters<T>) => {
|
2024-10-19 16:37:40 -04:00
|
|
|
if (timeout) clearTimeout(timeout);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
timeout = setTimeout(() => {
|
2024-10-19 16:37:40 -04:00
|
|
|
func(...args);
|
|
|
|
}, wait);
|
|
|
|
};
|
|
|
|
}
|