'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { useToast } from '@/components/ui/use-toast'; interface CreateProjectDialogProps { isOpen: boolean; onOpenChange: (open: boolean) => void; organizationId: string; onSuccess?: (projectId: string) => void; } export function CreateProjectDialog({ isOpen, onOpenChange, organizationId, onSuccess }: CreateProjectDialogProps) { const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const { toast } = useToast(); const router = useRouter(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!name.trim()) { toast({ title: 'Error', description: 'Project name is required', variant: 'destructive', }); return; } try { setIsSubmitting(true); const response = await fetch('/api/projects', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name, description: description.trim() || null, organizationId, }), }); if (response.ok) { const result = await response.json(); console.log("CreateProjectDialog: API response:", result); const project = result.data; // Extract project from data property toast({ title: 'Success', description: `${project.name} has been created`, }); // Reset form setName(''); setDescription(''); // Close dialog onOpenChange(false); // Call success callback or redirect console.log("CreateProjectDialog: About to redirect/callback with projectId:", project.id); if (onSuccess) { onSuccess(project.id); } else { router.push(`/projects/${project.id}`); } } else { const error = await response.json(); throw new Error(error.error || 'Failed to create project'); } } catch (error) { toast({ title: 'Error', description: error instanceof Error ? error.message : 'Failed to create project', variant: 'destructive', }); } finally { setIsSubmitting(false); } }; return (
Create project Create a new project in your organization.
setName(e.target.value)} placeholder="My Project" className="col-span-3" required />