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

#1064 [BACKOFFICE] [Bugs] Product Variations - show images with thumbnail #1093

Merged
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions storefront/common/components/ProductImageGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export function ProductImageGallery({ listImages }: IProductImageGalleryProps) {

const currentMax = Math.max(currentIndex - Math.floor(NO_SLIDER_IMAGE / 2), 0);

if (currentMax < listImages.length - NO_SLIDER_IMAGE) {
let remainingImages = listImages.length - NO_SLIDER_IMAGE;
if (currentMax < remainingImages) {
// If there are enough images after the current index to fill the slider, start the slider at the current max
startSliderIndex = currentMax;
} else {
} else if (remainingImages >= 0) {
// Otherwise, start the slider at the end of the list minus the maximum number of images to show
startSliderIndex = listImages.length - NO_SLIDER_IMAGE;
startSliderIndex = remainingImages;
}

const visibleImages =
Expand Down
91 changes: 69 additions & 22 deletions storefront/modules/catalog/components/ProductDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export default function ProductDetails({

const initCurrentSelectedOption = useMemo(() => {
if (!productOptions?.length || !productVariations?.length) {
setListImages(product.productImageMediaUrls);
setListImages([
...(product.thumbnailMediaUrl ? [product.thumbnailMediaUrl] : []),
...product.productImageMediaUrls,
]);
return {};
}

Expand All @@ -58,8 +61,14 @@ export default function ProductDetails({

setListImages(
productWithColor
? productWithColor.productImages.map((image) => image.url)
: product.productImageMediaUrls
? [
...(productWithColor.thumbnail?.url ? [productWithColor.thumbnail.url] : []),
...productWithColor.productImages.map((image) => image.url),
]
: [
...(product.thumbnailMediaUrl ? [product.thumbnailMediaUrl] : []),
...product.productImageMediaUrls,
]
);
return productWithColor ? productWithColor.options : productVariations[0].options;
}, [productOptions, productVariations, pvid]);
Expand All @@ -84,30 +93,68 @@ export default function ProductDetails({
}, [productOptions, productVariations, pvid, currentProduct.id]);

useEffect(() => {
if (productOptions?.length && productVariations?.length) {
const productVariation = productVariations.find((item) => {
const isOptionSelected = (
key: string,
currentSelectedOption: CurrentSelectedOption,
item: ProductVariation
) => {
return currentSelectedOption[+key] === item.options[+key];
};

const areAllOptionsSelected = (
optionKeys: string[],
currentSelectedOption: CurrentSelectedOption,
item: ProductVariation
) => {
return optionKeys.every((key: string) => isOptionSelected(key, currentSelectedOption, item));
};

const findProductVariation = () => {
return productVariations?.find((item) => {
const optionKeys = Object.keys(item.options);
return (
Object.keys(item.options).length === Object.keys(currentSelectedOption).length &&
Object.keys(item.options).every((key) => {
return currentSelectedOption[+key] === item.options[+key];
})
optionKeys.length === Object.keys(currentSelectedOption).length &&
areAllOptionsSelected(optionKeys, currentSelectedOption, item)
);
});
};

const updateListImages = (variation: ProductVariation) => {
const urls = [
...(variation.thumbnail?.url ? [variation.thumbnail.url] : []),
...variation.productImages.map((image) => image.url),
];
setListImages(urls);
setCurrentProduct(variation);
};

const updateListImagesBySelectedProduct = (
productVariations: ProductVariation[],
productOptions: ProductOptions[]
) => {
const productSelected = productVariations.find((item) => {
return item.options[+Object.keys(optionSelected)[0]] == Object.values(optionSelected)[0];
});

if (productSelected) {
const colorId = getColorId(productOptions);
if (colorId === +Object.keys(optionSelected)[0]) {
const urlList = productSelected.productImages.map((image) => image.url);
setListImages([
...(productSelected.thumbnail?.url ? [productSelected.thumbnail.url] : []),
...urlList,
]);
}
setCurrentProduct(productSelected);
}
};

if (productOptions?.length && productVariations?.length) {
const productVariation = findProductVariation();
if (productVariation) {
setListImages(productVariation.productImages.map((image) => image.url));
setCurrentProduct(productVariation);
updateListImages(productVariation);
} else {
const productSelected = productVariations.find((item) => {
return item.options[+Object.keys(optionSelected)[0]] == Object.values(optionSelected)[0];
});
if (productSelected) {
const colorId = getColorId(productOptions);
if (colorId === +Object.keys(optionSelected)[0]) {
const urlList = productSelected.productImages.map((image) => image.url);
setListImages(urlList);
}
setCurrentProduct(productSelected);
}
updateListImagesBySelectedProduct(productVariations, productOptions);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down