Files
cartsnitch-fork-test/src/pages/StoreComparison.tsx
T
Frontend Frankie d0185fd93d 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

108 lines
3.8 KiB
TypeScript

import { useParams, Link } from 'react-router-dom'
import { useProduct } from '../hooks/useApi.ts'
import { StoreIcon } from '../components/StoreIcon.tsx'
export function StoreComparison() {
const { productId } = useParams<{ productId: string }>()
const { data: product, isLoading } = useProduct(productId ?? '')
if (isLoading) {
return (
<div className="animate-pulse">
<div className="h-4 w-20 rounded bg-gray-200" />
<div className="mt-4 h-8 w-48 rounded bg-gray-200" />
<div className="mt-4 space-y-3">
{[1, 2, 3].map((i) => (
<div key={i} className="h-20 rounded-xl bg-gray-200" />
))}
</div>
</div>
)
}
if (!product) {
return (
<div className="py-8 text-center">
<p className="text-sm text-gray-500">Product not found.</p>
<Link to="/products" className="mt-4 inline-block text-sm text-brand-blue">
Back to products
</Link>
</div>
)
}
const sorted = product.prices.slice().sort((a, b) => a.price - b.price)
const lowestPrice = sorted[0]?.price ?? 0
const savings = sorted.length > 1 ? sorted[sorted.length - 1].price - sorted[0].price : 0
return (
<div>
{/* Back link */}
<Link to={`/products/${product.id}`} className="inline-flex items-center gap-1 text-sm text-brand-blue">
<svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M15 19l-7-7 7-7" />
</svg>
{product.name}
</Link>
<h1 className="mt-4 text-2xl font-bold text-gray-900">Store Comparison</h1>
<p className="mt-1 text-sm text-gray-500">{product.name} &middot; {product.brand}</p>
{/* Savings banner */}
{savings > 0 && (
<div className="mt-4 rounded-xl bg-green-50 p-4">
<p className="text-sm font-semibold text-green-800">
Save ${savings.toFixed(2)} by shopping at {sorted[0].storeName}
</p>
</div>
)}
{/* Store comparison cards */}
<div className="mt-4 space-y-3">
{sorted.map((pp, idx) => (
<div
key={pp.storeId}
className={`rounded-xl p-4 shadow-sm ${
idx === 0 ? 'border-2 border-green-400 bg-white' : 'bg-white'
}`}
>
<div className="flex items-center gap-3">
<StoreIcon storeId={pp.storeId} />
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-gray-900">{pp.storeName}</p>
<p className="text-xs text-gray-500">
Updated{' '}
{new Date(pp.lastUpdated).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
})}
</p>
</div>
<div className="text-right">
<p
className={`text-lg font-bold ${
pp.price === lowestPrice ? 'text-green-700' : 'text-gray-900'
}`}
>
${pp.price.toFixed(2)}
</p>
{pp.price === lowestPrice ? (
<span className="text-xs font-medium text-green-600">Best price</span>
) : (
<span className="text-xs text-gray-400">
+${(pp.price - lowestPrice).toFixed(2)}
</span>
)}
</div>
</div>
</div>
))}
</div>
<p className="mt-6 text-center text-xs text-gray-400">
Prices last verified from store loyalty card data. Map view coming soon.
</p>
</div>
)
}