Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: customer signup loop #2191

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading