Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 8x 8x 4x 8x 4x 8x 4x | import React, { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { KsCard, Button, Benefits, Benefit } from 'ag-ems-ui-library';
import { GatsbyImage, getImage } from "gatsby-plugin-image";
import { LocalizedLink as Link } from 'gatsby-theme-i18n';
import { useTranslation } from 'react-i18next'
import { format, formatToPrice } from '../../helpers';
import { useCartContext } from '../../contexts'
import { Duration, Tags } from '../Common';
const styles = require('./ProductCard.module.css');
export const ProductCard: FC<{
product: any;
classes?: string;
onMouseOver?: (sku: string) => void;
onMouseLeave?: () => void;
startDate?: string;
endDate?: string;
}> = ({
product,
classes,
onMouseOver,
onMouseLeave,
startDate,
endDate
}) => {
const { t } = useTranslation();
const { add } = useCartContext();
const [loading, setLoading] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const [key, setKey] = useState<'client' | 'server'>('server');
const image = getImage(product.thumbnail);
const addToCartHandler = useCallback(async () => {
setLoading(true);
await add(product.sku, 1);
setLoading(false);
}, []);
const mouseOver = useCallback(() => {
if (ref.current) {
ref.current.classList.add(styles.hovered);
}
onMouseOver && onMouseOver(product.sku);
}, [onMouseOver]);
const mouseLeave = useCallback(() => {
if (ref.current) {
ref.current.classList.remove(styles.hovered);
}
onMouseLeave && onMouseLeave();
}, [onMouseLeave]);
useEffect(() => setKey('client'), []);
const cardUrl = useMemo(() =>
product.productType === 'experience'
? `/${product.slug}?date=${startDate}`
: `/${product.slug}?startDate=${startDate}&endDate=${endDate}`,
[startDate, endDate]);
const cardImage = useMemo(() => image
? <GatsbyImage style={{ height: '100%' }} image={image} alt={product.name} />
: null, [image]);
const cardDescription = useMemo(() =>
product.productType === 'experience'
? <>
<Duration duration={product.duration} size="small" />
<Tags label={t('categories')} tags={product.experience_categories} size="small" />
<Tags label={t('themes')} tags={product.experience_attributes} size="small" />
</>
: product.productType === 'bicycle'
? <>
{startDate && endDate && <p>
{`${t('rentalPeriod')}:
${startDate ? format(startDate, 'dd.MM') : ''}
${t('until')}
${endDate ? format(endDate, 'dd.MM') : ''}`}
</p>}
</>
: <>
{startDate && endDate && <p>
{`${t('rentalPeriod')}:
${startDate ? format(startDate, 'dd.MM') : ''}
${t('until')}
${endDate ? format(endDate, 'dd.MM') : ''}`}
</p>}
<Benefits>
<Benefit>{t('parking.benefit')}</Benefit>
</Benefits>
</>,
[startDate, endDate]);
const cardButton = useMemo(() =>
product.productType === 'experience' || product.productType === 'bicycle'
? null
: <Button
classes={styles.ProductCardButton}
key={key}
sizing="lg"
value={t('book')}
variant={loading ? 'secondary' : 'ks-primary'}
disabled={loading}
onClick={addToCartHandler}
/>,
[addToCartHandler, loading]);
return useMemo(() => (
<div
className={`${styles.ProductCard} ${classes ? classes : ''}`}
data-product-sku={product.sku}
onMouseOver={mouseOver}
onMouseLeave={mouseLeave}
ref={ref}
>
<KsCard
classes={styles.ProductCardCard}
link={cardUrl}
image={cardImage}
title={<h2 className={styles.ProductCardTitle}>{product.name}</h2>}
price={`${t('from')} ${formatToPrice(product.magentoData.price.regularPrice.amount.value)}`}
button={cardButton}
linkComponent={Link}
>
{cardDescription}
</KsCard>
</div>
), [
product,
classes,
cardImage,
cardDescription,
addToCartHandler,
mouseOver,
mouseLeave
]);
} |