Files
app/src/pages/Purchases.tsx
T
Frontend Frankie fe5b0e87bd fix: address Chip's review — secure auth, wire TanStack Query, fix UX issues
Must-fix:
- Exclude JWT token from Zustand persist (partialize) to prevent
  localStorage XSS exfiltration — token now lives in memory only
- Wire all pages through TanStack Query hooks (usePurchases, useProduct,
  useProducts, usePriceHistory, useCoupons, usePriceAlerts) with proper
  loading skeletons and error states
- Add mock interceptor in api.ts (VITE_MOCK_API=true) so mock data flows
  through the same fetch path — single flag to switch to live API

Should-fix:
- Wire theme toggle to DOM (dark class on <html>)
- Fix AccountLinking form inputs (controlled with value/onChange)
- Remove unused err in catch blocks (Login, Register)
- Bump remaining min-h-10 touch targets to min-h-12 (48px)

Build: 128KB initial JS, Recharts 498KB lazy chunk. 5/5 tests pass.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-03-18 11:54:06 +00:00

114 lines
3.8 KiB
TypeScript

import { useState, useMemo } from 'react'
import { Link } from 'react-router-dom'
import { usePurchases } from '../hooks/useApi.ts'
import { StoreIcon } from '../components/StoreIcon.tsx'
export function Purchases() {
const { data: purchases = [], isLoading, error } = usePurchases()
const [storeFilter, setStoreFilter] = useState('all')
const stores = useMemo(
() => ['all', ...new Set(purchases.map((p) => p.storeName))],
[purchases],
)
const filtered =
storeFilter === 'all'
? purchases
: purchases.filter((p) => p.storeName === storeFilter)
if (isLoading) {
return (
<div className="animate-pulse">
<div className="h-8 w-48 rounded bg-gray-200" />
<div className="mt-4 flex gap-2">
<div className="h-10 w-24 rounded-full bg-gray-200" />
<div className="h-10 w-20 rounded-full bg-gray-200" />
<div className="h-10 w-20 rounded-full bg-gray-200" />
</div>
<div className="mt-4 space-y-3">
{[1, 2, 3].map((i) => (
<div key={i} className="h-24 rounded-xl bg-gray-200" />
))}
</div>
</div>
)
}
if (error) {
return (
<div className="py-8 text-center">
<p className="text-sm text-red-600">Failed to load purchases.</p>
</div>
)
}
return (
<div>
<h1 className="text-2xl font-bold text-gray-900">Purchase History</h1>
{/* Store filter chips */}
<div className="mt-4 flex gap-2 overflow-x-auto pb-1">
{stores.map((store) => (
<button
key={store}
onClick={() => setStoreFilter(store)}
className={`min-h-12 shrink-0 rounded-full px-4 text-sm font-medium ${
storeFilter === store
? 'bg-brand-blue text-white'
: 'bg-white text-gray-700 shadow-sm'
}`}
>
{store === 'all' ? 'All Stores' : store}
</button>
))}
</div>
{/* Purchase list */}
<div className="mt-4 space-y-3">
{filtered.length === 0 ? (
<div className="rounded-xl bg-white p-6 text-center shadow-sm">
<p className="text-sm text-gray-500">No purchases found for this filter.</p>
</div>
) : (
filtered.map((purchase) => (
<Link
key={purchase.id}
to={`/purchases/${purchase.id}`}
className="block rounded-xl bg-white p-4 shadow-sm active:bg-gray-50"
>
<div className="flex items-center gap-3">
<StoreIcon storeId={purchase.storeId} />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-gray-900">{purchase.storeName}</p>
<p className="text-xs text-gray-500">
{new Date(purchase.date).toLocaleDateString('en-US', {
weekday: 'short',
month: 'short',
day: 'numeric',
year: 'numeric',
})}
</p>
</div>
<div className="text-right">
<p className="text-sm font-semibold text-gray-900">${purchase.total.toFixed(2)}</p>
<p className="text-xs text-gray-500">{purchase.items.length} items</p>
</div>
</div>
{/* Item preview */}
<p className="mt-2 truncate text-xs text-gray-400">
{purchase.items
.slice(0, 3)
.map((i) => i.name)
.join(', ')}
{purchase.items.length > 3 && ` +${purchase.items.length - 3} more`}
</p>
</Link>
))
)}
</div>
</div>
)
}