All files / src/templates/Category/parts/CategoryMap CategoryMap.tsx

64.71% Statements 11/17
33.33% Branches 2/6
60% Functions 3/5
62.5% Lines 10/16

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        2x             2x           5x 5x   5x   5x             5x             5x 2x                   5x        
import React, { FC, useMemo, useRef, useState } from 'react';
import { Loading } from 'ag-ems-ui-library';
import { Map, MapProduct } from '../../../../components';
 
const productCardStyles = require('../../../../components/ProductCard/ProductCard.module.css');
 
export const CategoryMap: FC<{
  products: MapProduct[];
  center?: [number, number];
  zoom?: number;
  open?: boolean;
}> = ({
  products,
  center,
  zoom,
  open
}) => {
  const hoveredSku = useRef<string>('');
  const [preloader, setPreloader] = useState(true);
 
  const onInit = () => setPreloader(false);
 
  const mapMarkerMouseOver = sku => {
    if (sku !== hoveredSku.current) {
      hoveredSku.current = sku;
      document.querySelector(`[data-product-sku="${sku}"]`)?.classList.add(productCardStyles.hovered);
    }
  };
 
  const mapMarkerMouseLeave = () => {
    if (hoveredSku.current) {
      document.querySelector(`[data-product-sku="${hoveredSku.current}"]`)?.classList.remove(productCardStyles.hovered);
    }
    hoveredSku.current = '';
  };
 
  const map = useMemo(() =>
    <Map
      products={products}
      onMouseOver={mapMarkerMouseOver}
      onMouseLeave={mapMarkerMouseLeave}
      onInit={onInit}
      center={center}
      zoom={zoom}
      open={open}
    />, [products, center, zoom, open]);
 
  return <>
    {preloader && <Loading color="#007c00" />}
    {map}
  </>;
};