Skip to content

Commit

Permalink
RS-594 Image fallback for various filetypes (#583)
Browse files Browse the repository at this point in the history
* Created ImageContainer component

* Updated imageContainer component. Added various filetypes of images for SKU image name

* Added ImageContainer to OrderTable, QuickOrderForm and ProductListItem components

* Added ImageContainer to ProductDisplayItem component

* Increase test stability

* Added checking to incorrect file path provided. Updated readme for image container
  • Loading branch information
petro97 authored Feb 20, 2020
1 parent d475b19 commit 2975240
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 89 deletions.
11 changes: 2 additions & 9 deletions src/components/src/AppHeader/appheader.main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { login } from '../utils/AuthService';

import './appheader.main.less';
import { getConfig, IEpConfig } from '../utils/ConfigProvider';
import ImageContainer from '../ImageContainer/image.container';

let Config: IEpConfig | any = {};
let intl = { get: str => str };
Expand Down Expand Up @@ -283,15 +284,7 @@ class AppHeaderMain extends Component<AppHeaderMainProps, AppHeaderMainState> {
<div className="main-container-col">
<div className="logo-container">
<Link to={appHeaderLinks.mainPage} className="logo">
<img
className="logo-image"
alt="Header logo"
src={Config.siteImagesUrl.replace('%fileName%', headerLogoFileName)}
onError={(e) => {
const element: any = e.target;
element.src = headerLogo;
}}
/>
<ImageContainer className="logo-image" fileName={headerLogoFileName} imgUrl={headerLogo} />
</Link>
</div>

Expand Down
13 changes: 7 additions & 6 deletions src/components/src/B2cHomePage/b2c.home.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getConfig, IEpConfig } from '../utils/ConfigProvider';

import Carousel from '../Carousel/carousel.homepage';
import IndiRecommendationsDisplayMain from '../IndiRecommendations/indirecommendations.main';
import ImageContainer from '../ImageContainer/image.container';

import './b2c.home.page.less';

Expand Down Expand Up @@ -61,7 +62,7 @@ const B2CHomePage: React.FunctionComponent = () => {
{/* eslint-disable-next-line no-return-assign */}
<div className="section section-3 container">
<div className="sub-section">
<img className="small-image" alt="home-espot-2" src={Config.siteImagesUrl.replace('%fileName%', homeEspot2FileName)} onError={(e: any) => { e.target.src = homeEspot2; }} />
<ImageContainer className="small-image" fileName={homeEspot2FileName} imgUrl={homeEspot2} />
<div className="text-block">
<span className="line line-1">
{intl.get('home-sub-espot-container2-first-line')}
Expand All @@ -75,7 +76,7 @@ const B2CHomePage: React.FunctionComponent = () => {
<div className="section-parallax section-parallax-1 container" data-region="homeMainContentRegion">
<Parallax y={[-50, 10]} tagOuter="figure">
<div className="parallax-image-container">
<img className="parallax-image" alt="home-espot-1" src={Config.siteImagesUrl.replace('%fileName%', homeEspotParallax1FileName)} onError={(e: any) => { e.target.src = homeEspotParallax1; }} />
<ImageContainer className="parallax-image" fileName={homeEspotParallax1FileName} imgUrl={homeEspotParallax1} />
</div>
</Parallax>
<div className="sub-section">
Expand All @@ -91,7 +92,7 @@ const B2CHomePage: React.FunctionComponent = () => {
</div>
<div className="section section-4 container">
<div className="sub-section">
<img className="small-image image-1" alt="home-espot-3" src={Config.siteImagesUrl.replace('%fileName%', homeEspot3FileName)} onError={(e: any) => { e.target.src = homeEspot3; }} />
<ImageContainer className="small-image image-1" fileName={homeEspot3FileName} imgUrl={homeEspot3} />
<div className="text-block">
<span className="line line-1">
{intl.get('home-sub-espot-container3-first-line')}
Expand All @@ -100,12 +101,12 @@ const B2CHomePage: React.FunctionComponent = () => {
{intl.get('home-sub-espot-container3-second-line')}
</span>
</div>
<img className="small-image image-2" alt="home-espot-3" src={Config.siteImagesUrl.replace('%fileName%', homeEspot3FileName)} onError={(e: any) => { e.target.src = homeEspot3; }} />
<ImageContainer className="small-image image-2" fileName={homeEspot3FileName} imgUrl={homeEspot3} />
</div>
</div>
<div className="section section-5 container">
<div className="sub-section">
<img className="small-image" alt="home-espot-4" src={Config.siteImagesUrl.replace('%fileName%', homeEspot4FileName)} onError={(e: any) => { e.target.src = homeEspot4; }} />
<ImageContainer className="small-image" fileName={homeEspot4FileName} imgUrl={homeEspot4} />
<div className="text-block">
<span className="line line-1">
{intl.get('home-sub-espot-container4-first-line')}
Expand All @@ -119,7 +120,7 @@ const B2CHomePage: React.FunctionComponent = () => {
<div className="section-parallax section-parallax-2 container" data-region="homeMainContentRegion">
<Parallax y={[10, -30]} tagOuter="figure">
<div className="parallax-image-container">
<img className="parallax-image" alt="home-espot-1" src={Config.siteImagesUrl.replace('%fileName%', homeEspotParallax2FileName)} onError={(e: any) => { e.target.src = homeEspotParallax2; }} />
<ImageContainer className="parallax-image" fileName={homeEspotParallax2FileName} imgUrl={homeEspotParallax2} />
</div>
</Parallax>
<div className="sub-section">
Expand Down
39 changes: 6 additions & 33 deletions src/components/src/Carousel/carousel.homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import React from 'react';
import Slider from 'react-slick';
import { getConfig, IEpConfig } from '../utils/ConfigProvider';
import ImageContainer from '../ImageContainer/image.container';

import carouselBaner1 from '../../../images/carousel-images/baner_1.jpg';
import carouselBaner2 from '../../../images/carousel-images/baner_2.jpg';
Expand Down Expand Up @@ -51,7 +52,7 @@ const Carousel: React.FunctionComponent = () => {
customPaging(i) {
return (
<div className="">
<img alt="img" src={Config.siteImagesUrl.replace('%fileName%', carouselBanerNameArray[i])} onError={(e) => { const element: any = e.target; element.src = carouselBanerArray[i]; }} />
<ImageContainer fileName={carouselBanerNameArray[i]} imgUrl={carouselBanerArray[i]} />
</div>
);
},
Expand All @@ -78,44 +79,16 @@ const Carousel: React.FunctionComponent = () => {
</div>
<Slider {...settings}>
<div>
<img
alt="img"
src={Config.siteImagesUrl.replace('%fileName%', carouselBaner1FileName)}
onError={(e) => {
const element: any = e.target;
element.src = carouselBaner1;
}}
/>
<ImageContainer fileName={carouselBaner1FileName} imgUrl={carouselBaner1} />
</div>
<div>
<img
alt="img"
src={Config.siteImagesUrl.replace('%fileName%', carouselBaner2FileName)}
onError={(e) => {
const element: any = e.target;
element.src = carouselBaner2;
}}
/>
<ImageContainer fileName={carouselBaner2FileName} imgUrl={carouselBaner2} />
</div>
<div>
<img
alt="img"
src={Config.siteImagesUrl.replace('%fileName%', carouselBaner3FileName)}
onError={(e) => {
const element: any = e.target;
element.src = carouselBaner3;
}}
/>
<ImageContainer fileName={carouselBaner3FileName} imgUrl={carouselBaner3} />
</div>
<div>
<img
alt="img"
src={Config.siteImagesUrl.replace('%fileName%', carouselBaner4FileName)}
onError={(e) => {
const element: any = e.target;
element.src = carouselBaner4;
}}
/>
<ImageContainer fileName={carouselBaner4FileName} imgUrl={carouselBaner4} />
</div>
</Slider>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/src/CartLineItem/cart.lineitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import AppModalBundleConfigurationMain from '../AppModalBundleConfiguration/appm
import DropdownCartSelection from '../DropdownCartSelection/dropdown.cart.selection.main';
import './cart.lineitem.less';

import imgPlaceholder from '../../../images/[email protected]';
import { ReactComponent as UpdateQuantityIcon } from '../../../images/icons/ic_update.svg';
import { ReactComponent as RecycleBinIcon } from '../../../images/icons/ic_trash.svg';
import ImageContainer from '../ImageContainer/image.container';

let Config: IEpConfig | any = {};
let intl = { get: str => str };
Expand Down Expand Up @@ -510,7 +510,7 @@ class CartLineItem extends Component<CartLineItemProps, CartLineItemState> {
: ('')
}
<Link to={`${itemDetailLink}/${encodeURIComponent(itemCodeString)}`}>
<img src={Config.skuImagesUrl.replace('%sku%', itemCodeString)} onError={(e) => { const element: any = e.target; element.src = imgPlaceholder; }} alt="Not Available" className="cart-lineitem-thumbnail" />
<ImageContainer className="cart-lineitem-thumbnail" isSkuImage fileName={itemCodeString} imgUrl={Config.skuImagesUrl.replace('%sku%', itemCodeString)} />
</Link>
</div>
<div className="title-options-col">
Expand Down
23 changes: 23 additions & 0 deletions src/components/src/ImageContainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ImageContainer

#### Description

Initially trying to fetch primary file type received from the prop if not found try PNG, if not found try JPG, if not found try SVG etc.

The configuration of these file types may be managed within `ep.config.json`. If no file types are provided in the configuration, the image will be used as is provided in the URL.

#### Usage

```js
import ImageContainer from '../ImageContainer/image.container';
```

#### Example

```js
<ImageContainer className="parallax-image" fileName={homeEspotParallax1FileName} imgUrl={homeEspotParallax1} />
```

#### Properties

<!-- PROPS -->
38 changes: 38 additions & 0 deletions src/components/src/ImageContainer/image.container.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright © 2019 Elastic Path Software Inc. All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this license. If not, see
*
* https://www.gnu.org/licenses/
*
*
*/
import React from 'react';
import { storiesOf } from '@storybook/react';
import Readme from './README.md';
import ImageContainer from './image.container';
import homeEspotParallax1 from '../../../images/site-images/parallax-banner-1.jpg';

storiesOf('Components|ImageContainer', module)
.addParameters({
readme: {
// Show readme at the addons panel
sidebar: Readme,
},
})
.add('ImageContainer', () => {
const homeEspotParallax1FileName = 'parallax-banner-1.jpg';

return (<ImageContainer className="parallax-image" fileName={homeEspotParallax1FileName} imgUrl={homeEspotParallax1} />);
});
90 changes: 90 additions & 0 deletions src/components/src/ImageContainer/image.container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright © 2019 Elastic Path Software Inc. All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this license. If not, see
*
* https://www.gnu.org/licenses/
*
*
*/

import React from 'react';
import { getConfig, IEpConfig } from '../utils/ConfigProvider';
import imgPlaceholder from '../../../images/[email protected]';

let Config: IEpConfig | any = {};

interface ImageContainerProps {
/** name of file */
fileName: string;
/** image URL */
imgUrl: string;
/** class name */
className?: string;
/** is a sku image */
isSkuImage?: boolean;
/** loading data */
onLoadData?: (...args: any[]) => any;
}

function ImageContainer(props: ImageContainerProps) {
const epConfig = getConfig();
Config = epConfig.config;
const {
fileName, imgUrl, className, isSkuImage, onLoadData,
} = props;

const imageSource = isSkuImage ? Config.skuImagesUrl.replace('%sku%', fileName) : Config.siteImagesUrl.replace('%fileName%', fileName);

const handleError = (e, defaultImgUrl) => {
const { src } = e.target;
if (Config.imageFileTypes && Config.imageFileTypes.enable && e.target['data-source'] !== 'default') {
const fallbackTypes = Config.imageFileTypes.types;
const initType = imgUrl.replace(/.*(?=\.)/g, '');
const types = (fallbackTypes && fallbackTypes.length > 0) ? [
initType,
...fallbackTypes.filter(fileType => fileType !== initType),
] : [];
const currentType = src.replace(/.*(?=\.)/g, '');
const i = types.indexOf(currentType);
if (types[i + 1]) {
e.target.src = src.replace(currentType, types[i + 1]);
} else {
e.target.src = isSkuImage ? imgPlaceholder : defaultImgUrl;
e.target['data-source'] = 'default';
}
} else if (e.target['data-source'] !== 'default') {
e.target.src = isSkuImage ? imgPlaceholder : defaultImgUrl;
e.target['data-source'] = 'default';
}
};

return (
<img
className={className}
alt="img"
src={imageSource}
onError={e => handleError(e, imgUrl)}
onLoad={onLoadData}
/>
);
}

ImageContainer.defaultProps = {
className: '',
isSkuImage: false,
onLoadData: () => {},
};

export default ImageContainer;
12 changes: 2 additions & 10 deletions src/components/src/OrderTableLineItem/ordertable.lineitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { getConfig, IEpConfig } from '../utils/ConfigProvider';
import imgPlaceholder from '../../../images/[email protected]';
import './ordertable.lineitem.less';
import ImageContainer from '../ImageContainer/image.container';

let Config: IEpConfig | any = {};

Expand Down Expand Up @@ -106,15 +106,7 @@ function OrderTableLineItem(props: OrderTableLineItemProps) {
return (
<tr className="order-lineitem-row">
<td className="thumbnail-col">
<img
className="thumbnail"
src={Config.skuImagesUrl.replace('%sku%', code)}
alt="Not Available"
onError={(e) => {
const element:any = e.target;
element.src = imgPlaceholder;
}}
/>
<ImageContainer className="thumbnail" isSkuImage fileName={code} imgUrl={Config.skuImagesUrl.replace('%sku%', code)} />
</td>
<td className="title-col">
<Link to={`${itemDetailLink}/${encodeURIComponent(code)}`}>
Expand Down
14 changes: 3 additions & 11 deletions src/components/src/ProductDisplayItem/productdisplayitem.main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Slider from 'react-slick';
import { InlineShareButtons } from 'sharethis-reactjs';
import { login } from '../utils/AuthService';
import { itemLookup, cortexFetchItemLookupForm } from '../utils/CortexLookup';
import imgMissingHorizontal from '../../../images/[email protected]';
import ProductRecommendationsDisplayMain from '../ProductRecommendations/productrecommendations.main';
import IndiRecommendationsDisplayMain from '../IndiRecommendations/indirecommendations.main';
import BundleConstituentsDisplayMain from '../BundleConstituents/bundleconstituents.main';
Expand All @@ -36,6 +35,7 @@ import { useRequisitionListCountDispatch } from '../requisition-list-count-conte

import './productdisplayitem.main.less';
import PowerReview from '../PowerReview/powerreview.main';
import ImageContainer from '../ImageContainer/image.container';

// Array of zoom parameters to pass to Cortex
const zoomArray = [
Expand Down Expand Up @@ -580,23 +580,15 @@ class ProductDisplayItemMain extends Component<ProductDisplayItemMainProps, Prod
if (arFileExists) {
return (
<a href={Config.arKit.skuArImagesUrl.replace('%sku%', productData._code[0].code)} rel="ar">
<img
src={Config.skuImagesUrl.replace('%sku%', productData._code[0].code)}
onError={(e) => {
const element: any = e.target;
element.src = imgMissingHorizontal;
}}
alt={intl.get('none-available')}
className="itemdetail-main-img"
/>
<ImageContainer className="itemdetail-main-img" isSkuImage fileName={productData._code[0].code} imgUrl={Config.skuImagesUrl.replace('%sku%', productData._code[0].code)} />
</a>
);
}
return (
<div className="product-image-carousel">
<Slider {...settings}>
<div>
<img src={Config.skuImagesUrl.replace('%sku%', productData._code[0].code)} onError={(e) => { const element: any = e.target; element.src = imgMissingHorizontal; }} alt={intl.get('none-available')} className="itemdetail-main-img" />
<ImageContainer className="itemdetail-main-img" isSkuImage fileName={productData._code[0].code} imgUrl={Config.skuImagesUrl.replace('%sku%', productData._code[0].code)} />
</div>
</Slider>
</div>
Expand Down
Loading

0 comments on commit 2975240

Please sign in to comment.