Skip to content

Commit

Permalink
refactor: simplify variant selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rylanharper committed Dec 3, 2024
1 parent acbf3c4 commit ad021b2
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions app/components/product/product-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const isLoading = ref(false);
// Computed
const variants = computed(() => flattenConnection(props.product.variants));
// Add-to-cart button text
const addToCartText = computed(() => {
if (variants.value.length === 1) {
return currentVariant.value?.availableForSale ? 'Add to Cart' : 'Sold Out';
Expand All @@ -35,19 +33,6 @@ const addToCartText = computed(() => {
return 'Sold Out';
});
// Set current variant ID to URL
const setVariantId = (variant: ProductVariantFragment | undefined) => {
const query = { ...route.query };
if (variant) {
query.variant = formatVariantId(variant.id);
} else {
delete query.variant;
}
router.replace({ query });
};
// Actions
const selectSize = (size: string) => {
selectedSize.value = size;
Expand All @@ -61,26 +46,22 @@ const addToCart = async () => {
if (!currentVariant.value) return;
isLoading.value = true;
try {
await cartStore.addToCart([
{
merchandiseId: currentVariant.value.id,
quantity: 1
}
]);
openDrawer();
} catch (error) {
console.error('Failed to add item to cart:', error);
} finally {
isLoading.value = false;
}
await cartStore.addToCart([
{
merchandiseId: currentVariant.value.id,
quantity: 1
}
]);
openDrawer();
isLoading.value = false;
};
// Constants
const sizeOptionNames = ['Size', 'Length'];
// Watchers
// Functions to get and set the initial variant
// Updates the URL query based on the selected variant
const getInitialVariant = () => {
if (variants.value.length === 1) return variants.value[0];
if (variantId.value) {
Expand All @@ -90,6 +71,18 @@ const getInitialVariant = () => {
return undefined;
};
const setVariantId = (variant: ProductVariantFragment | undefined) => {
const query = { ...route.query };
if (variant) {
query.variant = formatVariantId(variant.id);
} else {
delete query.variant;
}
router.replace({ query });
};
const setInitialVariant = () => {
const initialVariant = getInitialVariant();
Expand All @@ -108,6 +101,7 @@ const setInitialVariant = () => {
}
};
// Watchers
watch(
() => props.product,
() => {
Expand All @@ -116,14 +110,11 @@ watch(
{ immediate: true }
);
const isMatchingVariant = (variant: ProductVariantFragment, size: string) =>
variant.selectedOptions.every(({ name, value }) =>
sizeOptionNames.includes(name) ? value === size : true
);
watch(selectedSize, (size) => {
currentVariant.value = variants.value.find((variant) =>
isMatchingVariant(variant, size)
variant.selectedOptions.every(({ name, value }) =>
sizeOptionNames.includes(name) ? value === size : true
)
);
});
Expand Down

0 comments on commit ad021b2

Please sign in to comment.