Skip to content

Commit

Permalink
ON-43678 # change to single ref of captchas
Browse files Browse the repository at this point in the history
  • Loading branch information
divporter committed Sep 25, 2024
1 parent 2a568d6 commit 05ef160
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
48 changes: 26 additions & 22 deletions src/OneBlinkFormBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export type OneBlinkFormBaseProps = OneBlinkReadOnlyFormProps & {
onUploadAttachment?: typeof attachmentsService.uploadAttachment
/**
* Determines whether to use checkboxes or invisible recaptcha v2 for captcha
* elements
* elements. Defaults to "CHECKBOX"
*/
captchaType?: CaptchaType
}
Expand Down Expand Up @@ -197,9 +197,7 @@ function OneBlinkFormBase({
}: Props) {
const isOffline = useIsOffline()
const { isUsingFormsKey, userProfile } = useAuth()
const [captchaRefs, setCaptchaRefs] = React.useState<
Array<React.RefObject<ReCAPTCHA>>
>([])
const captchasRef = React.useRef<Array<ReCAPTCHA>>([])

const theme = React.useMemo(
() =>
Expand Down Expand Up @@ -383,7 +381,7 @@ function OneBlinkFormBase({
const { validate } = useFormValidation(pages)

const recaptchaType = React.useMemo(
() => captchaType ?? 'CHECKBOXES',
() => captchaType ?? 'CHECKBOX',
[captchaType],
)

Expand Down Expand Up @@ -550,11 +548,23 @@ function OneBlinkFormBase({
[definition],
)

const addCaptchaRef = React.useCallback(
(recaptchaRef: React.RefObject<ReCAPTCHA>) => {
setCaptchaRefs((current) => [...current, recaptchaRef])
},
[],
const addCaptchaRef = React.useCallback((recaptcha: ReCAPTCHA) => {
captchasRef.current.push(recaptcha)
// this allows the FormElementCaptcha element to unregister any captchas
return () => {
captchasRef.current = captchasRef.current.filter(
(recaptchaInstance) => recaptchaInstance === recaptcha,
)
}
}, [])

const captchaContextValue = React.useMemo(
() => ({
captchaSiteKey,
captchaType: recaptchaType,
addCaptchaRef,
}),
[addCaptchaRef, captchaSiteKey, recaptchaType],
)

const resetRecaptchas = React.useCallback(() => {
Expand All @@ -566,14 +576,14 @@ function OneBlinkFormBase({
}
})
// reset each captcha
captchaRefs.forEach((ref) => {
ref.current?.reset()
captchasRef.current.forEach((captcha) => {
captcha.reset()
})
setHasAttemptedSubmit(false)
setFormSubmission((current) => {
return { ...current, submission: updatedModel }
})
}, [definition.elements, setFormSubmission, submission, captchaRefs])
}, [definition.elements, setFormSubmission, submission])

const handleSubmit = React.useCallback(
async (
Expand Down Expand Up @@ -613,9 +623,9 @@ function OneBlinkFormBase({
}

if (captchaType === 'INVISIBLE') {
if (captchaRefs.length) {
if (captchasRef.current.length) {
const tokenResults = await Promise.allSettled(
captchaRefs.map((ref) => ref.current?.executeAsync()),
captchasRef.current.map((captcha) => captcha.executeAsync()),
)

const captchaTokens: string[] = []
Expand Down Expand Up @@ -707,7 +717,6 @@ function OneBlinkFormBase({
userProfile,
onSubmit,
resetRecaptchas,
captchaRefs,
setFormSubmission,
],
)
Expand Down Expand Up @@ -1056,12 +1065,7 @@ function OneBlinkFormBase({
value={abnLookupAuthenticationGuid}
>
<CaptchaContext.Provider
value={{
captchaSiteKey,
captchaType: recaptchaType,
captchaRefs,
addCaptchaRef,
}}
value={captchaContextValue}
>
<AttachmentBlobsProvider>
<FormIsReadOnlyContext.Provider
Expand Down
4 changes: 2 additions & 2 deletions src/components/payments/WestpacQuickStreamPaymentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function WestpacQuickStreamPaymentForm({
}, [isTestMode, publishableApiKey, supplierBusinessCode])

const recaptchaType = React.useMemo(
() => captchaType ?? 'CHECKBOXES',
() => captchaType ?? 'CHECKBOX',
[captchaType],
)

Expand Down Expand Up @@ -264,7 +264,7 @@ function WestpacQuickStreamPaymentForm({
displayCaptchaRequired: false,
}
}
case 'CHECKBOXES':
case 'CHECKBOX':
default:
return { recaptchaToken: captchaToken, displayCaptchaRequired: true }
}
Expand Down
5 changes: 3 additions & 2 deletions src/form-elements/FormElementCaptcha.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ function FormElementCaptcha({
const captchaRef = React.useRef<ReCAPTCHA>(null)

React.useEffect(() => {
if (captchaRef) {
addCaptchaRef(captchaRef)
if (captchaRef.current) {
// addCaptchaRef returns a function to remove the captcha which will fire when the component unmounts
return addCaptchaRef(captchaRef.current)
}
}, [captchaRef, addCaptchaRef])

Expand Down
10 changes: 4 additions & 6 deletions src/hooks/useCaptcha.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { useContext, createContext, RefObject } from 'react'
import { useContext, createContext } from 'react'
import { ReCAPTCHA } from 'react-google-recaptcha'
import { CaptchaType } from '../types/form'

export const CaptchaContext = createContext<{
captchaSiteKey: string | undefined
captchaType: CaptchaType
captchaRefs: Array<RefObject<ReCAPTCHA>>
addCaptchaRef: (ref: RefObject<ReCAPTCHA>) => void
addCaptchaRef: (captcha: ReCAPTCHA) => () => void
}>({
captchaSiteKey: undefined,
captchaType: 'CHECKBOXES',
captchaRefs: [],
addCaptchaRef: () => {},
captchaType: 'CHECKBOX',
addCaptchaRef: () => () => {},
})

export default function useCaptcha() {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useReCAPTCHAProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function useReCAPTCHAProps({
size: 'invisible',
badge: 'inline',
}
case 'CHECKBOXES':
case 'CHECKBOX':
default: {
return {
...baseProps,
Expand Down
2 changes: 1 addition & 1 deletion src/services/form-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export function generateValidationSchema(
switch (captchaType) {
case 'INVISIBLE':
return
case 'CHECKBOXES':
case 'CHECKBOX':
default:
return {
presence: presence(
Expand Down
2 changes: 1 addition & 1 deletion src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ export type IsDirtyProps = {

//TODO get from types
// export type captchaType = IntegrationTypes.IntegrationRecaptcha['configuration']['domains'][number]['type']
export type CaptchaType = 'INVISIBLE' | 'CHECKBOXES'
export type CaptchaType = 'INVISIBLE' | 'CHECKBOX'

0 comments on commit 05ef160

Please sign in to comment.