feat: mark list items as bought on tap and record boughtAt
Tapping an item in the list toggles it via a new PATCH /lists/:listId/items/:itemId endpoint. Marking an item as bought stores boughtAt (for future stats) and clearing it back to pending resets the timestamp. The web app updates optimistically and each row is now a real button with aria-pressed.
This commit is contained in:
@@ -3,7 +3,7 @@ import { CheckIcon, ShoppingBasketIcon } from 'lucide-react'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { useItems } from '@/hooks/use-items'
|
||||
import { useItems, useUpdateItem } from '@/hooks/use-items'
|
||||
import { ApiError } from '@/lib/api-client'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
@@ -11,9 +11,10 @@ type ItemListProps = {
|
||||
listId: string
|
||||
}
|
||||
|
||||
/** Items loaded from the API, pending ones first. */
|
||||
/** Items loaded from the API, pending ones first. Tapping an item marks it as bought. */
|
||||
export function ItemList({ listId }: ItemListProps) {
|
||||
const { data: items, isPending, error } = useItems(listId)
|
||||
const updateItem = useUpdateItem(listId)
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
@@ -61,29 +62,37 @@ export function ItemList({ listId }: ItemListProps) {
|
||||
{items.map((item) => (
|
||||
<li key={item.id}>
|
||||
<Card>
|
||||
<CardContent className="flex items-center gap-3 p-3">
|
||||
<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
|
||||
<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"
|
||||
>
|
||||
<CheckIcon className="size-3" />
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
'min-w-0 flex-1 truncate',
|
||||
item.completed && 'text-muted-foreground line-through',
|
||||
<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>
|
||||
)}
|
||||
>
|
||||
{item.name}
|
||||
</span>
|
||||
{item.quantity && (
|
||||
<span className="text-muted-foreground shrink-0 text-sm">{item.quantity}</span>
|
||||
)}
|
||||
</button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user