forked from vtex-apps/store-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (48 loc) · 1.64 KB
/
index.js
File metadata and controls
54 lines (48 loc) · 1.64 KB
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
53
54
import React from 'react'
import { FormattedNumber } from 'react-intl'
import PropTypes from 'prop-types'
import { IOMessage } from 'vtex.native-types'
import styles from './styles.css'
const calculateDiscountTax = (listPrice, sellingPrice) => {
return (listPrice - sellingPrice) / listPrice
}
/**
* The discount badge component. It receives the product's list and selling prices
* and calculates the discount percent to show it in the product's sumary.
*/
const DiscountBadge = ({ listPrice, sellingPrice, label = '', children }) => {
const percent = calculateDiscountTax(listPrice, sellingPrice)
const shouldShowPercentage = percent && percent >= 0.01
return (
<div className={`${styles.discountContainer} relative dib`}>
{shouldShowPercentage ? (
<div
className={`${styles.discountInsideContainer} t-mini white absolute right-0 pv2 ph3 bg-emphasis z-1`}
>
<IOMessage id={label}>
{labelValue => (
<>
{!labelValue && '-'}
<FormattedNumber value={percent} style="percent" />{' '}
{labelValue && ' '}
{labelValue && <span>{labelValue}</span>}
</>
)}
</IOMessage>
</div>
) : null}
{children}
</div>
)
}
DiscountBadge.propTypes = {
/** The product's default price */
listPrice: PropTypes.number.isRequired,
/** The product's price with discount */
sellingPrice: PropTypes.number.isRequired,
/** Label to track the discount percent */
label: PropTypes.string,
/** Image element */
children: PropTypes.node.isRequired,
}
export default DiscountBadge