Skip to content

Commit c9a379a

Browse files
committed
Fix double place order request
1 parent 6bd7225 commit c9a379a

File tree

8 files changed

+179
-117
lines changed

8 files changed

+179
-117
lines changed

packages/react-components/src/components/orders/PlaceOrderButton.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
useRef,
66
useState,
77
type MouseEvent,
8-
type JSX,
9-
} from 'react';
8+
type JSX
9+
} from 'react'
1010
import Parent from '../utils/Parent'
1111
import { type ChildrenFunction } from '#typings/index'
1212
import PlaceOrderContext from '#context/PlaceOrderContext'
@@ -67,7 +67,8 @@ export function PlaceOrderButton(props: Props): JSX.Element {
6767
options,
6868
paymentType,
6969
setButtonRef,
70-
setPlaceOrderStatus
70+
setPlaceOrderStatus,
71+
status
7172
} = useContext(PlaceOrderContext)
7273
const [notPermitted, setNotPermitted] = useState(true)
7374
const [forceDisable, setForceDisable] = useState(disabled)
@@ -251,7 +252,8 @@ export function PlaceOrderButton(props: Props): JSX.Element {
251252
// @ts-expect-error no type
252253
ref?.current?.disabled === false &&
253254
currentCustomerPaymentSourceId == null &&
254-
autoPlaceOrder
255+
autoPlaceOrder &&
256+
status === 'standby'
255257
) {
256258
// NOTE: This is a workaround for the case when the user reloads the page after selecting a customer payment source
257259
if (
@@ -344,11 +346,19 @@ export function PlaceOrderButton(props: Props): JSX.Element {
344346
paymentSource: checkPaymentSource,
345347
currentCustomerPaymentSourceId
346348
}))
347-
setForceDisable(false)
348-
onClick && placed && onClick(placed)
349-
setIsLoading(false)
350-
if (setPlaceOrderStatus != null) {
351-
setPlaceOrderStatus({ status: 'standby' })
349+
if (placed && setPlaceOrderStatus != null) {
350+
if (placed.placed) {
351+
setPlaceOrderStatus({ status: 'placing' })
352+
onClick && placed && onClick(placed)
353+
} else {
354+
setForceDisable(false)
355+
onClick && placed && onClick(placed)
356+
setIsLoading(false)
357+
setPlaceOrderStatus({ status: 'standby' })
358+
}
359+
} else {
360+
setForceDisable(false)
361+
setIsLoading(false)
352362
}
353363
}
354364
const disabledButton = disabled !== undefined ? disabled : notPermitted

packages/react-components/src/components/payment_gateways/AdyenGateway.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import PaymentSourceContext from '#context/PaymentSourceContext'
88
import { type PaymentResource } from '#reducers/PaymentMethodReducer'
99
import { type StripeElementLocale } from '@stripe/stripe-js'
1010
import isEmpty from 'lodash/isEmpty'
11-
import { useContext, type JSX } from 'react';
11+
import { useContext, type JSX } from 'react'
1212
import AdyenPayment from '#components/payment_source/AdyenPayment'
1313
import PaymentCardsTemplate from '../utils/PaymentCardsTemplate'
1414
import { jwt } from '#utils/jwt'
1515
import getCardDetails from '#utils/getCardDetails'
1616
import { getPaymentAttributes } from '#utils/getPaymentAttributes'
17-
import { canPlaceOrder } from '#utils/canPlaceOrder'
1817

1918
type Props = GatewayBaseType
2019

@@ -38,8 +37,6 @@ export function AdyenGateway(props: Props): JSX.Element | null {
3837
useContext(PaymentMethodContext)
3938
const paymentResource: PaymentResource = 'adyen_payments'
4039
const locale = order?.language_code as StripeElementLocale
41-
console.log('AdyenGateway order', order, order?.status)
42-
console.log('AdyenGateway order can place? --- ', order && canPlaceOrder(order))
4340
if (!readonly && payment?.id !== currentPaymentMethodId) return null
4441
// @ts-expect-error no type
4542
const clientKey = paymentSource?.public_key

packages/react-components/src/components/payment_methods/PaymentMethod.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { useState, useEffect, type MouseEvent, useContext, type JSX } from 'react';
1+
import {
2+
useState,
3+
useEffect,
4+
type MouseEvent,
5+
useContext,
6+
type JSX
7+
} from 'react'
28
import PaymentMethodContext from '#context/PaymentMethodContext'
39
import PaymentMethodChildrenContext from '#context/PaymentMethodChildrenContext'
410
import type { LoaderType } from '#typings'
@@ -19,6 +25,7 @@ import {
1925
import { isEmpty } from '#utils/isEmpty'
2026
import { getAvailableExpressPayments } from '#utils/expressPaymentHelper'
2127
import PlaceOrderContext from '#context/PlaceOrderContext'
28+
import { sortPaymentMethods } from '#utils/payment-methods/sortPaymentMethods'
2229

2330
export interface PaymentMethodOnClickParams {
2431
payment?: PaymentMethodType | Record<string, any>
@@ -48,6 +55,10 @@ type Props = {
4855
* Enable express payment. Other payment methods will be disabled.
4956
*/
5057
expressPayments?: boolean
58+
/**
59+
* Sort payment methods by an array of strings
60+
*/
61+
sortBy?: Array<PaymentMethodType['payment_source_type']>
5162
} & Omit<JSX.IntrinsicElements['div'], 'onClick' | 'children'> &
5263
(
5364
| {
@@ -72,6 +83,7 @@ export function PaymentMethod({
7283
expressPayments,
7384
hide,
7485
onClick,
86+
sortBy,
7587
...p
7688
}: Props): JSX.Element {
7789
const [loading, setLoading] = useState(true)
@@ -200,7 +212,13 @@ export function PaymentMethod({
200212
setPaymentSelected('')
201213
}
202214
}, [paymentMethods, currentPaymentMethodId])
203-
const components = paymentMethods
215+
216+
const sortedPaymentMethods =
217+
paymentMethods != null && sortBy != null
218+
? sortPaymentMethods(paymentMethods, sortBy)
219+
: paymentMethods
220+
221+
const components = sortedPaymentMethods
204222
?.filter((payment) => {
205223
if (Array.isArray(hide)) {
206224
const source = payment?.payment_source_type as PaymentResource

packages/react-components/src/components/payment_methods/PaymentMethodsContainer.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
import PaymentMethodContext, {
33
defaultPaymentMethodContext
44
} from '#context/PaymentMethodContext'
5-
import { type ReactNode, useContext, useEffect, useReducer, useMemo, type JSX } from 'react';
5+
import {
6+
type ReactNode,
7+
useContext,
8+
useEffect,
9+
useReducer,
10+
useMemo,
11+
type JSX
12+
} from 'react'
613
import paymentMethodReducer, {
714
paymentMethodInitialState,
815
getPaymentMethods,

0 commit comments

Comments
 (0)