-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RS-594 Image fallback for various filetypes (#583)
* 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
Showing
17 changed files
with
192 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }; | ||
|
@@ -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"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/components/src/ImageContainer/image.container.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = {}; | ||
|
||
|
@@ -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)}`}> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 = [ | ||
|
@@ -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> | ||
|
Oops, something went wrong.