'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { ProjectCreate, ProjectUpdate } from '@/lib/types'; import { createProject, updateProject } from '@/lib/api'; import { motion } from 'framer-motion'; import { Type, AlignLeft, Palette, Calendar, CheckCircle2 } from 'lucide-react'; interface ProjectFormProps { initialProject?: any; isEditing?: boolean; onCancel?: () => void; onSuccess?: (project: any) => void; } export function ProjectForm({ initialProject, isEditing = false, onCancel, onSuccess }: ProjectFormProps) { const [name, setName] = useState(initialProject?.name || ''); const [description, setDescription] = useState(initialProject?.description || ''); const [color, setColor] = useState(initialProject?.color || '#3b82f6'); const [deadline, setDeadline] = useState(initialProject?.deadline || ''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const router = useRouter(); const getUserId = () => { if (typeof window !== 'undefined') { const userStr = localStorage.getItem('user'); if (userStr) { try { const user = JSON.parse(userStr); return user.id; } catch (e) { return null; } } } return null; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!name.trim()) { setError('Cluster designation required'); return; } const userId = getUserId(); if (!userId) return; setLoading(true); try { let project; if (isEditing && initialProject) { const projectData: ProjectUpdate = { name: name.trim(), description: description.trim() || undefined, color: color || undefined, deadline: deadline || undefined }; project = await updateProject(userId, initialProject.id, projectData); } else { const projectData: ProjectCreate = { name: name.trim(), description: description.trim() || undefined, color: color || undefined }; project = await createProject(userId, projectData); } if (onSuccess) onSuccess(project); } catch (err: any) { setError(err.message || 'Operational failure'); } finally { setLoading(false); } }; const colorOptions = [ { name: 'Blue', value: '#3b82f6' }, { name: 'Purple', value: '#8b5cf6' }, { name: 'Emerald', value: '#10b981' }, { name: 'Amber', value: '#f59e0b' }, { name: 'Rose', value: '#ef4444' }, { name: 'Indigo', value: '#6366f1' }, { name: 'Teal', value: '#14b8a6' }, { name: 'Fuchsia', value: '#d946ef' } ]; return (
); }