Skip to content

Commit

Permalink
Add $apply() instead of $digest()
Browse files Browse the repository at this point in the history
  • Loading branch information
caleballdrin committed Nov 22, 2024
1 parent 8e101e9 commit 374cce9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 0 additions & 3 deletions src/app/branded/step-1/branded-checkout-step-1.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ class BrandedCheckoutStep1Controller {
submit () {
this.resetSubmission()
this.submitted = true
if (this.useV3 === 'true') {
this.$scope.$digest()
}
}

resetSubmission () {
Expand Down
23 changes: 19 additions & 4 deletions src/common/components/Recaptcha/RecaptchaWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface RecaptchaWrapperProps {
envService: any
$translate: any
$log: any
$rootScope: any
}

export const RecaptchaWrapper = ({
Expand All @@ -38,7 +39,8 @@ export const RecaptchaWrapper = ({
buttonLabel,
envService,
$translate,
$log
$log,
$rootScope
}: RecaptchaWrapperProps): JSX.Element => {
const recaptchaKey = envService.read('recaptchaKey')
const apiUrl = envService.read('apiUrl')
Expand All @@ -50,10 +52,23 @@ export const RecaptchaWrapper = ({
document.body.appendChild(script)
}, [])

// Because The onSuccess and onFailure callbacks are called by a React component, AngularJS doesn't know that an event happened and doesn't know it needs to rerender. We have to use $apply to ensure that AngularJS rerenders after the event handlers return.
const onSuccessWrapped = (()=>{
$rootScope.$apply(()=> {
onSuccess()
})
})

const onFailureWrapped = (()=>{
$rootScope.$apply(()=> {
onFailure()
})
})

return (
<Recaptcha action={action}
onSuccess={onSuccess}
onFailure={onFailure}
onSuccess={onSuccessWrapped}
onFailure={onFailureWrapped}
componentInstance={componentInstance}
buttonId={buttonId}
buttonType={buttonType}
Expand Down Expand Up @@ -84,4 +99,4 @@ export default angular
'buttonDisabled',
'buttonLabel'
],
['envService', '$translate', '$log']))
['envService', '$translate', '$log', '$rootScope']))

0 comments on commit 374cce9

Please sign in to comment.