-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2205 from graphcommerce-org/fix/customer-cart-ass…
…ignment Solve an issue where the user would be presented with the Session expired dialog when the user would be logging in during the checkout process.
- Loading branch information
Showing
25 changed files
with
175 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcommerce/ecommerce-ui': patch | ||
--- | ||
|
||
`<WaitForQueries/>` will default to loading, restoring the previous behavior. This might introduce , this might introduce an additional spinner but prevents a flash where it is shown that there is no cart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphcommerce/magento-cart-shipping-address': patch | ||
'@graphcommerce/magento-newsletter': patch | ||
'@graphcommerce/magento-cart': patch | ||
--- | ||
|
||
Deprecate the allowUrl option for useCartQuery, it was already enabled by default and should never be set to false. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphcommerce/magento-cart": patch | ||
"@graphcommerce/magento-customer": patch | ||
--- | ||
|
||
Solve an issue where the user would be presented with the Session expired dialog when the user would be logging in during the checkout process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ query CurrentCartId { | |
currentCartId @client { | ||
__typename | ||
id | ||
locked | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ extend type Query { | |
|
||
type CurrentCartId { | ||
id: String | ||
locked: Boolean | ||
} | ||
|
||
input RegisterCartIdInput { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ query CustomerCart { | |
customerCart { | ||
id | ||
__typename | ||
...CartItemCountChanged | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...o-cart/hooks/UseMergeCustomerCart.graphql → ...ges/magento-cart/hooks/MergeCarts.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
mutation UseMergeCustomerCart($sourceCartId: String!, $destinationCartId: String!) { | ||
mutation MergeCarts($sourceCartId: String!, $destinationCartId: String!) { | ||
mergeCarts(source_cart_id: $sourceCartId, destination_cart_id: $destinationCartId) { | ||
__typename | ||
id | ||
...CartItemCountChanged | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,12 @@ | ||
import { useApolloClient } from '@graphcommerce/graphql' | ||
import { cookie } from '@graphcommerce/next-ui' | ||
import { CurrentCartIdDocument } from './CurrentCartId.gql' | ||
import { CART_ID_COOKIE } from './useAssignCurrentCartId' | ||
|
||
export function useClearCurrentCartId() { | ||
const { cache } = useApolloClient() | ||
|
||
return () => { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,6 @@ | ||
import { useMutation } from '@graphcommerce/graphql' | ||
import { useCustomerQuery } from '@graphcommerce/magento-customer' | ||
import { useEffect } from 'react' | ||
import { CustomerCartDocument } from './CustomerCart.gql' | ||
import { UseMergeCustomerCartDocument } from './UseMergeCustomerCart.gql' | ||
import { useAssignCurrentCartId } from './useAssignCurrentCartId' | ||
import { useCurrentCartId } from './useCurrentCartId' | ||
|
||
/** | ||
* - Automatically assign the customer cart as the current cart | ||
* - Merge the guest cart into the customer cart | ||
* @deprecated Is replaced by the useSignInFormMergeCart plugin. | ||
*/ | ||
export function useMergeCustomerCart() { | ||
const { currentCartId } = useCurrentCartId() | ||
const assignCurrentCartId = useAssignCurrentCartId() | ||
const [merge] = useMutation(UseMergeCustomerCartDocument, { errorPolicy: 'all' }) | ||
|
||
const destinationCartId = useCustomerQuery(CustomerCartDocument, { fetchPolicy: 'network-only' }) | ||
?.data?.customerCart.id | ||
|
||
useEffect(() => { | ||
// 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 the visitor has a guest cart, try merging it into the customer cart | ||
if (currentCartId) { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
merge({ variables: { sourceCartId: currentCartId, destinationCartId } }) | ||
// We're not handling exceptions here: | ||
// If the merge returns an error, we'll use the customer cart without merging the guest cart. | ||
.catch((e) => { | ||
console.error('Error merging carts', e) | ||
}) | ||
.finally(() => { | ||
// Assign the customer cart as the new cart id | ||
assignCurrentCartId(destinationCartId) | ||
}) | ||
} else { | ||
assignCurrentCartId(destinationCartId) | ||
} | ||
}, [assignCurrentCartId, destinationCartId, merge, currentCartId]) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useApolloClient } from '@graphcommerce/graphql' | ||
import type { useSignInForm } from '@graphcommerce/magento-customer/hooks/useSignInForm' | ||
import type { MethodPlugin } from '@graphcommerce/next-config' | ||
import { cartLock, readCartId, useAssignCurrentCartId } from '../hooks' | ||
import { CustomerCartDocument } from '../hooks/CustomerCart.gql' | ||
import { MergeCartsDocument } from '../hooks/MergeCarts.gql' | ||
|
||
export const func = 'useSignInForm' | ||
export const exported = '@graphcommerce/magento-customer/hooks/useSignInForm' | ||
|
||
const useSignInFormMergeCart: MethodPlugin<typeof useSignInForm> = (useSignInForm, options) => { | ||
const client = useApolloClient() | ||
const assignCurrentCartId = useAssignCurrentCartId() | ||
|
||
return useSignInForm({ | ||
...options, | ||
onComplete: async (data, variables) => { | ||
await options.onComplete?.(data, variables) | ||
|
||
cartLock(client.cache, true) | ||
|
||
const destinationCartId = ( | ||
await client.query({ | ||
query: CustomerCartDocument, | ||
fetchPolicy: 'network-only', | ||
}) | ||
).data.customerCart.id | ||
|
||
try { | ||
const sourceCartId = readCartId(client.cache)?.id | ||
if (sourceCartId && sourceCartId !== destinationCartId) { | ||
await client.mutate({ | ||
mutation: MergeCartsDocument, | ||
variables: { sourceCartId, destinationCartId }, | ||
}) | ||
} | ||
} catch (error) { | ||
console.error( | ||
'Error merging carts, continuing without merging, this might cause issues.', | ||
error, | ||
) | ||
} finally { | ||
// Assign the customer cart as the new cart id | ||
assignCurrentCartId(destinationCartId) | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
export const plugin = useSignInFormMergeCart |
Oops, something went wrong.