Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
[FSSS-172] Fix/Adjust inappropriate rerenders (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurTriis1 authored and danzanzini committed Feb 7, 2022
1 parent fe9618e commit 9302081
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/components/common/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import {
List as UIList,
Icon as UIIcon,
Expand Down Expand Up @@ -137,4 +137,4 @@ function Footer() {
)
}

export default Footer
export default memo(Footer)
4 changes: 2 additions & 2 deletions src/components/common/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { memo, useState } from 'react'
import { Link as LinkGatsby } from 'gatsby'
import { List as UIList } from '@faststore/ui'
import CartToggle from 'src/components/cart/CartToggle'
Expand Down Expand Up @@ -117,4 +117,4 @@ function Navbar() {
)
}

export default Navbar
export default memo(Navbar)
47 changes: 5 additions & 42 deletions src/components/product/ProductCard/ProductCard.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { graphql, Link } from 'gatsby'
import type { ReactNode } from 'react'
import React, { useMemo, memo } from 'react'
import Button from 'src/components/ui/Button'
import { DiscountBadge, Badge } from 'src/components/ui/Badge'
import Price from 'src/components/ui/Price'
import AspectRatio from 'src/components/ui/AspectRatio'
import type { AspectRatioProps } from 'src/components/ui/AspectRatio'
import { Image } from 'src/components/ui/Image'
import { useBuyButton } from 'src/sdk/cart/useBuyButton'
import { useFormattedPrice } from 'src/sdk/product/useFormattedPrice'
import { useProductLink } from 'src/sdk/product/useProductLink'
import type { ProductSummary_ProductFragment } from '@generated/graphql'
import { ShoppingCart as ShoppingCartIcon } from 'phosphor-react'
import {
Card as UICard,
CardImage as UICardImage,
Expand All @@ -28,27 +26,21 @@ interface Props {
bordered?: boolean
outOfStock?: boolean
variant?: Variant
showActions?: boolean
ratio?: AspectRatioProps['ratio']
buyButton?: ReactNode
}

function ProductCard({
product,
index,
variant = 'vertical',
showActions = true,
ratio = '1',
bordered = false,
outOfStock = false,
buyButton,
...otherProps
}: Props) {
const {
id,
sku,
gtin,
name: variantName,
brand,
isVariantOf,
isVariantOf: { name },
image: [img],
offers: { lowPrice: spotPrice, offers },
Expand All @@ -68,32 +60,16 @@ function ProductCard({
return lowestPriceOffer
}, [spotPrice, offers])

const { listPrice, seller } = offers[selectedOffer]
const { listPrice } = offers[selectedOffer]

const linkProps = useProductLink({ product, selectedOffer, index })
const buyProps = useBuyButton({
id,
brand,
price: spotPrice,
listPrice,
seller,
quantity: 1,
isVariantOf,
gtin,
itemOffered: {
name: variantName,
image: [img],
sku,
},
})

return (
<UICard
className="product-card"
data-card-variant={variant}
data-card-bordered={bordered}
data-card-out-of-stock={outOfStock}
data-sku={buyProps['data-sku']}
{...otherProps}
>
<UICardImage>
Expand Down Expand Up @@ -152,20 +128,7 @@ function ProductCard({
<DiscountBadge small listPrice={listPrice} spotPrice={spotPrice} />
)}
</UICardContent>
{showActions && (
<UICardActions>
<Button
{...buyProps}
variant="primary"
icon={<ShoppingCartIcon size={18} weight="bold" />}
iconPosition="left"
aria-label="Add to cart"
title="Add to cart"
>
Add
</Button>
</UICardActions>
)}
{!!buyButton && <UICardActions>{buyButton}</UICardActions>}
</UICard>
)
}
Expand Down
1 change: 0 additions & 1 deletion src/components/product/ProductGrid/ProductGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function ProductGrid({ products, page, pageSize }: Props) {
<ProductCard
product={product}
index={pageSize * page + idx + 1}
showActions={false}
bordered
outOfStock={
product.offers.offers?.[0].availability !==
Expand Down
4 changes: 2 additions & 2 deletions src/components/sections/Hero/Hero.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import type { ReactNode } from 'react'
import UIHero, {
HeroContent,
Expand Down Expand Up @@ -75,4 +75,4 @@ const Hero = ({
)
}

export default Hero
export default memo(Hero)
2 changes: 1 addition & 1 deletion src/components/sections/ProductShelf/ProductShelf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ProductShelf({ products }: ProductShelfProps) {
<ul data-product-shelf className="grid-content">
{products.map((product, idx) => (
<li key={`${product.id}`}>
<ProductCard product={product} index={idx + 1} showActions={false} />
<ProductCard product={product} index={idx + 1} />
</li>
))}
</ul>
Expand Down
1 change: 0 additions & 1 deletion src/components/sections/ProductTiles/ProductTiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const ProductTiles = ({ products }: TilesProps) => {
product={product}
index={idx + 1}
variant="horizontal"
showActions={false}
ratio={getRatio(idx)}
/>
</Tile>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/Alert/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode, MouseEvent } from 'react'
import React from 'react'
import React, { memo } from 'react'
import { Alert as UIAlert, Icon as UIIcon } from '@faststore/ui'
import type { AlertProps } from '@faststore/ui'
import Button from 'src/components/ui/Button'
Expand Down Expand Up @@ -56,4 +56,4 @@ function Alert({
)
}

export default Alert
export default memo(Alert)
4 changes: 2 additions & 2 deletions src/components/ui/Badge/DiscountBadge.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import { useDiscountPercent } from 'src/sdk/product/useDiscountPercent'

import Badge from './Badge'
Expand Down Expand Up @@ -44,4 +44,4 @@ const DiscountBadge = ({
)
}

export default DiscountBadge
export default memo(DiscountBadge)
4 changes: 2 additions & 2 deletions src/components/ui/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import { Breadcrumb as UIBreadcrumb } from '@faststore/ui'
import Link from 'src/components/ui/Link'
import type { BreadcrumbProps as UIBreadcrumbProps } from '@faststore/ui'
Expand Down Expand Up @@ -41,4 +41,4 @@ function Breadcrumb({ breadcrumbList }: BreadcrumbProps) {
)
}

export default Breadcrumb
export default memo(Breadcrumb)
21 changes: 21 additions & 0 deletions src/components/ui/Breadcrumb/BreadcrumbWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { memo } from 'react'

import type { BreadcrumbProps } from '.'
import Breadcrumb from '.'

interface BreadcrumbWrapperProps
extends Partial<Pick<BreadcrumbProps, 'breadcrumbList'>> {
name: string
}

const BreadcrumbWrapper = ({
breadcrumbList,
name,
}: BreadcrumbWrapperProps) => {
const fallback = [{ item: '/', name, position: 1 }]
const list = breadcrumbList ?? fallback

return <Breadcrumb breadcrumbList={list} />
}

export default memo(BreadcrumbWrapper)
1 change: 1 addition & 0 deletions src/components/ui/Breadcrumb/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default } from './Breadcrumb'
export { default as BreadcrumbWrapper } from './BreadcrumbWrapper'
export type { BreadcrumbProps } from './Breadcrumb'
4 changes: 2 additions & 2 deletions src/components/ui/Price/Price.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import { Price as UIPrice } from '@faststore/ui'
import type { PriceProps } from '@faststore/ui'

Expand Down Expand Up @@ -26,4 +26,4 @@ function Price({ classes, SRText, ...props }: Props) {
)
}

export default Price
export default memo(Price)
4 changes: 2 additions & 2 deletions src/components/ui/ProductTitle/ProductTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode } from 'react'
import React from 'react'
import React, { memo } from 'react'

import './product-title.scss'

Expand Down Expand Up @@ -35,4 +35,4 @@ function ProductTitle({ title, label, refNumber }: ProductTitleProp) {
)
}

export default ProductTitle
export default memo(ProductTitle)
4 changes: 2 additions & 2 deletions src/components/ui/QuantitySelector/QuantitySelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { memo, useEffect, useState } from 'react'
import { QuantitySelector as UIQuantitySelector } from '@faststore/ui'
import { Plus as PlusIcon, Minus as MinusIcon } from 'phosphor-react'

Expand Down Expand Up @@ -81,4 +81,4 @@ export function QuantitySelector({
)
}

export default QuantitySelector
export default memo(QuantitySelector)
37 changes: 37 additions & 0 deletions src/hooks/useSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { SearchState } from '@faststore/sdk'
import { parseSearchState } from '@faststore/sdk'
import type {
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables,
} from '@generated/graphql'
import type { PageProps } from 'gatsby'
import { useMemo } from 'react'

export type Props = PageProps<
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables
> & { slug: string }

export const useSearchParams = (props: Props): SearchState => {
const {
location: { href, pathname },
data,
} = props

const selectedFacets = data?.collection?.meta.selectedFacets

return useMemo(() => {
const maybeState = href ? parseSearchState(new URL(href)) : null

return {
page: maybeState?.page ?? 0,
base: maybeState?.base ?? pathname,
selectedFacets:
maybeState && maybeState.selectedFacets.length > 0
? maybeState.selectedFacets
: selectedFacets ?? [],
term: maybeState?.term ?? null,
sort: maybeState?.sort ?? 'score_desc',
}
}, [href, pathname, selectedFacets])
}
Loading

0 comments on commit 9302081

Please sign in to comment.