Skip to content

Commit

Permalink
perf: Add autoSelectSingleShippingMethod prop to Shipment component
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed May 25, 2022
1 parent 9be2795 commit d24b6f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
41 changes: 23 additions & 18 deletions src/components/Shipment.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import React, {
Fragment,
useContext,
ReactNode,
useState,
useEffect,
} from 'react'
import { useContext, ReactNode, useState, useEffect } from 'react'
import ShipmentContext from '#context/ShipmentContext'
import ShipmentChildrenContext from '#context/ShipmentChildrenContext'
import components from '#config/components'
Expand All @@ -18,16 +12,28 @@ const displayName = components.Shipment.displayName
type ShipmentProps = {
children: ReactNode
loader?: LoaderType
autoSelectSingleShippingMethod?: boolean
}

const Shipment: React.FunctionComponent<ShipmentProps> = ({
export default function Shipment({
children,
loader = 'Loading...',
}) => {
autoSelectSingleShippingMethod = false,
}: ShipmentProps) {
const [loading, setLoading] = useState(true)
const { shipments, deliveryLeadTimes } = useContext(ShipmentContext)
const { shipments, deliveryLeadTimes, setShippingMethod } =
useContext(ShipmentContext)
useEffect(() => {
if (shipments) setLoading(false)
if (autoSelectSingleShippingMethod && shipments) {
shipments.forEach((shipment) => {
const isSingle = shipment?.available_shipping_methods?.length === 1
if (!shipment?.shipping_method && isSingle) {
const [shippingMethod] = shipment?.available_shipping_methods || []
setShippingMethod(shipment?.id, shippingMethod?.id)
}
})
}
return () => {
setLoading(true)
}
Expand All @@ -43,7 +49,12 @@ const Shipment: React.FunctionComponent<ShipmentProps> = ({
return l
})
const shippingMethods = shipment.available_shipping_methods
const currentShippingMethodId = shipment.shipping_method?.id
const currentShippingMethodId =
autoSelectSingleShippingMethod &&
shippingMethods &&
shippingMethods.length === 1
? shippingMethods[0].id
: shipment.shipping_method?.id
const stockTransfers = shipment.stock_transfers
const times = deliveryLeadTimes?.filter(
(time) => time.stock_location?.id === shipment.stock_location?.id
Expand All @@ -63,14 +74,8 @@ const Shipment: React.FunctionComponent<ShipmentProps> = ({
</ShipmentChildrenContext.Provider>
)
})
return !loading ? (
<Fragment>{components}</Fragment>
) : (
getLoaderComponent(loader)
)
return !loading ? <>{components}</> : getLoaderComponent(loader)
}

Shipment.propTypes = propTypes
Shipment.displayName = displayName

export default Shipment
4 changes: 2 additions & 2 deletions src/components/ShippingMethod.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {
import {
FunctionComponent,
Fragment,
useContext,
Expand All @@ -10,7 +10,7 @@ import ShippingMethodChildrenContext from '#context/ShippingMethodChildrenContex
import components from '#config/components'
import ShipmentChildrenContext from '#context/ShipmentChildrenContext'
import isEmpty from 'lodash/isEmpty'
import { DeliveryLeadTime } from '@commercelayer/sdk'
import type { DeliveryLeadTime } from '@commercelayer/sdk'

const propTypes = components.ShippingMethod.propTypes
const displayName = components.ShippingMethod.displayName
Expand Down
4 changes: 2 additions & 2 deletions src/components/ShippingMethodRadioButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext, FunctionComponent, ReactNode } from 'react'
import { useContext, FunctionComponent, ReactNode, useEffect } from 'react'
import ShippingMethodChildrenContext from '#context/ShippingMethodChildrenContext'
import Parent from './utils/Parent'
import components from '#config/components'
import ShipmentContext from '#context/ShipmentContext'
import { ShippingMethod } from '@commercelayer/sdk'
import type { ShippingMethod } from '@commercelayer/sdk'

const propTypes = components.ShippingMethodRadioButton.propTypes
const displayName = components.ShippingMethodRadioButton.displayName
Expand Down

0 comments on commit d24b6f7

Please sign in to comment.