Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use quantity state #95

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/routes/products.$productSlug/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { LinksFunction, LoaderFunctionArgs, MetaFunction } from '@remix-run
import { isRouteErrorResponse, json, useLoaderData, useNavigate, useRouteError } from '@remix-run/react';
import type { products } from '@wix/stores';
import classNames from 'classnames';
import { useRef, useState } from 'react';
import { useState } from 'react';
import { useAddToCart } from '~/api/api-hooks';
import { getEcomApi } from '~/api/ecom-api';
import { AddToCartOptions, EcomApiErrorCodes } from '~/api/types';
Expand Down Expand Up @@ -44,9 +44,9 @@ export default function ProductDetailsPage() {
const { product } = useLoaderData<typeof loader>();
const { setIsOpen } = useCartOpen();
const [addToCartAttempted, setAddToCartAttempted] = useState(false);
const [quantity, setQuantity] = useState(1);

const { trigger: addToCart } = useAddToCart();
const quantityInput = useRef<HTMLInputElement>(null);

const getInitialSelectedChoices = () => {
const result: Record<string, products.Choice | undefined> = {};
Expand Down Expand Up @@ -78,7 +78,6 @@ export default function ProductDetailsPage() {
return;
}

const quantity = parseInt(quantityInput.current?.value ?? '1', 10);
const selectedVariant = getSelectedVariant(product, selectedChoices);

let options: AddToCartOptions = { options: selectedChoicesToVariantChoices(product, selectedChoices) };
Expand Down Expand Up @@ -129,6 +128,7 @@ export default function ProductDetailsPage() {
option={option}
selectedChoice={selectedChoices[option.name!]}
onChange={(newSelectedChoice) => {
setQuantity(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to reset quantity on option change?
also, are you going to handle quantity in stock in future PRs?

Copy link
Collaborator Author

@yurii-ve yurii-ve Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to reset quantity on option change?

this is how wix template behaves.. kind of makes sense I guess

also, are you going to handle quantity in stock in future PRs?

not for now

setSelectedChoices((prev) => ({
...prev,
[option.name!]: newSelectedChoice,
Expand All @@ -143,12 +143,11 @@ export default function ProductDetailsPage() {
<label>
<div>Quantity:</div>
<input
ref={quantityInput}
defaultValue={1}
className={classNames('numberInput', styles.quantity)}
type="number"
value={quantity}
min={1}
placeholder="1"
onChange={(e) => setQuantity(parseInt(e.target.value, 10))}
/>
</label>
</div>
Expand Down