Skip to content

Commit 9955768

Browse files
committed
fix: avoid checkout error when refreshing quickly
1 parent 1ab6dbf commit 9955768

7 files changed

Lines changed: 16 additions & 11 deletions

File tree

components/hooks/useSettingsOrInvalid.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useRouter } from "next/router"
2+
import { useRef } from "react"
23
import useSWR from "swr"
34

45
const fetcher = (url: string) => fetch(url).then((r) => r.json())
@@ -9,13 +10,17 @@ interface UseSettingsOrInvalid {
910
}
1011

1112
export const useSettingsOrInvalid = (): UseSettingsOrInvalid => {
13+
const random = useRef(Date.now())
1214
const router = useRouter()
1315
const { orderId, accessToken, paymentReturn } = router.query
1416
const paymentReturnQuery =
1517
paymentReturn === "true" ? "&paymentReturn=true" : ""
1618
const { data, error } = useSWR(
1719
router.isReady
18-
? `/api/settings?accessToken=${accessToken}&orderId=${orderId}${paymentReturnQuery}`
20+
? [
21+
`/api/settings?accessToken=${accessToken}&orderId=${orderId}${paymentReturnQuery}`,
22+
random,
23+
]
1924
: null,
2025
fetcher,
2126
{ revalidateOnFocus: false }

cypress/integration/checkout-bundle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("Checkout Bundle", () => {
88
const email = internet.email().toLocaleLowerCase()
99
const password = internet.password()
1010

11-
const returnUrl = "https://commercelayer.io"
11+
const returnUrl = "https://commercelayer.io/"
1212

1313
before(function () {
1414
cy.createCustomer({ email: email, password: password }).then(() => {

cypress/integration/checkout-complete.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("Checkout Complete", () => {
88
const email = internet.email().toLocaleLowerCase()
99
const password = internet.password()
1010

11-
const returnUrl = "https://commercelayer.io"
11+
const returnUrl = "https://commercelayer.io/"
1212

1313
before(function () {
1414
cy.createCustomer({ email: email, password: password }).then(() => {

cypress/integration/checkout-digital.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { internet } from "faker"
33
import { euAddress } from "../support/utils"
44

55
describe("Checkout Checkout-Digital", () => {
6-
const returnUrl = "https://commercelayer.io"
6+
const returnUrl = "https://commercelayer.io/"
77

88
const filename = "checkout-digital"
99

cypress/integration/checkout-free-payment.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("Checkout Free Payment", () => {
88
const email = internet.email().toLocaleLowerCase()
99
const password = internet.password()
1010

11-
const returnUrl = "https://commercelayer.io"
11+
const returnUrl = "https://commercelayer.io/"
1212

1313
before(function () {
1414
cy.createCustomer({ email: email, password: password }).then(() => {

cypress/integration/checkout-only-bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("Checkout Only Bundle", () => {
88
const email = internet.email().toLocaleLowerCase()
99
const password = internet.password()
1010

11-
const returnUrl = "https://commercelayer.io"
11+
const returnUrl = "https://commercelayer.io/"
1212

1313
before(function () {
1414
cy.createCustomer({ email: email, password: password }).then(() => {

pages/api/settings.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
7777
let order
7878

7979
try {
80-
const orderFetched: Order = await cl.orders.retrieve(orderId, {
80+
order = await cl.orders.retrieve(orderId, {
8181
fields: {
8282
orders: [
8383
"id",
@@ -93,15 +93,15 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
9393
include: ["line_items"],
9494
})
9595

96-
if (orderFetched.status === "draft" || orderFetched.status === "pending") {
96+
if (order.status === "draft" || order.status === "pending") {
9797
const _refresh = !paymentReturn
9898

9999
order = await cl.orders.update({
100-
id: orderFetched.id,
100+
id: order.id,
101101
_refresh,
102-
...(!orderFetched.autorefresh && { autorefresh: true }),
102+
...(!order.autorefresh && { autorefresh: true }),
103103
})
104-
} else if (orderFetched.status === "placed") {
104+
} else if (order.status === "placed") {
105105
order = await cl.orders.retrieve(orderId)
106106
}
107107
} catch (e) {

0 commit comments

Comments
 (0)