Skip to content

Commit c9ab5f1

Browse files
authored
Merge pull request #445 from commercelayer/fix/line-items-external-price
Allow line_items update using external_price
2 parents dc238d0 + ae24aa9 commit c9ab5f1

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

packages/react-components/src/components/line_items/LineItemQuantity.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ type Props = {
1616
max?: number
1717
disabled?: boolean
1818
readonly?: boolean
19+
/**
20+
* force the update of the line item price using `_external_price: true` attribute
21+
* @link https://docs.commercelayer.io/core/external-resources/external-prices
22+
*/
23+
hasExternalPrice?: boolean
1924
} & (Omit<JSX.IntrinsicElements['select'], 'children'> &
2025
Omit<JSX.IntrinsicElements['span'], 'children'>)
2126

2227
export function LineItemQuantity(props: Props): JSX.Element {
23-
const { max = 50, readonly = false, ...p } = props
28+
const { max = 50, readonly = false, hasExternalPrice, ...p } = props
2429
const { lineItem } = useContext(LineItemChildrenContext)
2530
const { updateLineItem } = useContext(LineItemContext)
2631
const options: ReactNode[] = []
@@ -33,7 +38,9 @@ export function LineItemQuantity(props: Props): JSX.Element {
3338
}
3439
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>): void => {
3540
const quantity = Number(e.target.value)
36-
if (updateLineItem && lineItem) void updateLineItem(lineItem.id, quantity)
41+
if (updateLineItem && lineItem) {
42+
void updateLineItem(lineItem.id, quantity, hasExternalPrice)
43+
}
3744
}
3845
const quantity = lineItem?.quantity
3946
const parentProps = {

packages/react-components/src/components/line_items/LineItemsContainer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ export function LineItemsContainer(props: Props): JSX.Element {
6565
const lineItemValue: LineItemContextValue = {
6666
...state,
6767
loader,
68-
updateLineItem: async (lineItemId, quantity = 1) => {
68+
updateLineItem: async (lineItemId, quantity = 1, hasExternalPrice) => {
6969
await updateLineItem({
7070
lineItemId,
7171
quantity,
72+
hasExternalPrice,
7273
dispatch,
7374
config,
7475
getOrder,

packages/react-components/src/reducers/LineItemReducer.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import getErrors from '#utils/getErrors'
1111
export interface UpdateLineItemParams {
1212
lineItemId: string
1313
quantity?: number
14+
hasExternalPrice?: boolean
1415
dispatch: Dispatch<LineItemAction>
1516
config: CommerceLayerConfig
1617
getOrder: getOrderContext | undefined
@@ -41,7 +42,11 @@ export interface LineItemPayload {
4142
}
4243

4344
export interface LineItemState extends LineItemPayload {
44-
updateLineItem?: (lineItemId: string, quantity?: number) => Promise<void>
45+
updateLineItem?: (
46+
lineItemId: string,
47+
quantity?: number,
48+
hasExternalPrice?: boolean
49+
) => Promise<void>
4550
deleteLineItem?: (lineItemId: string) => Promise<void>
4651
}
4752

@@ -95,10 +100,22 @@ export const getLineItems: GetLineItems = (params) => {
95100
export async function updateLineItem(
96101
params: UpdateLineItemParams
97102
): Promise<void> {
98-
const { config, lineItemId, quantity, getOrder, orderId, dispatch } = params
103+
const {
104+
config,
105+
lineItemId,
106+
quantity,
107+
hasExternalPrice,
108+
getOrder,
109+
orderId,
110+
dispatch
111+
} = params
99112
const sdk = getSdk(config)
100113
try {
101-
await sdk.line_items.update({ id: lineItemId, quantity })
114+
await sdk.line_items.update({
115+
id: lineItemId,
116+
quantity,
117+
_external_price: hasExternalPrice
118+
})
102119
getOrder && (await getOrder(orderId))
103120
dispatch({
104121
type: 'setErrors',

0 commit comments

Comments
 (0)