diff --git a/src/routes/(app)/habits/+page.svelte b/src/routes/(app)/habits/+page.svelte index b11a300..ba34019 100644 --- a/src/routes/(app)/habits/+page.svelte +++ b/src/routes/(app)/habits/+page.svelte @@ -93,10 +93,10 @@ - { + { showCreateDialog = false; - }} + }} /> diff --git a/src/routes/(app)/habits/HabitActionMenu.svelte b/src/routes/(app)/habits/HabitActionMenu.svelte index 1786356..7d76c7e 100644 --- a/src/routes/(app)/habits/HabitActionMenu.svelte +++ b/src/routes/(app)/habits/HabitActionMenu.svelte @@ -1,23 +1,21 @@ @@ -47,26 +45,10 @@ - - - Edit profile - - Make changes to your profile here. Click save when you're done. - - -
-
- - -
-
- - -
-
- - - + + {#if currentHabit} + { editHabitDialog = false; }} /> + {/if}
diff --git a/src/routes/(app)/habits/HabitCreationForm.svelte b/src/routes/(app)/habits/HabitCreationForm.svelte index 173af77..2829e1b 100644 --- a/src/routes/(app)/habits/HabitCreationForm.svelte +++ b/src/routes/(app)/habits/HabitCreationForm.svelte @@ -7,58 +7,86 @@ import * as Collapsible from '$lib/components/ui/collapsible'; import { ChevronDown, ChevronUp } from '@lucide/svelte'; import { getHabitService } from '$lib/context/habits.svelte'; + import type { Habit } from '$lib/triplit/schema'; + + let { + habit, + onComplete + }: { + habit?: Habit; + onComplete?: () => Promise | void; + } = $props(); - let { onHabitCreated }: { onHabitCreated?: () => Promise | void } = $props(); - const habitService = getHabitService(); + const isEditMode = $derived(!!habit); + let showAdvancedOptions = $state(false); let isSubmitting = $state(false); - // Form fields - let name = $state(''); - let target = $state(1); - let duration = $state(1); - let unit = $state(''); - let increment = $state(1); - let startDate = $state(new Date().toISOString().split('T')[0]); - let active = $state(true); + // Form fields - initialize from habit if editing + let name = $state(habit?.name ?? ''); + let target = $state(habit?.target ?? 1); + let duration = $state(habit?.duration ?? 1); + let unit = $state(habit?.unit ?? ''); + let increment = $state(habit?.increment ?? 1); + let startDate = $state( + habit?.startDate + ? habit.startDate.toISOString().split('T')[0] + : new Date().toISOString().split('T')[0] + ); + let active = $state(habit?.active ?? true); + // Re-initialize when habit changes (for edit mode) + $effect(() => { + if (habit) { + name = habit.name; + target = habit.target; + duration = habit.duration; + unit = habit.unit; + increment = habit.increment; + startDate = habit.startDate.toISOString().split('T')[0]; + active = habit.active; + } + }); async function handleSubmit() { if (isSubmitting || !name.trim()) return; - + try { isSubmitting = true; - - await habitService.createHabit({ + + const habitData = { name, target, duration, increment, unit, startDate: (() => { - // Create date in local timezone to avoid UTC conversion issues const [year, month, day] = startDate.split('-').map(Number); return new Date(year, month - 1, day); })(), active - }) + }; - // Reset form - name = ''; - target = 1; - duration = 1; - unit = ''; - increment = 1; - startDate = new Date().toISOString().split('T')[0]; - active = true; - showAdvancedOptions = false; - - // Notify parent - onHabitCreated?.(); + if (isEditMode && habit) { + await habitService.updateHabit(habit.id, habitData); + } else { + await habitService.createHabit(habitData); + // Reset form only for create mode + name = ''; + target = 1; + duration = 1; + unit = ''; + increment = 1; + startDate = new Date().toISOString().split('T')[0]; + active = true; + showAdvancedOptions = false; + } + + onComplete?.(); } catch (error) { - console.error('Failed to create habit:', error); + console.error(`Failed to ${isEditMode ? 'update' : 'create'} habit:`, error); } finally { isSubmitting = false; } @@ -66,9 +94,11 @@ - Create a new habit + {isEditMode ? 'Edit habit' : 'Create a new habit'} - Add a new habit to track. Set your target and customize the tracking period. + {isEditMode + ? 'Update your habit settings. Click save when you\'re done.' + : 'Add a new habit to track. Set your target and customize the tracking period.'} @@ -217,15 +247,15 @@ Cancel -