Skip to content

Commit

Permalink
feat: Add new prop typeAccepted to LineItemsCount
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Dec 21, 2021
1 parent 231863c commit 1317bdd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
13 changes: 9 additions & 4 deletions src/components/LineItemsCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, {
useEffect,
} from 'react'
import Parent from './utils/Parent'
import getLineItemsCount from '#utils/getLineItemsCount'
import getLineItemsCount, { TypeAccepted } from '#utils/getLineItemsCount'
import { isEmpty } from 'lodash'
import LineItemContext from '#context/LineItemContext'
import components from '#config/components'
Expand All @@ -22,23 +22,28 @@ type LineItemsCountChildrenProps = FunctionChildren<

type LineItemsCountProps = {
children?: LineItemsCountChildrenProps
typeAccepted?: TypeAccepted[]
} & JSX.IntrinsicElements['span']

const LineItemsCount: FunctionComponent<LineItemsCountProps> = (props) => {
const { children, ...p } = props
const { children, typeAccepted, ...p } = props
const { lineItems } = useContext(LineItemContext)
const [quantity, setQuantity] = useState(0)
useEffect(() => {
if (!isEmpty(lineItems)) {
const qty = getLineItemsCount(lineItems || [])
const qty = getLineItemsCount({
lineItems: lineItems || [],
typeAccepted,
})
setQuantity(qty)
}
return (): void => {
setQuantity(0)
}
}, [lineItems])
}, [lineItems, typeAccepted])
const parentProps = {
quantity,
typeAccepted,
...p,
}
return children ? (
Expand Down
2 changes: 1 addition & 1 deletion src/components/LineItemsEmpty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const LineItemsEmpty: FunctionComponent<LineItemsCountProps> = (props) => {
const emptyText = quantity === 0 ? <span {...p}>{text}</span> : null
useEffect(() => {
if (!isEmpty(lineItems)) {
const qty = getLineItemsCount(lineItems || [])
const qty = getLineItemsCount({ lineItems: lineItems || [] })
setQuantity(qty)
}
return (): void => {
Expand Down
8 changes: 8 additions & 0 deletions src/config/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ const components = {
required: true,
},
},
AdjustmentAmount: {
displayName: 'AdjustmentAmount',
propTypes: baseOrderComponentPricePropTypes,
defaultProps: {
format: 'formatted' as BaseFormatPrice,
},
},
AddToCartButton: {
displayName: 'AddToCartButton',
propTypes: {
Expand Down Expand Up @@ -563,6 +570,7 @@ const components = {
'LineItemsContainer',
'SubTotalAmount',
'DiscountAmount',
'AdjustmentAmount',
'ShippingAmount',
'TaxesAmount',
'GiftCardAmount',
Expand Down
21 changes: 10 additions & 11 deletions src/utils/getLineItemsCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import { LineItem } from '@commercelayer/sdk'
import { LineItemType } from '#typings'

export interface GetLineItemsCountInterface {
(lineItems: LineItem[], quantity?: number): number
(args: {
lineItems: LineItem[]
quantity?: number
typeAccepted?: TypeAccepted[]
}): number
}

type TypeAccepted = Extract<
export type TypeAccepted = Extract<
LineItemType,
'skus' | 'gift_cards' | 'bundles' | 'adjustments'
>

const getLineItemsCount: GetLineItemsCountInterface = (
const getLineItemsCount: GetLineItemsCountInterface = ({
lineItems,
quantity = 0
) => {
const typeAccepted: TypeAccepted[] = [
'skus',
'gift_cards',
'bundles',
'adjustments',
]
quantity = 0,
typeAccepted = ['skus', 'gift_cards', 'bundles', 'adjustments'],
}) => {
lineItems
.filter(
(l) => l.item_type && typeAccepted.includes(l.item_type as TypeAccepted)
Expand Down

0 comments on commit 1317bdd

Please sign in to comment.