import React, {useEffect, useReducer} from 'react'; import classnames from 'classnames'; import './VariantCell.scss'; import isTouchDevice from 'is-touch-device'; import FavoriteFilledIcon from '@moda/icons/favorite-filled-24'; import FavoriteOutlineIcon from '@moda/icons/favorite-outline-24'; import {AddToBag} from './AddToBag'; import {Button} from './Button'; import {Badge} from './Badge'; import {Image} from './Image'; import {SizeSlider} from './SizeSlider'; import {ColorPicker} from './ColorPicker'; import {ProductPrice} from './ProductPrice'; import QuickAddToBag from './QuickAddToBag'; import { MasterVariant, Variant, Size, Inventory, ImageUrls } from '../../types/VariantTypes'; interface State { activeImage: number; hover: boolean; transition: boolean; imageCount: number; selectedVariant: Variant; selectedSize: Inventory | null; showUnlikeConfirmation: boolean; preselectFilteredSize: boolean; removeUnfilteredSizes: boolean; sizesArray: Inventory[]; } type Action = | {type: 'StartRotate'} | {type: 'Transition'} | {type: 'StopRotate'} | {type: 'UpdateSelectedVariant'; variant: Variant} | {type: 'UpdatedSelectedSize'; size: Inventory} | {type: 'SetHover'; hover: boolean} | {type: 'ClearAllSelections'; attributes: MasterVariant} | {type: 'SetShowUnlikeConfirmation'; showUnlikeConfirmation: boolean} | {type: 'UpdateSizesArray'; sizesArray: Inventory[]}; interface Props { attributes?: MasterVariant; imageVersion?: 'small' | 'medium' | 'large'; unlikeConfirmation?: boolean; showFallEssentialBadge?: boolean; showSaleSavings?: boolean; isBestSeller?: boolean; isFavorited?: boolean; onFavorite?: Function; onAddToBag: (selectedSize: Size, selectedVariant: Variant) => Promise; onClick?: Function; preselectFilteredSize?: boolean; removeUnfilteredSizes?: boolean; quickAddToBag?: boolean; } const getImageUrls = (variantData: Variant): Array => new Array(variantData.primary_image_urls).concat( variantData.alternate_image_urls ); const isSingleOSSize = (selectedVariant: Variant): boolean => { if (selectedVariant.inventory.length == 1) { return selectedVariant.inventory[0].size == 'OS'; } else { return false; } }; const getLowestAvailableFilteredSize = ( selectedVariant: Variant ): Inventory => { return selectedVariant.inventory.find(function(sku: Inventory): boolean { return sku.filtered_by_this_size && sku.can_sell; }); }; const getDefaultSelectedSize = ( selectedVariant: Variant, preselectFilteredSize = false ): Inventory | null => { if (isSingleOSSize(selectedVariant)) { return selectedVariant.inventory[0]; } else if (preselectFilteredSize) { return getLowestAvailableFilteredSize(selectedVariant); } return null; }; const getSizesArray = ( selectedVariant: Variant, removeUnfilteredSizes: boolean ): Inventory[] => { let sizesArray = selectedVariant.inventory; if (removeUnfilteredSizes) { const filteredSizes = selectedVariant.inventory.filter( size => size.filtered_by_this_size ); if (filteredSizes.length > 0) { sizesArray = filteredSizes; } } return sizesArray; }; const reducer = (state: State, action: Action): State => { switch (action.type) { case 'StartRotate': if (state.imageCount != 0 && state.activeImage < state.imageCount - 1) { return { ...state, activeImage: state.activeImage + 1, transition: false }; } else { return {...state, activeImage: 0, transition: false}; } case 'Transition': return {...state, transition: true}; case 'StopRotate': return {...state, activeImage: 0, transition: false}; case 'SetHover': return {...state, hover: action.hover}; case 'UpdateSelectedVariant': return { ...state, selectedVariant: action.variant, selectedSize: getDefaultSelectedSize( action.variant, state.preselectFilteredSize ), imageCount: getImageUrls(action.variant).length, activeImage: 0 }; case 'UpdateSizesArray': return {...state, sizesArray: action.sizesArray}; case 'UpdatedSelectedSize': return {...state, selectedSize: action.size}; case 'ClearAllSelections': return { ...state, selectedVariant: action.attributes.master_variants_data }; case 'SetShowUnlikeConfirmation': return { ...state, showUnlikeConfirmation: action.showUnlikeConfirmation }; } }; export const VariantCell: React.FC = ({ attributes, imageVersion = 'large', unlikeConfirmation = false, showFallEssentialBadge = false, showSaleSavings, isBestSeller = false, isFavorited = false, onFavorite, onAddToBag, onClick = undefined, preselectFilteredSize = false, removeUnfilteredSizes = false, quickAddToBag = false }) => { const timerId: React.MutableRefObject = React.useRef(null); const initialState: State = { hover: false, transition: false, activeImage: 0, imageCount: getImageUrls(attributes.master_variants_data).length, selectedVariant: attributes.master_variants_data, selectedSize: getDefaultSelectedSize( attributes.master_variants_data, preselectFilteredSize ), showUnlikeConfirmation: false, preselectFilteredSize, removeUnfilteredSizes, sizesArray: getSizesArray( attributes.master_variants_data, removeUnfilteredSizes ) }; const [state, dispatch] = useReducer(reducer, initialState); useEffect(() => { timerId.current = window.setTimeout(() => { if (state.hover) { dispatch({type: 'Transition'}); } }, 3000); return (): void => window.clearTimeout(timerId.current); }, [state.activeImage, state.hover]); useEffect(() => { if (timerId.current) { window.clearTimeout(timerId.current); } }, [state.selectedVariant]); useEffect(() => { dispatch({ type: 'UpdateSizesArray', sizesArray: getSizesArray( state.selectedVariant, state.removeUnfilteredSizes ) }); }, [state.selectedVariant, state.removeUnfilteredSizes]); useEffect(() => { if (state.hover) { dispatch({type: 'Transition'}); } else { dispatch({type: 'StopRotate'}); dispatch({type: 'ClearAllSelections', attributes}); } }, [attributes, state.hover]); useEffect(() => { dispatch({ type: 'UpdateSelectedVariant', variant: attributes.master_variants_data }); }, [attributes]); const onSetSize = (size: Inventory): void => dispatch({type: 'UpdatedSelectedSize', size: size}); const onChangeVariant = (variant: Variant): void => dispatch({type: 'UpdateSelectedVariant', variant: variant}); const onClickFavorite = (): void => { if (unlikeConfirmation && isFavorited) { dispatch({ type: 'SetShowUnlikeConfirmation', showUnlikeConfirmation: true }); } else { onFavorite(!isFavorited, state.selectedVariant.id); } }; const onConfirmUnlike = (): void => { dispatch({ type: 'SetShowUnlikeConfirmation', showUnlikeConfirmation: false }); onFavorite(false, state.selectedVariant.id); }; const getImageUrl = (imageUrls: ImageUrls): string => { return imageUrls[imageVersion]; }; const PROMO_LABELS = [ { enabled: showFallEssentialBadge && state.selectedVariant.collections.includes('Sept19Offer'), label: 'Fall Essential', theme: 'brick' }, { enabled: isBestSeller, label: 'Bestseller', theme: 'forest-green' } ]; const promoLabel = PROMO_LABELS.find(label => label.enabled); const promoBadge30 = typeof isFeatureFlagEnabled !== 'undefined' && // eslint-disable-next-line no-undef isFeatureFlagEnabled('promo-badge-best-30') && attributes.master_variants_data.collections.includes('30Promo'); return (
{ if (!isTouchDevice()) { dispatch({type: 'SetHover', hover: true}); } }} onMouseLeave={(): void => { if (!isTouchDevice()) { dispatch({type: 'SetHover', hover: false}); } }} onClick={(event: React.MouseEvent): void => onClick(event, state.selectedVariant) } data-variant-pk={attributes.id} data-product-id={attributes.product_id} id={'variant-cell-' + attributes.id} >
onClickFavorite()}> {isFavorited ? : }
{state.showUnlikeConfirmation && (
Do you really want to remove this item from your wishlist?
)}
{state.selectedVariant.label}
{quickAddToBag ? ( onAddToBag(size, state.selectedVariant) } onChangeVariant={onChangeVariant} /> ) : ( onAddToBag(state.selectedSize, state.selectedVariant) } selectedVariant={state.selectedVariant} sizesArray={state.sizesArray} showSaleSavings={showSaleSavings} /> )}
{attributes.designer_name}
{attributes.name}
{promoBadge30 && ( 30% off with Code BEST30 )} {attributes.tag && ( {attributes.tag} )}
{attributes.other_variants_data.length > 0 && (
+ More Colors
)}
); }; interface QuickShopOverlayProps { attributes: MasterVariant; onChangeVariant: (variant: Variant) => void; selectedSize: Inventory; onSetSize: (size: Inventory) => void; onAddToBag: () => Promise; selectedVariant: Variant; sizesArray: Inventory[]; showSaleSavings?: boolean; } const QuickShopOverlay: React.FC = ({ attributes, onChangeVariant, selectedSize, onSetSize, onAddToBag, selectedVariant, sizesArray, showSaleSavings }) => { const variantsDataArray: Variant[] = [ attributes.master_variants_data, ...attributes.other_variants_data ]; return (
{variantsDataArray.length > 1 && ( )} {isSingleOSSize(selectedVariant) && variantsDataArray.length <= 1 && ( <>
{attributes.designer_name}
{attributes.name}
)} {!isSingleOSSize(selectedVariant) && ( )}
); };