fix: reset completed numeric habits from check button
ci / docker (push) Successful in 7m21s

This commit is contained in:
2026-09-07 17:00:43 +08:00
parent a1cb780774
commit 6ca613f5fa
3 changed files with 15 additions and 5 deletions
+2 -4
View File
@@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue' import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { Activity, ArchiveRestore, Check, Download, FileJson, LogOut, RefreshCw, Trash2, Upload, X } from 'lucide-vue-next' import { Activity, ArchiveRestore, Check, Download, FileJson, LogOut, RefreshCw, Trash2, Upload, X } from 'lucide-vue-next'
import { dateKey, isHabitComplete, isHabitScheduledToday, mergePage, nextHabitSwipeValue, previousHabitSwipeValue, shouldToggleRowSwipe } from './lib/mvp-utils' import { dateKey, habitButtonValue, isHabitComplete, isHabitScheduledToday, mergePage, nextHabitSwipeValue, previousHabitSwipeValue, shouldToggleRowSwipe } from './lib/mvp-utils'
import { csrfHeader } from './lib/csrf' import { csrfHeader } from './lib/csrf'
type View = 'habits' | 'today-habits' | 'settings' type View = 'habits' | 'today-habits' | 'settings'
@@ -187,9 +187,7 @@ function cancelHabitSwipe(h?: Habit) {
async function toggleHabitFromButton(h: Habit) { async function toggleHabitFromButton(h: Habit) {
const current = logFor(h, todayKey.value)?.value const current = logFor(h, todayKey.value)?.value
const previous = current ?? 0 const previous = current ?? 0
const next = isDone(h, todayKey.value) const next = habitButtonValue(h.kind, current, h.target ?? 1)
? previousHabitSwipeValue(h.kind, current)
: nextHabitSwipeValue(h.kind, current, h.target ?? 1)
setLocalHabitValue(h, next) setLocalHabitValue(h, next)
try { try {
await request(`/habits/${h.id}/logs/${todayKey.value}`, { method: 'PUT', body: JSON.stringify({ value: next }) }) await request(`/habits/${h.id}/logs/${todayKey.value}`, { method: 'PUT', body: JSON.stringify({ value: next }) })
+8 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, shouldToggleRowSwipe } from './mvp-utils' import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, shouldToggleRowSwipe } from './mvp-utils'
describe('MVP view utilities', () => { describe('MVP view utilities', () => {
it('formats a local date as YYYY-MM-DD', () => { it('formats a local date as YYYY-MM-DD', () => {
@@ -63,6 +63,13 @@ describe('MVP view utilities', () => {
expect(previousHabitSwipeValue('numeric', 0)).toBe(0) expect(previousHabitSwipeValue('numeric', 0)).toBe(0)
}) })
it('resets a completed numeric habit when its round button is clicked again', () => {
expect(habitButtonValue('numeric', 2, 3)).toBe(3)
expect(habitButtonValue('numeric', 3, 3)).toBe(0)
expect(habitButtonValue('boolean', 0, 1)).toBe(1)
expect(habitButtonValue('boolean', 1, 1)).toBe(0)
})
it('renders countdown labels from date-only day values', () => { it('renders countdown labels from date-only day values', () => {
expect(countdownDayText(8)).toBe('还有 8 天') expect(countdownDayText(8)).toBe('还有 8 天')
expect(countdownDayText(0)).toBe('就是今天') expect(countdownDayText(0)).toBe('就是今天')
+5
View File
@@ -70,6 +70,11 @@ export function previousHabitSwipeValue(kind: string | undefined, current: numbe
return 0 return 0
} }
export function habitButtonValue(kind: string | undefined, current: number | boolean | undefined, target = 1) {
if (isHabitComplete(kind, current, target)) return 0
return nextHabitSwipeValue(kind, current, target)
}
export function numericHabitInputValue(input: number | string | undefined) { export function numericHabitInputValue(input: number | string | undefined) {
if (input === undefined || input === '') return null if (input === undefined || input === '') return null
const value = Number(input) const value = Number(input)