This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
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'
|
||||
|
||||
type View = 'habits' | 'today-habits' | 'settings'
|
||||
@@ -187,9 +187,7 @@ function cancelHabitSwipe(h?: Habit) {
|
||||
async function toggleHabitFromButton(h: Habit) {
|
||||
const current = logFor(h, todayKey.value)?.value
|
||||
const previous = current ?? 0
|
||||
const next = isDone(h, todayKey.value)
|
||||
? previousHabitSwipeValue(h.kind, current)
|
||||
: nextHabitSwipeValue(h.kind, current, h.target ?? 1)
|
||||
const next = habitButtonValue(h.kind, current, h.target ?? 1)
|
||||
setLocalHabitValue(h, next)
|
||||
try {
|
||||
await request(`/habits/${h.id}/logs/${todayKey.value}`, { method: 'PUT', body: JSON.stringify({ value: next }) })
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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', () => {
|
||||
it('formats a local date as YYYY-MM-DD', () => {
|
||||
@@ -63,6 +63,13 @@ describe('MVP view utilities', () => {
|
||||
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', () => {
|
||||
expect(countdownDayText(8)).toBe('还有 8 天')
|
||||
expect(countdownDayText(0)).toBe('就是今天')
|
||||
|
||||
@@ -70,6 +70,11 @@ export function previousHabitSwipeValue(kind: string | undefined, current: numbe
|
||||
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) {
|
||||
if (input === undefined || input === '') return null
|
||||
const value = Number(input)
|
||||
|
||||
Reference in New Issue
Block a user