Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | "use client"; import { useState, useEffect, useCallback } from "react"; import Image from "next/image"; import { Button, Card } from "@/components/ui"; import { Icon } from "@/components/ui/icons"; import { clientLogger } from "@/lib/logging/clientLogger"; interface Product { id: number; title: string; price: number; discountedPrice: number; images: { url: string; thumbnailUrl: string }[]; } interface HeroProductSelectorProps { onSelect: (productId: number) => void; onClose: () => void; title?: string; } export default function HeroProductSelector({ onSelect, onClose, title = "Select Product"}: HeroProductSelectorProps) { const [products, setProducts] = useState<Product[]>([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(""); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const fetchProducts = useCallback(async (searchTerm: string, pageNum: number) => { try { setLoading(true); const params = new URLSearchParams({ page: pageNum.toString(), limit: "12"}); if (searchTerm) { params.set("search", searchTerm); } const response = await fetch(`/api/admin/products?${params}`); const data = await response.json(); if (data.success && data.data) { if (pageNum === 1) { setProducts(data.data); } else { setProducts((prev) => [...prev, ...data.data]); } setHasMore(data.data.length === 12); } } catch (error) { clientLogger.error("Error fetching products", error instanceof Error ? error : new Error(String(error))); } finally { setLoading(false); } }, []); useEffect(() => { fetchProducts(search, 1); setPage(1); }, [search, fetchProducts]); const handleLoadMore = () => { const nextPage = page + 1; setPage(nextPage); fetchProducts(search, nextPage); }; const formatPrice = (price: number) => { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD"}).format(price); }; return ( <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"> <Card className="w-full max-w-3xl max-h-[80vh] flex flex-col"> {/* Header */} <div className="flex items-center justify-between p-4 border-b dark:border-gray-700"> <h2 className="text-lg font-semibold text-gray-900 dark:text-white"> {title} </h2> <button onClick={onClose} className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200" > <Icon name="close" className="w-5 h-5" /> </button> </div> {/* Search */} <div className="p-4 border-b dark:border-gray-700"> <div className="relative"> <Icon name="search" className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> <input type="text" placeholder="Search products..." value={search} onChange={(e) => setSearch(e.target.value)} className="w-full pl-10 pr-4 py-2 border rounded-lg dark:bg-gray-800 dark:border-gray-600 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-transparent" /> </div> </div> {/* Products Grid */} <div className="flex-1 overflow-y-auto p-4"> {loading && products.length === 0 ? ( <div className="grid grid-cols-2 md:grid-cols-3 gap-4"> {[...Array(6)].map((_, i) => ( <div key={i} className="animate-pulse"> <div className="aspect-square bg-gray-200 dark:bg-gray-700 rounded-lg mb-2"></div> <div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-3/4"></div> <div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-1/2 mt-1"></div> </div> ))} </div> ) : products.length === 0 ? ( <div className="text-center py-8"> <Icon name="package" className="w-12 h-12 mx-auto text-gray-400 mb-4" /> <p className="text-gray-500 dark:text-gray-400"> {search ? "No products found matching your search" : "No products available"} </p> </div> ) : ( <> <div className="grid grid-cols-2 md:grid-cols-3 gap-4"> {products.map((product) => ( <button key={product.id} onClick={() => onSelect(product.id)} className="text-left group p-3 rounded-lg border border-gray-200 dark:border-gray-700 hover:border-blue-500 dark:hover:border-blue-400 hover:shadow-md transition-all bg-white dark:bg-gray-800" > {/* Image */} <div className="aspect-square relative rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700 mb-3"> {product.images[0] ? ( <Image src={product.images[0].thumbnailUrl || product.images[0].url} alt={product.title} fill sizes="(max-width: 640px) 50vw, (max-width: 1024px) 33vw, 200px" className="object-cover group-hover:scale-105 transition-transform" /> ) : ( <div className="w-full h-full flex items-center justify-center"> <Icon name="image" className="w-8 h-8 text-gray-400" /> </div> )} </div> {/* Info */} <h3 className="font-medium text-gray-900 dark:text-white truncate text-sm"> {product.title} </h3> <div className="flex items-center gap-2 mt-1"> {product.discountedPrice < product.price ? ( <> <span className="text-blue-600 dark:text-blue-400 font-semibold text-sm"> {formatPrice(product.discountedPrice)} </span> <span className="text-xs text-gray-400 line-through"> {formatPrice(product.price)} </span> </> ) : ( <span className="text-blue-600 dark:text-blue-400 font-semibold text-sm"> {formatPrice(product.price)} </span> )} </div> </button> ))} </div> {/* Load More */} {hasMore && ( <div className="text-center mt-6"> <Button variant="outline" onClick={handleLoadMore} disabled={loading} > {loading ? ( <> <Icon name="loader" className="w-4 h-4 mr-2 animate-spin" /> Loading... </> ) : ( "Load More" )} </Button> </div> )} </> )} </div> {/* Footer */} <div className="flex justify-end gap-2 p-4 border-t dark:border-gray-700"> <Button variant="outline" onClick={onClose}> Cancel </Button> </div> </Card> </div> ); } |