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 dateStr = formatDate(date);
// Optimistically update completions for instant UI
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));
const user = await getAuthUser();
if (user) {
// Optimistically update completions for instant UI
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));
}
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 (

View File

@@ -69,19 +69,26 @@ const MiniGrid = ({ habit, onUpdate }) => {
const dateStr = formatDate(date);
const isTodayCell = isToday(date);
const wasCompleted = habit.completions.includes(dateStr);
// Optimistically update completions for instant UI
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));
const user = await getAuthUser();
if (user) {
// Optimistically update completions for instant UI
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));
}
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
if (isTodayCell && !wasCompleted) {
try {