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:
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