124 lines
3.4 KiB
TypeScript
124 lines
3.4 KiB
TypeScript
import { Link } from "wouter";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import Alert from "@mui/material/Alert";
|
|
import Button from "@mui/material/Button";
|
|
import Skeleton from "@mui/material/Skeleton";
|
|
import { Flame } from "lucide-react";
|
|
import { listProducts } from "@/lib/api";
|
|
import { usePlatform } from "@/lib/platform";
|
|
import { ApiNotice } from "./ApiNotice";
|
|
import "@/pages/products.css";
|
|
|
|
export function Catalog({
|
|
search = "",
|
|
offset = 0,
|
|
limit = 12,
|
|
onNext,
|
|
onPrevious,
|
|
}: {
|
|
search?: string;
|
|
offset?: number;
|
|
limit?: number;
|
|
onNext?: () => void;
|
|
onPrevious?: () => void;
|
|
}) {
|
|
const { organization } = usePlatform();
|
|
const query = useQuery({
|
|
queryKey: ["catalog", organization, search, offset, limit],
|
|
queryFn: ({ signal }) =>
|
|
listProducts(organization, search, offset, limit, signal),
|
|
enabled: Boolean(organization),
|
|
retry: false,
|
|
});
|
|
if (!organization)
|
|
return (
|
|
<Alert severity="info">
|
|
Configure your organization on the{" "}
|
|
<Link href="/connection">
|
|
<u>Connection page</u>
|
|
</Link>{" "}
|
|
to view the published collection.
|
|
</Alert>
|
|
);
|
|
if (query.isPending)
|
|
return (
|
|
<div
|
|
className="products-grid"
|
|
aria-busy="true"
|
|
aria-label="Loading collection"
|
|
>
|
|
{Array.from({ length: Math.min(limit, 4) }, (_, i) => (
|
|
<Skeleton key={i} variant="rectangular" height={280} />
|
|
))}
|
|
</div>
|
|
);
|
|
if (query.isError)
|
|
return (
|
|
<>
|
|
<ApiNotice error={query.error} />
|
|
<Button variant="outlined" onClick={() => query.refetch()}>
|
|
Try again
|
|
</Button>
|
|
</>
|
|
);
|
|
return (
|
|
<>
|
|
{!query.data.length ? (
|
|
<Alert severity="info">
|
|
{search
|
|
? "No candles match your search. Try another name."
|
|
: "No published candles are available yet."}
|
|
</Alert>
|
|
) : (
|
|
<div className="products-grid">
|
|
{query.data.map((product) => (
|
|
<article className="prod-card" key={product.id}>
|
|
<Link
|
|
href={`/products/${product.id}`}
|
|
className="prod-img-wrap catalog-placeholder"
|
|
aria-label={`View ${product.name}`}
|
|
>
|
|
<Flame size={48} aria-hidden="true" />
|
|
<span>Mani Candles</span>
|
|
</Link>
|
|
<div className="prod-info">
|
|
<h3 className="prod-name">
|
|
<Link href={`/products/${product.id}`}>{product.name}</Link>
|
|
</h3>
|
|
<p className="prod-scent">
|
|
View available variants and pricing.
|
|
</p>
|
|
<Link
|
|
className="catalog-details"
|
|
href={`/products/${product.id}`}
|
|
>
|
|
View candle →
|
|
</Link>
|
|
</div>
|
|
</article>
|
|
))}
|
|
</div>
|
|
)}
|
|
{onNext && onPrevious && (
|
|
<div className="catalog-pagination">
|
|
<Button
|
|
variant="outlined"
|
|
disabled={offset === 0}
|
|
onClick={onPrevious}
|
|
>
|
|
Previous
|
|
</Button>
|
|
<span>Page {offset / limit + 1}</span>
|
|
<Button
|
|
variant="outlined"
|
|
disabled={query.data.length < limit || offset + limit > 10000}
|
|
onClick={onNext}
|
|
>
|
|
Next
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|