Skip to content

Commit

Permalink
Lift state manipulation up
Browse files Browse the repository at this point in the history
  • Loading branch information
klzns committed May 25, 2019
1 parent 84856c4 commit c5b6cb8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
16 changes: 10 additions & 6 deletions react/Shelf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types'
import { path } from 'ramda'
import { path, identity } from 'ramda'
import React, { Component, useMemo, useEffect } from 'react'
import { graphql } from 'react-apollo'
import { withRuntimeContext, Loading, useRuntime } from 'vtex.render-runtime'
Expand All @@ -15,7 +15,7 @@ import productsQuery from './queries/productsQuery.gql'
import ShelfContent from './components/ShelfContent'

import shelf from './components/shelf.css'
import normalizeProduct from './utils/normalizeProduct'
import { normalizeProduct, normalizeBuyable } from './utils/normalize'

const useProductImpression = (products) => {
const { push } = usePixel()
Expand All @@ -32,7 +32,7 @@ const useProductImpression = (products) => {
event: 'productImpression',
list: 'Shelf',
position: index + 1,
product,
product: normalizedProduct,
})
});
}, [push, products])
Expand All @@ -45,14 +45,18 @@ const Shelf = ({ data, productList = ProductList.defaultProps }) => {
const { hints: { mobile }} = useRuntime()
const { loading, error, products } = data || {}


const filteredProducts =
products && products.map(normalizeBuyable).filter(identity)

const productListProps = useMemo(() => ({
products,
products: filteredProducts,
loading: loading,
isMobile: mobile,
...productList,
}), [products, loading, mobile, productList])
}), [filteredProducts, loading, mobile, productList])

useProductImpression(products)
useProductImpression(filteredProducts)

if (loading) {
return <Loading />
Expand Down
33 changes: 1 addition & 32 deletions react/components/ProductList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,11 @@ import shelf from './shelf.css'
const DEFAULT_MAX_ITEMS = 10
const DEFAULT_ITEMS_PER_PAGE = 5

function normalizeBuyable(product) {
const items = path(['items'], product)
const buyableItems =
path(['length'], items) &&
items
.map(item => ({
...item,
sellers: getBuyableSellers(item.sellers),
}))
.filter(item => path(['sellers', 'length'], item))

return buyableItems
? {
...product,
items: buyableItems,
}
: null
}

function getBuyableSellers(sellers) {
return (
path(['length'], sellers) &&
sellers.filter(seller => path(['sellerId'], seller))
)
}

/**
* Product List Component. Shows a collection of products.
*/
const ProductList = ({
products,
maxItems,
titleText,
arrows,
scroll,
Expand All @@ -58,9 +31,6 @@ const ProductList = ({
gap,
showTitle,
}) => {
const filteredProducts =
products && products.map(normalizeBuyable).filter(identity)

return products && !products.length ? null : (
<Fragment>
{showTitle && (
Expand All @@ -75,8 +45,7 @@ const ProductList = ({
<ReactResizeDetector handleWidth>
{width => (
<ShelfContent
products={filteredProducts}
maxItems={maxItems}
products={products}
arrows={arrows}
scroll={scroll}
itemsPerPage={itemsPerPage}
Expand Down
6 changes: 2 additions & 4 deletions react/components/ShelfContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ShelfContent extends Component {
loop
easing="ease"
>
{productList.slice(0, maxItems).map((item, index) => (
{productList.map((item, index) => (
<Slide
sliderTransitionDuration={500}
className={classNames('justify-center h-100', gap)}
Expand All @@ -134,7 +134,7 @@ class ShelfContent extends Component {
showDotsPerPage={isScrollByPage}
perPage={this.perPage}
currentSlide={currentSlide}
totalSlides={productList.slice(0, maxItems).length}
totalSlides={productList.length}
onChangeSlide={this.handleChangeSlide}
classes={{
root: 'pt4',
Expand All @@ -159,8 +159,6 @@ ShelfContent.propTypes = {
products: PropTypes.arrayOf(shelfItemPropTypes.item),
/** Max Items per page */
itemsPerPage: PropTypes.number.isRequired,
/** Max items in shelf */
maxItems: PropTypes.number.isRequired,
/** Show Arrows */
arrows: PropTypes.bool.isRequired,
/** Scroll type */
Expand Down
2 changes: 1 addition & 1 deletion react/components/ShelfItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ExtensionPoint } from 'vtex.render-runtime'
import { usePixel } from 'vtex.pixel-manager/PixelContext'

import { shelfItemPropTypes } from '../utils/propTypes'
import normalizeProduct from '../utils/normalizeProduct'
import { normalizeProduct } from '../utils/normalize'

/**
* ShelfItem Component. Normalizes the item received in the props
Expand Down
24 changes: 23 additions & 1 deletion react/utils/normalizeProduct.js → react/utils/normalize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { path } from 'ramda'
import { changeImageUrlSize, toHttps } from './urlHelpers'

export default function normalizeProduct(product) {
export function normalizeProduct(product) {
if (!product) return null
const normalizedProduct = { ...product }
const items = normalizedProduct.items || []
Expand All @@ -28,3 +28,25 @@ function findAvailableProduct(item) {
({ commertialOffer = {} }) => commertialOffer.AvailableQuantity > 0
)
}

export function normalizeBuyable(product) {
if (!product || !product.items || product.items.length === 0) {
return product
}

const buyableItems = product.items.map(item => ({
...item,
sellers: getBuyableSellers(item.sellers),
}))
.filter(item => item && item.sellers && item.sellers.length > 0)

return buyableItems
? { ...product, items: buyableItems }
: null
}

function getBuyableSellers(sellers) {
return sellers && sellers.length > 0
? sellers.filter(seller => seller.sellerId)
: sellers
}

0 comments on commit c5b6cb8

Please sign in to comment.