98 lines
3.5 KiB
TypeScript
98 lines
3.5 KiB
TypeScript
import { Link } from "wouter";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import Container from "@mui/material/Container";
|
|
import Alert from "@mui/material/Alert";
|
|
import Button from "@mui/material/Button";
|
|
import Skeleton from "@mui/material/Skeleton";
|
|
import Chip from "@mui/material/Chip";
|
|
import { getProduct } from "@/lib/api";
|
|
import { usePlatform } from "@/lib/platform";
|
|
import { ApiNotice } from "@/components/ApiNotice";
|
|
import "./products.css";
|
|
import { AddToCart } from "@/components/AddToCart";
|
|
import { money } from "@/lib/commerce";
|
|
|
|
export default function ProductPage({ id }: { id: string }) {
|
|
const { organization } = usePlatform();
|
|
const query = useQuery({
|
|
queryKey: ["product", organization, id],
|
|
queryFn: ({ signal }) => getProduct(organization, id, signal),
|
|
enabled: Boolean(organization),
|
|
retry: false,
|
|
});
|
|
return (
|
|
<section className="products-body">
|
|
<Container maxWidth="md">
|
|
<Link href="/products" className="catalog-details">
|
|
← Back to collection
|
|
</Link>
|
|
{!organization ? (
|
|
<Alert severity="info" sx={{ mt: 3 }}>
|
|
Configure your organization under{" "}
|
|
<Link href="/connection">
|
|
<u>Connection</u>
|
|
</Link>{" "}
|
|
first.
|
|
</Alert>
|
|
) : query.isPending ? (
|
|
<Skeleton variant="rectangular" height={300} sx={{ mt: 3 }} />
|
|
) : query.isError ? (
|
|
<>
|
|
<ApiNotice error={query.error} />
|
|
<Button onClick={() => query.refetch()}>Try again</Button>
|
|
</>
|
|
) : (
|
|
<div className="product-detail">
|
|
<h1>{query.data.name}</h1>
|
|
<p className="product-description">
|
|
{query.data.description ||
|
|
"Explore the available candle variants below."}
|
|
</p>
|
|
<div className="product-groups">
|
|
{query.data.groups.map(({ group }) => (
|
|
<Chip key={group.id} label={group.name} />
|
|
))}
|
|
</div>
|
|
<h2>Available variants</h2>
|
|
{!query.data.variants.length ? (
|
|
<Alert severity="info">No active variants are available.</Alert>
|
|
) : (
|
|
<ul className="variant-list">
|
|
{query.data.variants.map((variant) => (
|
|
<li key={variant.id}>
|
|
<div>
|
|
<h3>{variant.name}</h3>
|
|
<small>SKU: {variant.sku}</small>
|
|
<dl>
|
|
{Object.entries(variant.attributes).map(
|
|
([key, value]) => (
|
|
<div key={key}>
|
|
<dt>{key.replaceAll("_", " ")}</dt>
|
|
<dd>{value}</dd>
|
|
</div>
|
|
),
|
|
)}
|
|
</dl>
|
|
</div>
|
|
<strong>
|
|
{variant.currency} {money(variant.price)}
|
|
</strong>
|
|
<AddToCart
|
|
key={`${variant.id}-${organization}`}
|
|
variantId={variant.id}
|
|
/>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
<Alert severity="info">
|
|
Orders reserve stock while pending. Payment and shipping
|
|
integrations are not enabled yet.
|
|
</Alert>
|
|
</div>
|
|
)}
|
|
</Container>
|
|
</section>
|
|
);
|
|
}
|