From 8af725e76f8f66cf58023f63fe0f5b3033e892c9 Mon Sep 17 00:00:00 2001 From: Davide Negretti Date: Fri, 9 Sep 2022 16:32:23 +0200 Subject: [PATCH] [UXP-10] Refactoring WIP --- .../register-email-form.component.html | 5 +-- .../register-email-form.component.ts | 32 +++++++++---------- .../google-recaptcha.component.html | 2 +- .../google-recaptcha.component.ts | 27 +++++++++------- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/app/register-email-form/register-email-form.component.html b/src/app/register-email-form/register-email-form.component.html index 8ade052708..63320db9d3 100644 --- a/src/app/register-email-form/register-email-form.component.html +++ b/src/app/register-email-form/register-email-form.component.html @@ -35,16 +35,17 @@

{{ MESSAGE_PREFIX + '.google-recaptcha.open-cookie-settings' | translate }}

-
+
- + invalid = {{form.invalid}}, disableUntilChecked = {{disableUntilChecked() | async}} diff --git a/src/app/register-email-form/register-email-form.component.ts b/src/app/register-email-form/register-email-form.component.ts index 3338200eb9..dc82cec86a 100644 --- a/src/app/register-email-form/register-email-form.component.ts +++ b/src/app/register-email-form/register-email-form.component.ts @@ -11,7 +11,7 @@ import { getFirstSucceededRemoteDataPayload } from '../core/shared/operators'; import { ConfigurationProperty } from '../core/shared/configuration-property.model'; import { isNotEmpty } from '../shared/empty.util'; import { BehaviorSubject, combineLatest, Observable, of, switchMap } from 'rxjs'; -import { map, take, tap } from 'rxjs/operators'; +import { map, startWith, take, tap } from 'rxjs/operators'; import { CAPTCHA_NAME, GoogleRecaptchaService } from '../core/google-recaptcha/google-recaptcha.service'; import { AlertType } from '../shared/alert/aletr-type'; import { KlaroService } from '../shared/cookies/klaro.service'; @@ -159,19 +159,18 @@ export class RegisterEmailFormComponent implements OnInit { } /** - * Return true if the user completed the reCaptcha verification (checkbox mode) + * Return true if the user has not completed the reCaptcha verification (checkbox mode) */ - isCheckboxChecked(): Observable { - return combineLatest([this.captchaVersion(), this.captchaMode()]).pipe( - switchMap(([captchaVersion, captchaMode]) => { - if (captchaVersion === 'v2' && captchaMode === 'checkbox') { - return this.checkboxCheckedSubject$.asObservable(); - } else { - return of(true); - } - }), - tap(console.log), - tap(() => { this.changeDetectorRef.markForCheck(); }) + disableUntilChecked(): Observable { + const checked$ = this.checkboxCheckedSubject$.asObservable(); + return combineLatest([this.captchaVersion(), this.captchaMode(), checked$]).pipe( + // disable if checkbox is not checked or if reCaptcha is not in v2 checkbox mode + switchMap(([captchaVersion, captchaMode, checked]) => captchaVersion === 'v2' && captchaMode === 'checkbox' ? of(!checked) : of(false)), + startWith(true), + tap((res) => { + console.log('DISABLED = ' + res); + }), // TODO remove + // tap(() => { this.changeDetectorRef.markForCheck(); }), ); } @@ -179,10 +178,8 @@ export class RegisterEmailFormComponent implements OnInit { return this.form.get('email'); } - onCheckboxChecked($event) { - if (isNotEmpty($event)) { - this.checkboxCheckedSubject$.next(true); - } + onCheckboxChecked(checked: boolean) { + this.checkboxCheckedSubject$.next(checked); } /** @@ -201,6 +198,7 @@ export class RegisterEmailFormComponent implements OnInit { this.notificationsService.error(notificationTitle, notificationErrorMsg); break; default: + console.warn(`Unimplemented notification '${key}' from reCaptcha service`); } } diff --git a/src/app/shared/google-recaptcha/google-recaptcha.component.html b/src/app/shared/google-recaptcha/google-recaptcha.component.html index 315514d696..64c05cb739 100644 --- a/src/app/shared/google-recaptcha/google-recaptcha.component.html +++ b/src/app/shared/google-recaptcha/google-recaptcha.component.html @@ -1,5 +1,5 @@
{ - this.executeRecaptcha.emit(event); - }; - - checkboxCheckedFcn = (event) => { - this.checkboxChecked.emit(event); // todo fix con boolean + dataCallbackFcn = ($event) => { + switch (this.captchaMode) { + case 'invisible': + this.executeRecaptcha.emit($event); + break; + case 'checkbox': + console.log('CB ' + isNotEmpty($event)); + this.checkboxChecked.emit(isNotEmpty($event)); // todo fix con boolean + break; + default: + console.error(`Invalid reCaptcha mode '${this.captchaMode}`); + this.showNotification.emit('error'); + } }; notificationFcn(key) {