|
| 1 | +import React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import styled from "styled-components"; |
| 4 | +import { __ } from "@wordpress/i18n"; |
| 5 | +import { round } from "lodash"; |
| 6 | + |
| 7 | +import { StarRating } from "@yoast/components"; |
| 8 | + |
| 9 | +const ProductData = styled.div` |
| 10 | + display: flex; |
| 11 | +`; |
| 12 | + |
| 13 | +const ProductDataCell50 = styled.div` |
| 14 | + flex: 1; |
| 15 | + max-width: 50%; |
| 16 | +`; |
| 17 | + |
| 18 | +const ProductDataCell25 = styled.div` |
| 19 | + flex: 1; |
| 20 | + max-width: 25%; |
| 21 | +`; |
| 22 | + |
| 23 | +const ProductDataInnerLower = styled.div` |
| 24 | + color: #70757a; |
| 25 | +`; |
| 26 | + |
| 27 | +/** |
| 28 | + * Renders ProductData component. |
| 29 | + * |
| 30 | + * @param {Object} props The props. |
| 31 | + * |
| 32 | + * @returns {React.Component} The StarRating Component. |
| 33 | + */ |
| 34 | +function ProductDataMobile( props ) { |
| 35 | + const { shoppingData } = props; |
| 36 | + |
| 37 | + return ( |
| 38 | + <ProductData> |
| 39 | + { ( shoppingData.rating > 0 ) && |
| 40 | + <ProductDataCell50 className="yoast-shopping-data-preview__column"> |
| 41 | + <div className="yoast-shopping-data-preview__upper">{ __( "Rating", "yoast-components" ) }</div> |
| 42 | + <ProductDataInnerLower className="yoast-shopping-data-preview__lower"> |
| 43 | + <span>{ round( ( shoppingData.rating * 2 ), 1 ) }/10 </span> |
| 44 | + <StarRating rating={ shoppingData.rating } /> |
| 45 | + <span> ({ shoppingData.reviewCount })</span> |
| 46 | + </ProductDataInnerLower> |
| 47 | + </ProductDataCell50> |
| 48 | + } |
| 49 | + { ( shoppingData.price ) && |
| 50 | + <ProductDataCell25 className="yoast-shopping-data-preview__column"> |
| 51 | + <div className="yoast-shopping-data-preview__upper">{ __( "Price", "yoast-components" ) }</div> |
| 52 | + <ProductDataInnerLower |
| 53 | + className="yoast-shopping-data-preview__lower" |
| 54 | + dangerouslySetInnerHTML={ { __html: shoppingData.price } } |
| 55 | + /> |
| 56 | + </ProductDataCell25> |
| 57 | + } |
| 58 | + { ( shoppingData.availability ) && |
| 59 | + <ProductDataCell25 className="yoast-shopping-data-preview__column"> |
| 60 | + <div className="yoast-shopping-data-preview__upper">{ __( "Availability", "yoast-components" ) }</div> |
| 61 | + <ProductDataInnerLower className="yoast-shopping-data-preview__lower"> |
| 62 | + { shoppingData.availability } |
| 63 | + </ProductDataInnerLower> |
| 64 | + </ProductDataCell25> |
| 65 | + } |
| 66 | + </ProductData> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +export default ProductDataMobile; |
| 71 | + |
| 72 | +ProductDataMobile.propTypes = { |
| 73 | + shoppingData: PropTypes.shape( { |
| 74 | + rating: PropTypes.number, |
| 75 | + reviewCount: PropTypes.number, |
| 76 | + availability: PropTypes.string, |
| 77 | + price: PropTypes.string, |
| 78 | + } ).isRequired, |
| 79 | +}; |
0 commit comments