This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||||
import { Activity, ArchiveRestore, Check, Download, FileJson, GripVertical, LogOut, Trash2, X } from 'lucide-vue-next'
|
import { Activity, ArchiveRestore, Check, Download, FileJson, GripVertical, LogOut, Trash2, X } from 'lucide-vue-next'
|
||||||
import { moveItemWithinScope } from './lib/task-utils'
|
import { moveItemWithinScope } from './lib/task-utils'
|
||||||
import { dateKey, habitButtonValue, isHabitComplete, isHabitScheduledToday, mergePage, nextHabitSwipeValue, previousHabitSwipeValue, readHabitGridCache, shouldToggleRowSwipe, writeHabitGridCache } from './lib/mvp-utils'
|
import { dateKey, habitButtonNotice, habitButtonValue, isHabitComplete, isHabitScheduledToday, mergePage, nextHabitSwipeValue, previousHabitSwipeValue, readHabitGridCache, shouldToggleRowSwipe, writeHabitGridCache } 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'
|
||||||
@@ -239,7 +239,7 @@ async function toggleHabitFromButton(h: Habit) {
|
|||||||
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 }) })
|
||||||
emit('notice', next > Number(current ?? 0) ? '已记录一次 🎉' : '已减少一次')
|
emit('notice', habitButtonNotice(h.kind, previous, next))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setLocalHabitValue(h, previous)
|
setLocalHabitValue(h, previous)
|
||||||
error.value = e instanceof Error ? e.message : '请求失败'
|
error.value = e instanceof Error ? e.message : '请求失败'
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, nextTotalAfterLocalTaskAdd, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, readCountdownCache, readHabitGridCache, shouldToggleRowSwipe, writeCountdownCache, writeHabitGridCache } from './mvp-utils'
|
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonNotice, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, nextTotalAfterLocalTaskAdd, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, readCountdownCache, readHabitGridCache, shouldToggleRowSwipe, writeCountdownCache, writeHabitGridCache } 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', () => {
|
||||||
@@ -70,6 +70,13 @@ describe('MVP view utilities', () => {
|
|||||||
expect(habitButtonValue('boolean', 1, 1)).toBe(0)
|
expect(habitButtonValue('boolean', 1, 1)).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('uses reset wording when a completed numeric habit button returns to zero', () => {
|
||||||
|
expect(habitButtonNotice('numeric', 3, 0)).toBe('今日进度已重置')
|
||||||
|
expect(habitButtonNotice('numeric', 2, 3)).toBe('已记录一次 🎉')
|
||||||
|
expect(habitButtonNotice('numeric', 2, 1)).toBe('已减少一次')
|
||||||
|
expect(habitButtonNotice('boolean', 1, 0)).toBe('已取消完成')
|
||||||
|
})
|
||||||
|
|
||||||
it('reuses the current week habit grid while refreshing in the background', () => {
|
it('reuses the current week habit grid while refreshing in the background', () => {
|
||||||
const habits = [{ id: 'habit-1', name: '喝水' }]
|
const habits = [{ id: 'habit-1', name: '喝水' }]
|
||||||
writeHabitGridCache('2026-09-07', habits)
|
writeHabitGridCache('2026-09-07', habits)
|
||||||
|
|||||||
@@ -75,6 +75,12 @@ export function habitButtonValue(kind: string | undefined, current: number | boo
|
|||||||
return nextHabitSwipeValue(kind, current, target)
|
return nextHabitSwipeValue(kind, current, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function habitButtonNotice(kind: string | undefined, previous: number | boolean | undefined, next: number | boolean) {
|
||||||
|
if (kind === 'numeric' && Number(previous ?? 0) > 0 && Number(next) === 0) return '今日进度已重置'
|
||||||
|
if (kind !== 'numeric' && Number(previous ?? 0) > 0 && Number(next) === 0) return '已取消完成'
|
||||||
|
return Number(next) > Number(previous ?? 0) ? '已记录一次 🎉' : '已减少一次'
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user