Skip to content

Commit

Permalink
Merge pull request #2221 from graphcommerce-org/fix/customer-session
Browse files Browse the repository at this point in the history
After a user just logged in the checkout, the useFormGqlMutationCart would still run even though the cart was locked.
  • Loading branch information
paales authored Mar 21, 2024
2 parents 36e0921 + 0ba1fdc commit 15ba06f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-pets-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-cart': patch
---

After a user just logged in the checkout, the useFormGqlMutationCart would still run even though the cart was locked.
19 changes: 13 additions & 6 deletions packages/magento-cart/hooks/useFormGqlMutationCart.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MutationHookOptions, TypedDocumentNode } from '@graphcommerce/graphql'
import { MutationHookOptions, TypedDocumentNode, useApolloClient } from '@graphcommerce/graphql'
import {
useFormGqlMutation,
UseFormGqlMutationReturn,
UseFormGraphQlOptions,
} from '@graphcommerce/react-hook-form'
import { CurrentCartIdDocument } from './CurrentCartId.gql'
import { useCartIdCreate } from './useCartIdCreate'

export function useFormGqlMutationCart<
Expand All @@ -15,14 +16,20 @@ export function useFormGqlMutationCart<
operationOptions?: MutationHookOptions<Q, V>,
): UseFormGqlMutationReturn<Q, V> {
const cartId = useCartIdCreate()
const client = useApolloClient()

const onBeforeSubmit = async (variables: V) => {
const vars = { ...variables, cartId: await cartId() }
return options.onBeforeSubmit ? options.onBeforeSubmit(vars) : vars
}
const result = useFormGqlMutation<Q, V>(
document,
{ ...options, onBeforeSubmit },
{
...options,
onBeforeSubmit: async (variables) => {
const vars = { ...variables, cartId: await cartId() }

const res = client.cache.readQuery({ query: CurrentCartIdDocument })
if (res?.currentCartId?.locked) return false
return options.onBeforeSubmit ? options.onBeforeSubmit(vars) : vars
},
},
{ errorPolicy: 'all', ...operationOptions },
)

Expand Down

0 comments on commit 15ba06f

Please sign in to comment.