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:
36
apps/web/src/components/items/bought-item-list.tsx
Normal file
36
apps/web/src/components/items/bought-item-list.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
import { ChevronDownIcon } from 'lucide-react'
|
||||
|
||||
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'
|
||||
import type { useUpdateItem } from '@/hooks/use-items'
|
||||
|
||||
import { ItemRow } from './item-row'
|
||||
|
||||
type BoughtItemListProps = {
|
||||
items: ListItem[]
|
||||
updateItem: ReturnType<typeof useUpdateItem>
|
||||
}
|
||||
|
||||
/**
|
||||
* Collapsible "Comprados hoy" section below the pending items. Expanded by
|
||||
* default so marking an item visibly moves it down; the user can fold it.
|
||||
* Tapping an item here un-buys it and it returns to the pending list.
|
||||
*/
|
||||
export function BoughtItemList({ items, updateItem }: BoughtItemListProps) {
|
||||
return (
|
||||
<Collapsible defaultOpen>
|
||||
<CollapsibleTrigger className="group flex w-full items-center gap-1.5 py-1 text-left">
|
||||
<ChevronDownIcon className="text-muted-foreground size-4 transition-transform group-data-[state=closed]:-rotate-90" />
|
||||
<span className="text-muted-foreground text-sm font-medium">Comprados hoy</span>
|
||||
<span className="text-muted-foreground text-sm">({items.length})</span>
|
||||
</CollapsibleTrigger>
|
||||
<CollapsibleContent>
|
||||
<ul className="flex flex-col gap-2 pt-2">
|
||||
{items.map((item) => (
|
||||
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
||||
))}
|
||||
</ul>
|
||||
</CollapsibleContent>
|
||||
</Collapsible>
|
||||
)
|
||||
}
|
||||
@@ -1,20 +1,30 @@
|
||||
import { CheckIcon, ShoppingBasketIcon } from 'lucide-react'
|
||||
import { ShoppingBasketIcon } from 'lucide-react'
|
||||
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { useItems, useUpdateItem } from '@/hooks/use-items'
|
||||
import { useToday } from '@/hooks/use-today'
|
||||
import { ApiError } from '@/lib/api-client'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { splitItems } from '@/lib/items'
|
||||
|
||||
import { BoughtItemList } from './bought-item-list'
|
||||
import { ItemRow } from './item-row'
|
||||
|
||||
type ItemListProps = {
|
||||
listId: string
|
||||
}
|
||||
|
||||
/** Items loaded from the API, pending ones first. Tapping an item marks it as bought. */
|
||||
/**
|
||||
* Items loaded from the API, pending ones first. Tapping an item marks it as
|
||||
* bought; bought items move to a collapsible "Comprados hoy" section below
|
||||
* and are only shown on the day they were bought.
|
||||
*/
|
||||
export function ItemList({ listId }: ItemListProps) {
|
||||
const { data: items, isPending, error } = useItems(listId)
|
||||
const updateItem = useUpdateItem(listId)
|
||||
const today = useToday()
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
@@ -39,7 +49,9 @@ export function ItemList({ listId }: ItemListProps) {
|
||||
)
|
||||
}
|
||||
|
||||
if (items.length === 0) {
|
||||
const { pending, boughtToday } = splitItems(items, today)
|
||||
|
||||
if (pending.length === 0 && boughtToday.length === 0) {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center gap-3 py-10 text-center">
|
||||
@@ -58,45 +70,21 @@ export function ItemList({ listId }: ItemListProps) {
|
||||
}
|
||||
|
||||
return (
|
||||
<ul className="flex flex-col gap-2">
|
||||
{items.map((item) => (
|
||||
<li key={item.id}>
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={item.completed}
|
||||
disabled={updateItem.isPending}
|
||||
onClick={() => updateItem.mutate({ itemId: item.id, completed: !item.completed })}
|
||||
className="flex w-full cursor-pointer items-center gap-3 p-3 text-left disabled:cursor-default disabled:opacity-60"
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'flex size-5 shrink-0 items-center justify-center rounded-full border',
|
||||
item.completed
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'border-muted-foreground/40 text-transparent',
|
||||
)}
|
||||
aria-hidden
|
||||
>
|
||||
<CheckIcon className="size-3" />
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
'min-w-0 flex-1 truncate',
|
||||
item.completed && 'text-muted-foreground line-through',
|
||||
)}
|
||||
>
|
||||
{item.name}
|
||||
</span>
|
||||
{item.quantity && (
|
||||
<span className="text-muted-foreground shrink-0 text-sm">{item.quantity}</span>
|
||||
)}
|
||||
</button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="flex flex-col gap-2">
|
||||
{pending.length > 0 && (
|
||||
<ul className="flex flex-col gap-2">
|
||||
{pending.map((item) => (
|
||||
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{boughtToday.length > 0 && (
|
||||
<>
|
||||
{pending.length > 0 && <Separator className="mt-2" />}
|
||||
<BoughtItemList items={boughtToday} updateItem={updateItem} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
53
apps/web/src/components/items/item-row.tsx
Normal file
53
apps/web/src/components/items/item-row.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { ListItem } from '@superlist/shared'
|
||||
import { CheckIcon } from 'lucide-react'
|
||||
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import type { useUpdateItem } from '@/hooks/use-items'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
type ItemRowProps = {
|
||||
item: ListItem
|
||||
updateItem: ReturnType<typeof useUpdateItem>
|
||||
}
|
||||
|
||||
/** A single item row: tapping it toggles its bought state. */
|
||||
export function ItemRow({ item, updateItem }: ItemRowProps) {
|
||||
return (
|
||||
<li>
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={item.completed}
|
||||
disabled={updateItem.isPending}
|
||||
onClick={() => updateItem.mutate({ itemId: item.id, completed: !item.completed })}
|
||||
className="flex w-full cursor-pointer items-center gap-3 p-3 text-left disabled:cursor-default disabled:opacity-60"
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'flex size-5 shrink-0 items-center justify-center rounded-full border',
|
||||
item.completed
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'border-muted-foreground/40 text-transparent',
|
||||
)}
|
||||
aria-hidden
|
||||
>
|
||||
<CheckIcon className="size-3" />
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
'min-w-0 flex-1 truncate',
|
||||
item.completed && 'text-muted-foreground line-through',
|
||||
)}
|
||||
>
|
||||
{item.name}
|
||||
</span>
|
||||
{item.quantity && (
|
||||
<span className="text-muted-foreground shrink-0 text-sm">{item.quantity}</span>
|
||||
)}
|
||||
</button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
19
apps/web/src/components/ui/collapsible.tsx
Normal file
19
apps/web/src/components/ui/collapsible.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Collapsible as CollapsiblePrimitive } from 'radix-ui'
|
||||
|
||||
function Collapsible({ ...props }: React.ComponentProps<typeof CollapsiblePrimitive.Root>) {
|
||||
return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} />
|
||||
}
|
||||
|
||||
function CollapsibleTrigger({
|
||||
...props
|
||||
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleTrigger>) {
|
||||
return <CollapsiblePrimitive.CollapsibleTrigger data-slot="collapsible-trigger" {...props} />
|
||||
}
|
||||
|
||||
function CollapsibleContent({
|
||||
...props
|
||||
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleContent>) {
|
||||
return <CollapsiblePrimitive.CollapsibleContent data-slot="collapsible-content" {...props} />
|
||||
}
|
||||
|
||||
export { Collapsible, CollapsibleContent, CollapsibleTrigger }
|
||||
Reference in New Issue
Block a user