Skip to content

Commit

Permalink
Merge pull request #286 from vtex-apps/feature/on-product-click
Browse files Browse the repository at this point in the history
Add action on product click
  • Loading branch information
thalytafabrine authored Dec 2, 2020
2 parents 95b928b + 6a43c2f commit 8b5ef65
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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
- `actionOnProductClick` prop to `ProductSummaryList`.

## [2.64.0] - 2020-11-30
### Added
- `ProductSummaryImage` can now receive a custom placeholder image by using the `placeholder` prop.
Expand Down
2 changes: 2 additions & 0 deletions react/ProductSummaryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function ProductSummaryList(props) {
installmentCriteria,
children,
ProductSummary,
actionOnProductClick,
} = props

const { data, loading, error } = useQuery(productsQuery, {
Expand Down Expand Up @@ -90,6 +91,7 @@ function ProductSummaryList(props) {
<ProductSummaryListWithoutQuery
products={products}
ProductSummary={ProductSummary}
actionOnProductClick={actionOnProductClick}
>
<ProductListStructuredData products={products} />
{children}
Expand Down
25 changes: 21 additions & 4 deletions react/ProductSummaryListWithoutQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ProductListEventCaller from './components/ProductListEventCaller'

const { ProductListProvider } = ProductListContext

function List({ children, products, ProductSummary }) {
function List({ children, products, ProductSummary, actionOnProductClick }) {
const { list } = useListContext()
const { treePath } = useTreePath()

Expand All @@ -18,8 +18,19 @@ function List({ children, products, ProductSummary }) {
products.map((product) => {
const normalizedProduct = mapCatalogProductToProductSummary(product)

const handleOnClick = () => {
if (typeof actionOnProductClick === 'function') {
actionOnProductClick(normalizedProduct)
}
}

if (typeof ProductSummary === 'function') {
return <ProductSummary product={normalizedProduct} />
return (
<ProductSummary
product={normalizedProduct}
actionOnClick={handleOnClick}
/>
)
}

return (
Expand All @@ -28,12 +39,13 @@ function List({ children, products, ProductSummary }) {
key={product.id}
treePath={treePath}
product={normalizedProduct}
actionOnClick={handleOnClick}
/>
)
})

return list.concat(componentList)
}, [products, treePath, list, ProductSummary])
}, [products, treePath, list, ProductSummary, actionOnProductClick])

return (
<ListContextProvider list={newListContextValue}>
Expand All @@ -46,10 +58,15 @@ const ProductSummaryListWithoutQuery = ({
children,
products,
ProductSummary,
actionOnProductClick,
}) => {
return (
<ProductListProvider>
<List products={products} ProductSummary={ProductSummary}>
<List
products={products}
ProductSummary={ProductSummary}
actionOnProductClick={actionOnProductClick}
>
{children}
</List>
<ProductListEventCaller />
Expand Down

0 comments on commit 8b5ef65

Please sign in to comment.