Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Button, Form, FormActions, FormRow } from '@graphcommerce/next-ui'
import { useFormGqlMutation } from '@graphcommerce/react-hook-form'
import { Trans } from '@lingui/react'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import { ApolloCustomerErrorAlert } from '../ApolloCustomerError/ApolloCustomerErrorAlert'
import { ValidatedPasswordElement } from '../ValidatedPasswordElement/ValidatedPasswordElement'
import type { ResetPasswordMutation, ResetPasswordMutationVariables } from './ResetPassword.gql'
Expand All @@ -15,6 +16,8 @@ export type ResetPasswordFormProps = {

export function ResetPasswordForm(props: ResetPasswordFormProps) {
const { token, buttonProps } = props
const router = useRouter()
const emailFromUrl = router.query.email as string | undefined

const form = useFormGqlMutation<
ResetPasswordMutation,
Expand All @@ -26,14 +29,22 @@ export function ResetPasswordForm(props: ResetPasswordFormProps) {
...formData,
resetPasswordToken: token,
}),
defaultValues: {
email: '',
},
},
{ errorPolicy: 'all' },
)

const router = useRouter()
const { handleSubmit, data, formState, error, control } = form
const { handleSubmit, data, formState, error, control, reset } = form
const submitHandler = handleSubmit(() => {})

useEffect(() => {
if (emailFromUrl) {
reset({ email: emailFromUrl })
}
}, [emailFromUrl, reset])

if (formState.isSubmitSuccessful && data && !error) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
router.replace(`${window.location.href.split('?')[0]}?success=1`)
Expand All @@ -48,6 +59,7 @@ export function ResetPasswordForm(props: ResetPasswordFormProps) {
variant='outlined'
required
disabled={formState.isSubmitting}
InputProps={{ readOnly: true }}
/>
</FormRow>
<FormRow>
Expand Down
Loading