Skip to content

Commit

Permalink
Refactor hook
Browse files Browse the repository at this point in the history
  • Loading branch information
fanny committed May 23, 2019
1 parent 74babc4 commit 33f72e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 9 additions & 3 deletions react/components/ProductSummaryPrice/ProductSummaryPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import ProductSummaryContext from '../ProductSummaryContext'
import { productShape } from '../../utils/propTypes'
import productSummary from '../../productSummary.css'

const sortPrices = (priceA, priceB) => priceA - priceB
const lowestPrice = compose(head, sort(sortPrices))
const highestPrice = compose(last, sort(sortPrices))

const isAvailableProduct = price => price !== 0
const getListPrices = prices => {
const sortPrices = prices.sort()
const [lowPrice, _, highPrice] = sortPrices
const lowPrice = lowestPrice(prices)
const highPrice = highestPrice(prices)
return [
lowPrice,
highPrice,
Expand Down Expand Up @@ -50,7 +54,9 @@ const ProductSummaryPrice = ({
sellingPriceClass: 'dib ph2 t-body t-heading-5-ns',
}

const listPrices = useMemo((availableProductsPrices) => getListPrices(availableProductsPrices))
const listPrices = useMemo(() => availableProductsPrices =>
getListPrices(availableProductsPrices)
)

const getPriceRange = () => {
const { items } = product
Expand Down
18 changes: 14 additions & 4 deletions react/legacy/components/ProductSummaryPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import {
prop,
flatten,
map,
filter
filter,
sort,
compose,
head,
last
} from 'ramda'
import { Spinner } from 'vtex.styleguide'
import { ProductPrice } from 'vtex.store-components'

import { productShape } from '../../utils/propTypes'

const sortPrices = (priceA, priceB) => priceA - priceB
const lowestPrice = compose(head, sort(sortPrices))
const highestPrice = compose(last, sort(sortPrices))

const isAvailableProduct = price => price !== 0
const getListPrices = prices => {
const sortPrices = prices.sort()
const [lowPrice, _, highPrice] = sortPrices
const lowPrice = lowestPrice(prices)
const highPrice = highestPrice(prices)
return [
lowPrice,
highPrice,
Expand All @@ -41,7 +49,9 @@ const ProductSummaryPrice = ({
)
}

const listPrices = useMemo((availableProductsPrices) => getListPrices(availableProductsPrices))
const listPrices = useMemo(() => availableProductsPrices =>
getListPrices(availableProductsPrices)
)

const getPriceRange = () => {
const { items } = product
Expand Down

0 comments on commit 33f72e9

Please sign in to comment.