Skip to content

Commit

Permalink
Reflect pre-order state in add to cart button
Browse files Browse the repository at this point in the history
  • Loading branch information
futurGH committed Jul 19, 2023
1 parent 2694a1f commit c1df6bf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/components/product/ProductInfo.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function ProductDescription({ product, gallery }: ProductInfoProps) {
</span>
) : null}
</div>
<AddToCart />
<AddToCart product={product} />
</div>
</div>
);
Expand Down Expand Up @@ -183,7 +183,7 @@ function VariantSelector({ gallery }: { gallery: RefObject<ImageGallery> }) {
);
}

function AddToCart() {
function AddToCart({ product }: { product: ProductDetailsFragment }) {
const { selectedVariant } = useProductOptions();
const { status: cartStatus } = useCart();
const outOfStock = !selectedVariant?.availableForSale ?? true;
Expand All @@ -196,7 +196,13 @@ function AddToCart() {

useEffect(() => {
if (isLoadingState()) setButtonText("Loading...");
if (isIdleState()) setButtonText(outOfStock ? "Sold out" : "Add to cart");
if (isIdleState()) {
if (outOfStock) {
const isPreOrder = product.preorder?.value === "true";
if (isPreOrder) setButtonText("Coming soon");
else setButtonText("Currently unavailable");
} else setButtonText("Add to cart");
}
}, [cartStatus]);

return (
Expand Down

0 comments on commit c1df6bf

Please sign in to comment.