fix duplicates

This commit is contained in:
2025-10-17 23:20:23 +02:00
parent 0c5e75f726
commit 2b0d8a4a73

View File

@@ -51,7 +51,7 @@ const AddEditHabitPage = () => {
} }
}, [id, isEdit, navigate, toast]); }, [id, isEdit, navigate, toast]);
const handleSubmit = (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
if (!name.trim()) { if (!name.trim()) {
@@ -78,8 +78,7 @@ const AddEditHabitPage = () => {
description: "Your habit has been updated successfully.", description: "Your habit has been updated successfully.",
}); });
} else { } else {
// Add to localStorage for instant UI // Single source of truth: delegate to datastore; it will handle local or remote as needed
const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
const newHabit = { const newHabit = {
id: generateUUID(), id: generateUUID(),
name: name.trim(), name: name.trim(),
@@ -90,11 +89,8 @@ const AddEditHabitPage = () => {
longestStreak: 0, longestStreak: 0,
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
sortOrder: habits.length,
}; };
habits.push(newHabit); await saveHabit(newHabit);
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
saveHabit(newHabit); // background sync
toast({ toast({
title: "✅ Habit created", title: "✅ Habit created",
description: "Your new habit is ready to track!", description: "Your new habit is ready to track!",