From 67c8327a966ae8c56c15e67568d0f69e1cfe077f Mon Sep 17 00:00:00 2001 From: Paul Hachmang Date: Fri, 16 Feb 2024 16:31:53 +0100 Subject: [PATCH] Lock the current cart so the queries do not get refreshed during load --- .../magento-cart/hooks/CurrentCartId.graphql | 1 + .../magento-cart/hooks/CurrentCartId.graphqls | 1 + .../hooks/useAssignCurrentCartId.ts | 2 +- packages/magento-cart/hooks/useCartQuery.ts | 10 +++---- .../hooks/useClearCurrentCartId.ts | 6 +---- .../magento-cart/hooks/useCurrentCartId.ts | 7 ++++- .../plugins/useSignInFormMergeCart.ts | 26 +++++++++++++------ .../hooks/CustomerToken.graphqls | 2 +- 8 files changed, 32 insertions(+), 23 deletions(-) diff --git a/packages/magento-cart/hooks/CurrentCartId.graphql b/packages/magento-cart/hooks/CurrentCartId.graphql index 0ed32d02f5..a8b4b8a3bf 100644 --- a/packages/magento-cart/hooks/CurrentCartId.graphql +++ b/packages/magento-cart/hooks/CurrentCartId.graphql @@ -2,5 +2,6 @@ query CurrentCartId { currentCartId @client { __typename id + locked } } diff --git a/packages/magento-cart/hooks/CurrentCartId.graphqls b/packages/magento-cart/hooks/CurrentCartId.graphqls index e9cee2a29c..4856711677 100644 --- a/packages/magento-cart/hooks/CurrentCartId.graphqls +++ b/packages/magento-cart/hooks/CurrentCartId.graphqls @@ -4,6 +4,7 @@ extend type Query { type CurrentCartId { id: String + locked: Boolean } input RegisterCartIdInput { diff --git a/packages/magento-cart/hooks/useAssignCurrentCartId.ts b/packages/magento-cart/hooks/useAssignCurrentCartId.ts index de62813bec..4aeafa9dd1 100644 --- a/packages/magento-cart/hooks/useAssignCurrentCartId.ts +++ b/packages/magento-cart/hooks/useAssignCurrentCartId.ts @@ -8,7 +8,7 @@ export const CART_ID_COOKIE = 'cart' export function writeCartId(cache: ApolloCache, id: string | null = null) { cache.writeQuery({ query: CurrentCartIdDocument, - data: { currentCartId: { __typename: 'CurrentCartId', id } }, + data: { currentCartId: { __typename: 'CurrentCartId', locked: false, id } }, broadcast: true, }) } diff --git a/packages/magento-cart/hooks/useCartQuery.ts b/packages/magento-cart/hooks/useCartQuery.ts index d93e84d981..67c457d42f 100644 --- a/packages/magento-cart/hooks/useCartQuery.ts +++ b/packages/magento-cart/hooks/useCartQuery.ts @@ -20,13 +20,14 @@ export function useCartQuery) - - return { - ...result, - // error: called && !currentCartId ? noCartError : result.error, - } + return useQuery(document, queryOptions as QueryHookOptions) } diff --git a/packages/magento-cart/hooks/useClearCurrentCartId.ts b/packages/magento-cart/hooks/useClearCurrentCartId.ts index 5590918cba..662d2cfd06 100644 --- a/packages/magento-cart/hooks/useClearCurrentCartId.ts +++ b/packages/magento-cart/hooks/useClearCurrentCartId.ts @@ -10,11 +10,7 @@ export function useClearCurrentCartId() { const id = cache.readQuery({ query: CurrentCartIdDocument })?.currentCartId?.id if (!id) return - cache.writeQuery({ - query: CurrentCartIdDocument, - data: { currentCartId: { __typename: 'CurrentCartId', id: null } }, - broadcast: true, - }) + cache.evict({ fieldName: 'currentCartId', broadcast: true }) cookie(CART_ID_COOKIE, null) } } diff --git a/packages/magento-cart/hooks/useCurrentCartId.ts b/packages/magento-cart/hooks/useCurrentCartId.ts index 47fae36cda..a6da6b5e85 100644 --- a/packages/magento-cart/hooks/useCurrentCartId.ts +++ b/packages/magento-cart/hooks/useCurrentCartId.ts @@ -10,5 +10,10 @@ export function useCurrentCartId< V extends CurrentCartIdQueryVariables, >(options: QueryHookOptions = {}) { const queryResults = useQuery(CurrentCartIdDocument, options) - return { currentCartId: queryResults.data?.currentCartId?.id || '', ...queryResults } + + return { + currentCartId: queryResults.data?.currentCartId?.id || '', + locked: queryResults.data?.currentCartId?.locked || false, + ...queryResults, + } } diff --git a/packages/magento-cart/plugins/useSignInFormMergeCart.ts b/packages/magento-cart/plugins/useSignInFormMergeCart.ts index 10bdcbbca6..53d1b2daf1 100644 --- a/packages/magento-cart/plugins/useSignInFormMergeCart.ts +++ b/packages/magento-cart/plugins/useSignInFormMergeCart.ts @@ -1,7 +1,8 @@ import { useApolloClient } from '@graphcommerce/graphql' import type { useSignInForm } from '@graphcommerce/magento-customer/hooks/useSignInForm' import type { MethodPlugin } from '@graphcommerce/next-config' -import { useCurrentCartId, useAssignCurrentCartId } from '../hooks' +import { useAssignCurrentCartId } from '../hooks' +import { CurrentCartIdDocument } from '../hooks/CurrentCartId.gql' import { CustomerCartDocument } from '../hooks/CustomerCart.gql' import { UseMergeCustomerCartDocument } from '../hooks/UseMergeCustomerCart.gql' @@ -9,31 +10,40 @@ export const func = 'useSignInForm' export const exported = '@graphcommerce/magento-customer/hooks/useSignInForm' const useSignInFormMergeCart: MethodPlugin = (useSignInForm, options) => { - const { currentCartId } = useCurrentCartId() const client = useApolloClient() const assignCurrentCartId = useAssignCurrentCartId() return useSignInForm({ ...options, onComplete: async (data, variables) => { + const currentCartId = client.cache.readQuery({ query: CurrentCartIdDocument })?.currentCartId + if (currentCartId?.id && !currentCartId.locked) { + client.cache.writeQuery({ + query: CurrentCartIdDocument, + data: { currentCartId: { ...currentCartId, locked: true } }, + }) + } + await options.onComplete?.(data, variables) - const destinationCartId = ( - await client.query({ query: CustomerCartDocument, fetchPolicy: 'network-only' }) - ).data.customerCart.id + const customerCart = await client.query({ + query: CustomerCartDocument, + fetchPolicy: 'network-only', + }) + const destinationCartId = customerCart.data.customerCart.id // If we don't have a customer cart, we're done // If the vistor cart is the same as the customer cart, we're done - if (!destinationCartId || currentCartId === destinationCartId) return + if (!destinationCartId || currentCartId?.id === destinationCartId) return // Assign the customer cart as the new cart id assignCurrentCartId(destinationCartId) - if (currentCartId) { + if (currentCartId?.id) { await client .mutate({ mutation: UseMergeCustomerCartDocument, - variables: { sourceCartId: currentCartId, destinationCartId }, + variables: { sourceCartId: currentCartId.id, destinationCartId }, }) // We're not handling exceptions here: // If the merge returns an error, we'll use the customer cart without merging the guest cart. diff --git a/packages/magento-customer/hooks/CustomerToken.graphqls b/packages/magento-customer/hooks/CustomerToken.graphqls index a62a01fba0..60f0b7434e 100644 --- a/packages/magento-customer/hooks/CustomerToken.graphqls +++ b/packages/magento-customer/hooks/CustomerToken.graphqls @@ -3,6 +3,6 @@ extend type Query { } extend type CustomerToken { - createdAt: String + createdAt: String @deprecated(reason: "Value is not used in GraphCommerce, but still filled in.") valid: Boolean }