2018-11-19 16:33:17 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-09-05 15:09:31 +00:00
|
|
|
import {
|
|
|
|
PLACEHOLDER_IMG_SRC,
|
|
|
|
THUMBNAIL_SIZE,
|
|
|
|
} from '@woocommerce/block-settings';
|
2018-11-19 16:33:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a preview for a given product.
|
|
|
|
*/
|
|
|
|
const ProductPreview = ( { product } ) => {
|
|
|
|
let image = null;
|
|
|
|
if ( product.images.length ) {
|
2019-03-15 16:48:46 +00:00
|
|
|
image = (
|
|
|
|
<img
|
|
|
|
className="wc-product-preview__image"
|
|
|
|
src={ product.images[ 0 ].src }
|
|
|
|
alt=""
|
2019-08-17 09:14:11 +00:00
|
|
|
style={ { width: `${ THUMBNAIL_SIZE }px` } }
|
2019-03-15 16:48:46 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-02-01 15:45:53 +00:00
|
|
|
} else {
|
2019-03-15 16:48:46 +00:00
|
|
|
image = (
|
|
|
|
<img
|
|
|
|
className="wc-product-preview__image"
|
2019-08-17 09:14:11 +00:00
|
|
|
src={ PLACEHOLDER_IMG_SRC }
|
2019-03-15 16:48:46 +00:00
|
|
|
alt=""
|
2019-08-17 09:14:11 +00:00
|
|
|
style={ { width: `${ THUMBNAIL_SIZE }px` } }
|
2019-03-15 16:48:46 +00:00
|
|
|
/>
|
|
|
|
);
|
2018-11-19 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 18:50:32 +00:00
|
|
|
const rating = Number( product.average_rating );
|
|
|
|
let displayRating = false;
|
|
|
|
if ( rating > 0 ) {
|
|
|
|
displayRating = ( rating / 5 ) * 100;
|
|
|
|
}
|
|
|
|
|
2018-11-19 16:33:17 +00:00
|
|
|
return (
|
2019-09-05 15:09:31 +00:00
|
|
|
<div className="wc-product-preview wc-block-grid__product">
|
2019-05-16 13:03:11 +00:00
|
|
|
<div className="wc-product-preview__image wc-block-grid__product-image">
|
|
|
|
{ image }
|
|
|
|
</div>
|
2019-02-26 05:32:04 +00:00
|
|
|
<div
|
2019-05-16 13:03:11 +00:00
|
|
|
className="wc-product-preview__title wc-block-grid__product-title"
|
2019-02-26 05:32:04 +00:00
|
|
|
dangerouslySetInnerHTML={ { __html: product.name } }
|
|
|
|
/>
|
2018-11-19 16:33:17 +00:00
|
|
|
<div
|
2019-05-16 13:03:11 +00:00
|
|
|
className="wc-product-preview__price wc-block-grid__product-price"
|
2018-11-19 16:33:17 +00:00
|
|
|
dangerouslySetInnerHTML={ { __html: product.price_html } }
|
|
|
|
/>
|
2019-03-06 18:50:32 +00:00
|
|
|
|
|
|
|
{ displayRating && (
|
2019-09-05 15:09:31 +00:00
|
|
|
<div
|
|
|
|
className="wc-product-preview__rating star-rating wc-block-grid__product-rating"
|
|
|
|
role="img"
|
|
|
|
>
|
2019-03-06 18:50:32 +00:00
|
|
|
<span style={ { width: `${ displayRating }%` } } />
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
|
2018-12-13 17:19:06 +00:00
|
|
|
<span className="wp-block-button">
|
2019-05-16 13:03:11 +00:00
|
|
|
<span className="wc-product-preview__add-to-cart wc-block-grid__product-add-to-cart wp-block-button__link">
|
2018-12-13 17:19:06 +00:00
|
|
|
{ __( 'Add to cart', 'woo-gutenberg-products-block' ) }
|
|
|
|
</span>
|
2018-11-19 16:33:17 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ProductPreview.propTypes = {
|
|
|
|
/**
|
|
|
|
* The product object as returned from the API.
|
|
|
|
*/
|
|
|
|
product: PropTypes.shape( {
|
|
|
|
id: PropTypes.number,
|
2019-09-05 15:09:31 +00:00
|
|
|
average_rating: PropTypes.oneOf( [
|
|
|
|
'PropTypes.number',
|
|
|
|
'PropTypes.string',
|
|
|
|
] ),
|
2018-11-19 16:33:17 +00:00
|
|
|
images: PropTypes.array,
|
|
|
|
name: PropTypes.string,
|
|
|
|
price_html: PropTypes.string,
|
|
|
|
} ).isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ProductPreview;
|