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: check form error on first proceed to pay #6135

Merged
merged 1 commit into from
Apr 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const FormFields = ({
const {
reset,
formState: { isDirty },
trigger,
} = formMethods

// Reset default values when they change
Expand Down Expand Up @@ -143,6 +144,7 @@ export const FormFields = ({
formFields={augmentedFormFields}
formLogics={formLogics}
colorTheme={colorTheme}
trigger={trigger}
/>
</form>
</FormProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MouseEventHandler, useMemo } from 'react'
import { useFormState, useWatch } from 'react-hook-form'
import { useFormState, UseFormTrigger, useWatch } from 'react-hook-form'
import { Stack, useDisclosure, VisuallyHidden } from '@chakra-ui/react'

import {
Expand All @@ -25,6 +25,7 @@ interface PublicFormSubmitButtonProps {
formLogics: LogicDto[]
colorTheme: string
onSubmit: MouseEventHandler<HTMLButtonElement> | undefined
trigger: UseFormTrigger<FormFieldValues>
}

/**
Expand All @@ -36,6 +37,7 @@ export const PublicFormSubmitButton = ({
formLogics,
colorTheme,
onSubmit,
trigger,
}: PublicFormSubmitButtonProps): JSX.Element => {
const isMobile = useIsMobile()
const { isSubmitting } = useFormState()
Expand All @@ -53,6 +55,11 @@ export const PublicFormSubmitButton = ({
// For payments submit and pay modal
const { isOpen, onOpen, onClose } = useDisclosure({ defaultIsOpen: false })

const checkBeforeOpen = async () => {
const result = await trigger()
if (result) onOpen()
}

const isPaymentEnabled =
form?.responseMode === FormResponseMode.Encrypt &&
form?.payments_field?.enabled
Expand All @@ -74,7 +81,7 @@ export const PublicFormSubmitButton = ({
isLoading={isSubmitting}
isDisabled={!!preventSubmissionLogic || !onSubmit}
loadingText="Submitting"
onClick={isPaymentEnabled ? onOpen : onSubmit}
onClick={isPaymentEnabled ? checkBeforeOpen : onSubmit}
>
<VisuallyHidden>End of form.</VisuallyHidden>
{preventSubmissionLogic
Expand Down