enable completing past habit

This commit is contained in:
dohsimpson
2025-01-18 19:02:17 -05:00
parent 7ca1744168
commit 2bcbabccc1
11 changed files with 359 additions and 134 deletions

View File

@@ -2,7 +2,8 @@ import { useAtom } from 'jotai'
import { habitsAtom, coinsAtom, settingsAtom } from '@/lib/atoms'
import { addCoins, removeCoins, saveHabitsData } from '@/app/actions/data'
import { Habit } from '@/lib/types'
import { getNowInMilliseconds, getTodayInTimezone, isSameDate, t2d, d2t, getNow, getCompletionsForDate } from '@/lib/utils'
import { DateTime } from 'luxon'
import { getNowInMilliseconds, getTodayInTimezone, isSameDate, t2d, d2t, getNow, getCompletionsForDate, getISODate, d2s } from '@/lib/utils'
import { toast } from '@/hooks/use-toast'
import { ToastAction } from '@/components/ui/toast'
import { Undo2 } from 'lucide-react'
@@ -161,10 +162,80 @@ export function useHabits() {
return updatedHabits
}
const completePastHabit = async (habit: Habit, date: DateTime) => {
const timezone = settings.system.timezone
const dateKey = getISODate({ dateTime: date, timezone })
// Check if already completed on this date
const completionsOnDate = habit.completions.filter(completion =>
isSameDate(t2d({ timestamp: completion, timezone }), date)
).length
const target = habit.targetCompletions || 1
if (completionsOnDate >= target) {
toast({
title: "Already completed",
description: `This habit was already completed on ${d2s({ dateTime: date, timezone, format: 'yyyy-MM-dd' })}.`,
variant: "destructive",
})
return null
}
// Use current time but with the past date
const now = getNow({ timezone })
const completionDateTime = date.set({
hour: now.hour,
minute: now.minute,
second: now.second,
millisecond: now.millisecond
})
const completionTimestamp = d2t({ dateTime: completionDateTime })
const updatedHabit = {
...habit,
completions: [...habit.completions, completionTimestamp]
}
const updatedHabits = habitsData.habits.map(h =>
h.id === habit.id ? updatedHabit : h
)
await saveHabitsData({ habits: updatedHabits })
setHabitsData({ habits: updatedHabits })
// Check if we've now reached the target
const isTargetReached = completionsOnDate + 1 === target
if (isTargetReached) {
const updatedCoins = await addCoins(
habit.coinReward,
`Completed habit: ${habit.name} on ${d2s({ dateTime: date, timezone, format: 'yyyy-MM-dd' })}`,
'HABIT_COMPLETION',
habit.id
)
setCoins(updatedCoins)
}
toast({
title: isTargetReached ? "Habit completed!" : "Progress!",
description: isTargetReached
? `You earned ${habit.coinReward} coins for ${dateKey}.`
: `You've completed ${completionsOnDate + 1}/${target} times on ${dateKey}.`,
action: <ToastAction altText="Undo" className="gap-2" onClick={() => undoComplete(updatedHabit)}>
<Undo2 className="h-4 w-4" />Undo
</ToastAction>
})
return {
updatedHabits,
newBalance: coins.balance,
newTransactions: coins.transactions
}
}
return {
completeHabit,
undoComplete,
saveHabit,
deleteHabit
deleteHabit,
completePastHabit
}
}