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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import React, { useState } from "react";
import { ProductCard } from "@/components/features/product";
import { Select } from "@/components/ui/Select";
import { useProducts } from "@/hooks/useProducts";
import { Icon } from "@/components/ui/icons";
import { ProductGridSkeleton } from "@/components/ui/Skeleton";
import ErrorMessage from "@/components/ui/ErrorMessage";
import { Button } from "@/components/ui";
const ShopWithoutSidebar = () => {
const [productStyle, setProductStyle] = useState("grid");
const [sortOption, setSortOption] = useState("0");
const [currentPage, setCurrentPage] = useState(1);
const options = [
{ label: "Latest Products", value: "0" },
{ label: "Best Selling", value: "1" },
{ label: "Old Products", value: "2" },
];
// Map sort option to API filters
const getFilters = () => {
const filters: { page: number; limit: number; latest?: boolean; bestseller?: boolean } = { page: currentPage, limit: 12 };
switch (sortOption) {
case "0": // Latest Products
filters.latest = true;
break;
case "1": // Best Selling
filters.bestseller = true;
break;
case "2": // Old Products (default order)
// Use default ordering
break;
}
return filters;
};
const { products, loading, error, refetch, pagination } = useProducts(getFilters());
return (
<>
<section className="overflow-hidden relative pb-20 pt-[140px] bg-[#f3f4f6]">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0">
<div className="flex gap-7.5">
{/* // <!-- Content Start --> */}
<div className="w-full">
<div className="rounded-lg bg-white shadow-1 pl-3 pr-2.5 py-2.5 mb-6">
<div className="flex items-center justify-between">
{/* <!-- top bar left --> */}
<div className="flex flex-wrap items-center gap-4">
<Select
options={options}
value={sortOption}
onChange={(value) => {
setSortOption(value);
setCurrentPage(1); // Reset to page 1 when sorting changes
}}
label="Sort products"
hideLabel
variant="minimal"
/>
<p>
Showing <span className="text-dark">
{loading ? "..." : `${products.length} of ${pagination?.total || 0}`}
</span>{" "}
Products
</p>
</div>
{/* <!-- top bar right --> */}
<div className="flex items-center gap-2.5">
<Button
onClick={() => setProductStyle("grid")}
aria-label="View as grid"
aria-pressed={productStyle === "grid"}
variant={productStyle === "grid" ? "primary" : "secondary"}
size="sm"
className="w-10.5 h-9"
>
<Icon name="grid" className="fill-current" size={18} />
</Button>
<Button
onClick={() => setProductStyle("list")}
aria-label="View as list"
aria-pressed={productStyle === "list"}
variant={productStyle === "list" ? "primary" : "secondary"}
size="sm"
className="w-10.5 h-9"
>
<Icon name="list" className="fill-current" size={18} />
</Button>
</div>
</div>
</div>
{/* Error State */}
{error && (
<div className="mb-6">
<ErrorMessage
title="Failed to Load Products"
message={error.message}
onRetry={refetch}
/>
</div>
)}
{/* Loading State */}
{loading && <ProductGridSkeleton count={12} />}
{/* Success State */}
{!loading && !error && (
<>
{/* <!-- Products Grid Tab Content Start --> */}
<div
className={`${
productStyle === "grid"
? "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-x-7.5 gap-y-9"
: "flex flex-col gap-7.5"
}`}
>
{products.length === 0 ? (
<div className="col-span-4 text-center py-10">
<p className="text-gray-700 dark:text-gray-300">No products found</p>
</div>
) : (
products.map((item, key) => (
<ProductCard
key={key}
product={item}
variant={productStyle === "grid" ? "grid" : "list"}
/>
))
)}
</div>
{/* <!-- Products Grid Tab Content End --> */}
</>
)}
{/* <!-- Products Pagination Start --> */}
{!loading && !error && pagination && pagination.totalPages > 1 && (
<div className="flex justify-center mt-15">
<div className="bg-white shadow-1 rounded-md p-2">
<ul className="flex items-center">
<li>
<Button
id="paginationLeft"
aria-label="Previous page"
type="button"
disabled={currentPage === 1}
onClick={() => setCurrentPage(prev => Math.max(1, prev - 1))}
variant="ghost"
size="sm"
className="w-8 h-9"
>
<Icon name="chevron-left" className="fill-current" size={18} />
</Button>
</li>
{/* Generate page numbers */}
{Array.from({ length: Math.min(pagination.totalPages, 5) }, (_, i) => {
const pageNum = i + 1;
return (
<li key={pageNum}>
<Button
onClick={() => setCurrentPage(pageNum)}
variant={currentPage === pageNum ? "primary" : "ghost"}
size="sm"
className="py-1.5 px-3.5"
>
{pageNum}
</Button>
</li>
);
})}
{pagination.totalPages > 5 && (
<>
<li>
<span className="flex py-1.5 px-3.5">...</span>
</li>
<li>
<Button
onClick={() => setCurrentPage(pagination.totalPages)}
variant={currentPage === pagination.totalPages ? "primary" : "ghost"}
size="sm"
className="py-1.5 px-3.5"
>
{pagination.totalPages}
</Button>
</li>
</>
)}
<li>
<Button
id="paginationRight"
aria-label="Next page"
type="button"
disabled={currentPage === pagination.totalPages}
onClick={() => setCurrentPage(prev => Math.min(pagination.totalPages, prev + 1))}
variant="ghost"
size="sm"
className="w-8 h-9"
>
<Icon name="chevron-right" className="fill-current" size={18} />
</Button>
</li>
</ul>
</div>
</div>
)}
{/* <!-- Products Pagination End --> */}
</div>
{/* // <!-- Content End --> */}
</div>
</div>
</section>
</>
);
};
export default ShopWithoutSidebar;
|