147 lines
4.4 KiB
TypeScript
147 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { createProject } from "@/app/(frontend)/projects/actions";
|
|
|
|
interface CreateProjectDialogProps {
|
|
readonly open: boolean;
|
|
readonly onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
export function CreateProjectDialog({ open, onOpenChange }: CreateProjectDialogProps) {
|
|
const router = useRouter();
|
|
const [name, setName] = useState("");
|
|
const [key, setKey] = useState("");
|
|
const [description, setDescription] = useState("");
|
|
const [pending, setPending] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
function reset() {
|
|
setName("");
|
|
setKey("");
|
|
setDescription("");
|
|
setPending(false);
|
|
setError(null);
|
|
}
|
|
|
|
async function handleSubmit() {
|
|
const trimmedName = name.trim();
|
|
const trimmedKey = key.trim().toUpperCase();
|
|
if (!trimmedName) {
|
|
setError("Project name is required.");
|
|
return;
|
|
}
|
|
if (!trimmedKey) {
|
|
setError("Project key is required.");
|
|
return;
|
|
}
|
|
|
|
setPending(true);
|
|
setError(null);
|
|
try {
|
|
const result = await createProject({
|
|
name: trimmedName,
|
|
key: trimmedKey,
|
|
description: description.trim(),
|
|
});
|
|
if (!result.success) {
|
|
setError(result.error ?? "Could not create the project.");
|
|
setPending(false);
|
|
return;
|
|
}
|
|
onOpenChange(false);
|
|
reset();
|
|
toast.success("Project created");
|
|
router.refresh();
|
|
} catch (caught) {
|
|
setError(caught instanceof Error ? caught.message : "Could not create the project.");
|
|
setPending(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog
|
|
open={open}
|
|
onOpenChange={(nextOpen) => {
|
|
onOpenChange(nextOpen);
|
|
if (!nextOpen) reset();
|
|
}}
|
|
>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>Create project</DialogTitle>
|
|
<DialogDescription>Set up a project space for tickets, sprints, and releases.</DialogDescription>
|
|
</DialogHeader>
|
|
<form
|
|
className="flex flex-col gap-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
void handleSubmit();
|
|
}}
|
|
>
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor="project-name">Name</Label>
|
|
<Input
|
|
id="project-name"
|
|
placeholder="Project name"
|
|
value={name}
|
|
onChange={(event) => setName(event.target.value)}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor="project-key">Key</Label>
|
|
<Input
|
|
id="project-key"
|
|
placeholder="POLARIS"
|
|
maxLength={10}
|
|
value={key}
|
|
onChange={(event) => setKey(event.target.value.toUpperCase().slice(0, 10))}
|
|
disabled={pending}
|
|
className="font-mono uppercase"
|
|
/>
|
|
<p className="text-xs text-muted-foreground">Up to 10 characters. Keys are stored in uppercase.</p>
|
|
</div>
|
|
<div className="flex flex-col gap-1.5">
|
|
<Label htmlFor="project-description">Description</Label>
|
|
<Textarea
|
|
id="project-description"
|
|
rows={4}
|
|
placeholder="What is this project about?"
|
|
value={description}
|
|
onChange={(event) => setDescription(event.target.value)}
|
|
disabled={pending}
|
|
/>
|
|
</div>
|
|
{error && (
|
|
<p role="alert" className="text-sm text-destructive">
|
|
{error}
|
|
</p>
|
|
)}
|
|
<DialogFooter>
|
|
<Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={pending}>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={pending}>
|
|
{pending ? "Creating..." : "Create project"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|