'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import api from '@/lib/api'; import { ArrowLeftIcon, PlusIcon, TrashIcon, GripVerticalIcon } from '@/components/ui/Icons'; interface Step { id: string; name: string; duration_minutes?: number; position: number; } const ICONS = ['✨', '🌅', '🌙', '☀️', '💪', '🧘', '📚', '🍳', '🏃', '💼', '🎯', '⭐', '🔥', '💤', '🧠']; const STEP_TYPES = [ { value: 'generic', label: 'Generic' }, { value: 'timer', label: 'Timer' }, { value: 'checklist', label: 'Checklist' }, { value: 'meditation', label: 'Meditation' }, { value: 'exercise', label: 'Exercise' }, ]; export default function NewRoutinePage() { const router = useRouter(); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [icon, setIcon] = useState('✨'); const [steps, setSteps] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const handleAddStep = () => { const newStep: Step = { id: `temp-${Date.now()}`, name: '', duration_minutes: 5, position: steps.length + 1, }; setSteps([...steps, newStep]); }; const handleUpdateStep = (index: number, updates: Partial) => { const newSteps = [...steps]; newSteps[index] = { ...newSteps[index], ...updates }; setSteps(newSteps); }; const handleDeleteStep = (index: number) => { const newSteps = steps.filter((_, i) => i !== index); setSteps(newSteps.map((s, i) => ({ ...s, position: i + 1 }))); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!name.trim()) { setError('Please enter a routine name'); return; } const validSteps = steps.filter(s => s.name.trim()); if (validSteps.length === 0) { setError('Please add at least one step'); return; } setIsLoading(true); setError(''); try { const routine = await api.routines.create({ name, description, icon }); for (const step of validSteps) { await api.routines.addStep(routine.id, { name: step.name, duration_minutes: step.duration_minutes, }); } router.push('/dashboard/routines'); } catch (err) { setError((err as Error).message || 'Failed to create routine'); } finally { setIsLoading(false); } }; return (

New Routine

{error && (
{error}
)} {/* Basic Info */}
{ICONS.map((i) => ( ))}
setName(e.target.value)} placeholder="Morning Routine" className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 outline-none" />
setDescription(e.target.value)} placeholder="Start your day right" className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 outline-none" />
{/* Steps */}

Steps

{steps.length === 0 ? (

Add steps to your routine

) : (
{steps.map((step, index) => (
handleUpdateStep(index, { name: e.target.value })} placeholder="Step name" className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 outline-none" />
))}
)}
); }