bug fixing

This commit is contained in:
2025-10-18 14:24:45 +02:00
parent 3dd34f4f17
commit 39f7bbd96f
2 changed files with 38 additions and 24 deletions

View File

@@ -40,19 +40,26 @@ const HabitGrid = ({ habit, onUpdate, fullView = false }) => {
const handleCellClick = async (date) => { const handleCellClick = async (date) => {
const dateStr = formatDate(date); const dateStr = formatDate(date);
// Optimistically update completions for instant UI const user = await getAuthUser();
const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); if (user) {
const idx = habits.findIndex(h => h.id === habit.id); // Optimistically update completions for instant UI
if (idx !== -1) { const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; const idx = habits.findIndex(h => h.id === habit.id);
const cidx = completions.indexOf(dateStr); if (idx !== -1) {
if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : [];
habits[idx].completions = completions; const cidx = completions.indexOf(dateStr);
localStorage.setItem('habitgrid_data', JSON.stringify(habits)); if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr);
habits[idx].completions = completions;
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
}
onUpdate();
// Sync in background
toggleCompletion(habit.id, dateStr);
} else {
// Local-only: just call toggleCompletion, then update UI
await toggleCompletion(habit.id, dateStr);
onUpdate();
} }
onUpdate();
// Sync in background
toggleCompletion(habit.id, dateStr);
}; };
return ( return (

View File

@@ -69,19 +69,26 @@ const MiniGrid = ({ habit, onUpdate }) => {
const dateStr = formatDate(date); const dateStr = formatDate(date);
const isTodayCell = isToday(date); const isTodayCell = isToday(date);
const wasCompleted = habit.completions.includes(dateStr); const wasCompleted = habit.completions.includes(dateStr);
// Optimistically update completions for instant UI const user = await getAuthUser();
const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]'); if (user) {
const idx = habits.findIndex(h => h.id === habit.id); // Optimistically update completions for instant UI
if (idx !== -1) { const habits = JSON.parse(localStorage.getItem('habitgrid_data') || '[]');
const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : []; const idx = habits.findIndex(h => h.id === habit.id);
const cidx = completions.indexOf(dateStr); if (idx !== -1) {
if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr); const completions = Array.isArray(habits[idx].completions) ? [...habits[idx].completions] : [];
habits[idx].completions = completions; const cidx = completions.indexOf(dateStr);
localStorage.setItem('habitgrid_data', JSON.stringify(habits)); if (cidx > -1) completions.splice(cidx, 1); else completions.push(dateStr);
habits[idx].completions = completions;
localStorage.setItem('habitgrid_data', JSON.stringify(habits));
}
onUpdate();
// Sync in background
toggleCompletion(habit.id, dateStr);
} else {
// Local-only: just call toggleCompletion, then update UI
await toggleCompletion(habit.id, dateStr);
onUpdate();
} }
onUpdate();
// Sync in background
toggleCompletion(habit.id, dateStr);
// Only show encouragement toast if validating (adding) today's dot // Only show encouragement toast if validating (adding) today's dot
if (isTodayCell && !wasCompleted) { if (isTodayCell && !wasCompleted) {
try { try {