Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/code reference minicart #84

Merged
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- New component called 'product-reference', where is possible to pull an identifier product information.

## [0.25.0] - 2020-10-27

### Added
Expand Down
12 changes: 11 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Therefore, in order to customize the `product-list` configuration, you can simpl
| `product-list-content-mobile` | Creates the product list layout for mobile devices. |
| `message` | Renders a message text about the product availability. |
| `product-name` | Renders the product names. |
| `product-reference` | Renders the product reference information. |
| `price` | Renders the product prices. |
| `unit-price` | Renders the price for each product unit added to the cart. |
| `product-list-image` | Renders the product images. |
Expand Down Expand Up @@ -298,6 +299,13 @@ Therefore, in order to customize the `product-list` configuration, you can simpl
| ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `width` | `number` | Product image width (in pixels). | `96` |

### `product-reference` props

| Prop name | Type | Description | Default value |
| :------------------: | :---------: | :---------------------------------: | :-------------: |
| `identifierLabel` | `string` | Text label to be displayed to the left of the product reference value. | `undefined` |
| `identifierOption` | `enum` | Desired product reference data i.e. product identifier to be displayed. Possible options are: `ProductId`, `ProductSkuItemId`, `ProductReferenceId`, and `ProductSkuReferenceId`. | `ProductReferenceId` |

### `remove-button` props

| Prop name | Type | Description | Default value |
Expand Down Expand Up @@ -326,6 +334,9 @@ In order to apply CSS customizations in this and other blocks, follow the instru
| `productPriceCurrency` |
| `productPrice` |
| `productQuantityLabel` |
| `productIdentifier` |
| `productIdentifierValue` |
| `productIdentifierLabelValue` |
| `productVariationsContainer` |
| `productVariationsItem` |
| `quantityDropdownContainer` |
Expand All @@ -340,7 +351,6 @@ In order to apply CSS customizations in this and other blocks, follow the instru
| `unitPriceMeasurementUnit` |
| `unitPricePerUnitCurrency` |


<!-- DOCS-IGNORE:start -->

## Contributors ✨
Expand Down
68 changes: 68 additions & 0 deletions react/ProductReference.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { FunctionComponent, useState, useEffect } from 'react'
import { Loading } from 'vtex.render-runtime'
import { useCssHandles } from 'vtex.css-handles'
LucasCastroAcctGlobal marked this conversation as resolved.
Show resolved Hide resolved

import { useItemContext } from './ItemContext'
import { opaque } from './utils/opaque'
import {
PRODUCT_REFERENCE_ID,
PRODUCT_SKU_REFERENCE_ID,
PRODUCT_ID,
PRODUCT_SKU_ITEM_ID,
} from './constants/Identifiers'

const CSS_HANDLES = [
'productIdentifier',
'productIdentifierValue',
'productIdentifierLabelValue',
] as const

interface Props {
identifierLabel?: string
identifierOption?: string
}

const ProductReference: FunctionComponent<Props> = props => {
const { identifierOption = PRODUCT_REFERENCE_ID, identifierLabel } = props
const { item, loading } = useItemContext()
const handles = useCssHandles(CSS_HANDLES)

const [identifierValue, setIdentifierValue] = useState<
string | null | undefined
>()

useEffect(() => {
if (identifierOption === PRODUCT_REFERENCE_ID) {
setIdentifierValue(item.productRefId)
} else if (identifierOption === PRODUCT_SKU_REFERENCE_ID) {
setIdentifierValue(item.refId)
} else if (identifierOption === PRODUCT_SKU_ITEM_ID) {
setIdentifierValue(item.id)
} else if (identifierOption === PRODUCT_ID) {
setIdentifierValue(item.productId)
}
}, [item.productRefId, identifierOption, item.id, item.productId, item.refId])

if (loading) {
return <Loading />
}
LucasCastroAcctGlobal marked this conversation as resolved.
Show resolved Hide resolved

return identifierValue ? (
kaisermann marked this conversation as resolved.
Show resolved Hide resolved
<div
className={`c-on-base t-title lh-copy fw6 no-underline fw5-m ${
LucasCastroAcctGlobal marked this conversation as resolved.
Show resolved Hide resolved
handles.productIdentifier
} ${opaque(item.availability)}`}
>
{identifierLabel && (
<span className={`${handles.productIdentifierLabelValue}`}>
{identifierLabel}
</span>
)}
<span className={`${handles.productIdentifierValue}`}>
{identifierValue}
</span>
</div>
) : null
}

export default ProductReference
4 changes: 4 additions & 0 deletions react/constants/Identifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const PRODUCT_ID = 'ProductId'
export const PRODUCT_SKU_ITEM_ID = 'ProductSkuItemId'
export const PRODUCT_REFERENCE_ID = 'ProductReferenceId'
export const PRODUCT_SKU_REFERENCE_ID = 'ProductSkuReferenceId'
kaisermann marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions store/interfaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
}
}
},
"product-reference": {
"component": "ProductReference"
},
"product-brand": {
"component": "ProductBrand",
"preview": {
Expand Down