feat: show bought items in a collapsible section for the current day only
Some checks failed
CI / Verify (push) Has been cancelled
Some checks failed
CI / Verify (push) Has been cancelled
Bought items now move down to a 'Comprados hoy' collapsible section (expanded by default) below the pending ones, and are only visible on the local day they were bought. Items bought on previous days stay completed in the database but are hidden from the list.
This commit is contained in:
28
apps/web/src/lib/dates.test.ts
Normal file
28
apps/web/src/lib/dates.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { isSameLocalDay } from './dates'
|
||||
|
||||
describe('isSameLocalDay', () => {
|
||||
it('matches two moments of the same local day', () => {
|
||||
const morning = new Date(2026, 9, 15, 8, 30)
|
||||
const night = new Date(2026, 9, 15, 23, 59)
|
||||
|
||||
expect(isSameLocalDay(morning, night)).toBe(true)
|
||||
})
|
||||
|
||||
it('does not match moments across local midnight', () => {
|
||||
const justBefore = new Date(2026, 9, 15, 23, 59)
|
||||
const justAfter = new Date(2026, 9, 16, 0, 1)
|
||||
|
||||
expect(isSameLocalDay(justBefore, justAfter)).toBe(false)
|
||||
})
|
||||
|
||||
it('compares in the local timezone, not in UTC', () => {
|
||||
// 23:30 local on Oct 15th is already Oct 16th in UTC for UTC-negative
|
||||
// timezones, so a naive UTC comparison would report different days.
|
||||
const localNight = new Date(2026, 9, 15, 23, 30)
|
||||
const nextLocalMidnight = new Date(2026, 9, 16, 0, 0)
|
||||
|
||||
expect(isSameLocalDay(localNight, nextLocalMidnight)).toBe(false)
|
||||
})
|
||||
})
|
||||
14
apps/web/src/lib/dates.ts
Normal file
14
apps/web/src/lib/dates.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Local-day helpers. A shopping day belongs to the user's timezone (the
|
||||
* browser's), which the API cannot know, so "bought today" is decided here
|
||||
* and never on the server.
|
||||
*/
|
||||
|
||||
/** True when both moments fall on the same calendar day in the local timezone. */
|
||||
export function isSameLocalDay(a: Date, b: Date): boolean {
|
||||
return (
|
||||
a.getFullYear() === b.getFullYear() &&
|
||||
a.getMonth() === b.getMonth() &&
|
||||
a.getDate() === b.getDate()
|
||||
)
|
||||
}
|
||||
87
apps/web/src/lib/items.test.ts
Normal file
87
apps/web/src/lib/items.test.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { splitItems } from './items'
|
||||
|
||||
function item(overrides: Partial<ListItem>): ListItem {
|
||||
return {
|
||||
id: 'item-1',
|
||||
listId: 'list-1',
|
||||
name: 'Leche',
|
||||
completed: false,
|
||||
boughtAt: null,
|
||||
quantity: null,
|
||||
notes: null,
|
||||
order: 0,
|
||||
createdAt: '2026-10-15T10:00:00.000Z',
|
||||
updatedAt: '2026-10-15T10:00:00.000Z',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
const NOW = new Date(2026, 9, 15, 18, 0)
|
||||
|
||||
describe('splitItems', () => {
|
||||
it('keeps pending items untouched, in API order', () => {
|
||||
const first = item({ id: 'a', order: 1 })
|
||||
const second = item({ id: 'b', order: 2 })
|
||||
|
||||
const { pending, boughtToday } = splitItems([first, second], NOW)
|
||||
|
||||
expect(pending).toEqual([first, second])
|
||||
expect(boughtToday).toEqual([])
|
||||
})
|
||||
|
||||
it('moves items bought today to boughtToday, most recent first', () => {
|
||||
const boughtEarlier = item({
|
||||
id: 'a',
|
||||
completed: true,
|
||||
boughtAt: new Date(2026, 9, 15, 9, 0).toISOString(),
|
||||
})
|
||||
const boughtJustNow = item({
|
||||
id: 'b',
|
||||
completed: true,
|
||||
boughtAt: new Date(2026, 9, 15, 17, 30).toISOString(),
|
||||
})
|
||||
|
||||
const { pending, boughtToday } = splitItems([boughtEarlier, boughtJustNow], NOW)
|
||||
|
||||
expect(pending).toEqual([])
|
||||
expect(boughtToday.map((i) => i.id)).toEqual(['b', 'a'])
|
||||
})
|
||||
|
||||
it('hides items bought on a previous local day', () => {
|
||||
const boughtYesterday = item({
|
||||
completed: true,
|
||||
boughtAt: new Date(2026, 9, 14, 23, 30).toISOString(),
|
||||
})
|
||||
|
||||
const { pending, boughtToday } = splitItems([boughtYesterday], NOW)
|
||||
|
||||
expect(pending).toEqual([])
|
||||
expect(boughtToday).toEqual([])
|
||||
})
|
||||
|
||||
it('compares in the local timezone, not in UTC', () => {
|
||||
// 23:30 local on Oct 15th already belongs to Oct 16th in UTC for
|
||||
// UTC-negative timezones; the item must still count as bought today.
|
||||
const boughtLateAtNight = item({
|
||||
completed: true,
|
||||
boughtAt: new Date(2026, 9, 15, 23, 30).toISOString(),
|
||||
})
|
||||
|
||||
const lateNightNow = new Date(2026, 9, 15, 23, 45)
|
||||
const { boughtToday } = splitItems([boughtLateAtNight], lateNightNow)
|
||||
|
||||
expect(boughtToday).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('hides completed items without boughtAt', () => {
|
||||
const legacy = item({ completed: true, boughtAt: null })
|
||||
|
||||
const { pending, boughtToday } = splitItems([legacy], NOW)
|
||||
|
||||
expect(pending).toEqual([])
|
||||
expect(boughtToday).toEqual([])
|
||||
})
|
||||
})
|
||||
40
apps/web/src/lib/items.ts
Normal file
40
apps/web/src/lib/items.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
|
||||
import { isSameLocalDay } from './dates'
|
||||
|
||||
export type SplitItems = {
|
||||
/** Items still to buy, in the API's order (by `order`). */
|
||||
pending: ListItem[]
|
||||
/** Items bought during the current local day, most recent first. */
|
||||
boughtToday: ListItem[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a list's items into the two groups the UI renders: pending items
|
||||
* and items bought today. Items bought on a previous local day are dropped
|
||||
* entirely — they stay `completed` in the database but are only shown on
|
||||
* the day they were bought.
|
||||
*/
|
||||
export function splitItems(items: ListItem[], now: Date): SplitItems {
|
||||
const pending: ListItem[] = []
|
||||
const boughtToday: ListItem[] = []
|
||||
|
||||
for (const item of items) {
|
||||
if (!item.completed) {
|
||||
pending.push(item)
|
||||
continue
|
||||
}
|
||||
|
||||
// A completed item without `boughtAt` is legacy data with no known
|
||||
// purchase day, so it is not considered bought today.
|
||||
if (item.boughtAt && isSameLocalDay(new Date(item.boughtAt), now)) {
|
||||
boughtToday.push(item)
|
||||
}
|
||||
}
|
||||
|
||||
boughtToday.sort(
|
||||
(a, b) => new Date(b.boughtAt ?? 0).getTime() - new Date(a.boughtAt ?? 0).getTime(),
|
||||
)
|
||||
|
||||
return { pending, boughtToday }
|
||||
}
|
||||
Reference in New Issue
Block a user