Skip to content

Commit

Permalink
Merge pull request #2191 from graphcommerce-org/fix/customer-signup-loop
Browse files Browse the repository at this point in the history
fix: customer signup loop
  • Loading branch information
paales authored Feb 9, 2024
2 parents b2218dc + 13ffa6b commit b13eb7a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/tall-phones-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphcommerce/magento-customer": patch
"@graphcommerce/next-ui": patch
---

When a user was logging in from the checkout react would be caught in an infinite loop and thus the page would hang
21 changes: 12 additions & 9 deletions packages/magento-customer/hooks/useAccountSignInUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IsEmailAvailableQueryVariables,
} from './IsEmailAvailable.gql'
import { useCustomerSession } from './useCustomerSession'
import { useUrlQuery } from '@graphcommerce/next-ui'

export type UseFormIsEmailAvailableProps = {
onSubmitted?: (data: { email: string }) => void
Expand All @@ -22,6 +23,7 @@ export const isToggleMethod = !import.meta.graphCommerce.enableGuestCheckoutLogi
export function useAccountSignInUpForm(props: UseFormIsEmailAvailableProps = {}) {
const { onSubmitted } = props
const { token, valid } = useCustomerSession()
const [queryState, setRouterQuery] = useUrlQuery<{ email?: string | null }>()

const customerQuery = useQuery(CustomerDocument, { fetchPolicy: 'cache-only' })
const cachedEmail = customerQuery?.data?.customer?.email
Expand All @@ -39,19 +41,20 @@ export function useAccountSignInUpForm(props: UseFormIsEmailAvailableProps = {})
},
{ fetchPolicy: 'cache-and-network' },
)
const { formState, data, handleSubmit } = form
const { formState, data, handleSubmit, setValue, trigger } = form
const { isSubmitSuccessful, isValid } = formState

const router = useRouter()
useEffect(() => {
const emailFromParams = router.query.email as string
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
if (!cachedEmail && queryState.email) {
await setRouterQuery({ email: null })

if (!cachedEmail && emailFromParams) {
form.setValue('email', emailFromParams)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
form.trigger('email')
}
}, [cachedEmail, router.query.email, form])
setValue('email', queryState.email)
await trigger('email')
}
})()
}, [cachedEmail, queryState.email, setRouterQuery, setValue, trigger])

const submit = isToggleMethod
? async (e?: React.BaseSyntheticEvent) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-ui/hooks/useUrlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useUrlQuery<T extends Record<string, string | null>>() {
Object.entries({ ...current, ...incoming }).filter(([, value]) => value !== null),
)

if (JSON.stringify(current) === JSON.stringify(newQuery)) return undefined
if (JSON.stringify(current) === JSON.stringify(newQuery)) return Promise.resolve(true)

return replace({ query: newQuery }, undefined, { shallow: true })
},
Expand Down

0 comments on commit b13eb7a

Please sign in to comment.