update toggleColpletion

This commit is contained in:
2025-10-18 14:20:23 +02:00
parent 85db9f5efa
commit 158e3ae342
2 changed files with 1 additions and 26 deletions

View File

@@ -40,19 +40,7 @@ const HabitGrid = ({ habit, onUpdate, fullView = false }) => {
const handleCellClick = async (date) => {
const dateStr = formatDate(date);
// Only do optimistic localStorage write if logged in (remote-first). In local-only mode, let datastore handle it to avoid double-toggle.
const user = await getAuthUser();
if (user) {
const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
const idx = habits.findIndex(h => h.id === habit.id);
if (idx !== -1) {
const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : [];
const cidx = completions.indexOf(dateStr);
if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr);
habits[idx].completions = completions;
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
}
}
// Always call toggleCompletion (handles local/remote), then update UI
await toggleCompletion(habit.id, dateStr);
onUpdate();
};

View File

@@ -69,19 +69,6 @@ const MiniGrid = ({ habit, onUpdate }) => {
const dateStr = formatDate(date);
const isTodayCell = isToday(date);
const wasCompleted = habit.completions.includes(dateStr);
// Only optimistic write if logged in; in local-only mode, datastore handles it to avoid double-toggle
const user = await getAuthUser();
if (user) {
const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
const idx = habits.findIndex(h => h.id === habit.id);
if (idx !== -1) {
const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : [];
const cidx = completions.indexOf(dateStr);
if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr);
habits[idx].completions = completions;
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
}
}
await toggleCompletion(habit.id, dateStr);
onUpdate();
// Only show encouragement toast if validating (adding) today's dot