From 4e9db9057b7eecb15f5ffdb8b18fee796881d74a Mon Sep 17 00:00:00 2001 From: KiruthigaManivannan <162679756+KiruthigaManivannan@users.noreply.github.com> Date: Fri, 24 May 2024 19:00:06 +0530 Subject: [PATCH] AC-2401 Migrate sponsored families component (#8874) * AC-2401 Migrate sponsored families component * AC-2401 Removed unused method --- .../sponsored-families.component.html | 151 ++++++++---------- .../settings/sponsored-families.component.ts | 50 +++++- 2 files changed, 111 insertions(+), 90 deletions(-) diff --git a/apps/web/src/app/admin-console/settings/sponsored-families.component.html b/apps/web/src/app/admin-console/settings/sponsored-families.component.html index 63fa370797..27f2fdf65a 100644 --- a/apps/web/src/app/admin-console/settings/sponsored-families.component.html +++ b/apps/web/src/app/admin-console/settings/sponsored-families.component.html @@ -3,103 +3,86 @@ - {{ "loading" | i18n }} + {{ "loading" | i18n }} -

+

{{ "sponsoredFamiliesEligible" | i18n }}

-
+
{{ "sponsoredFamiliesInclude" | i18n }}: -
    +
    • {{ "sponsoredFamiliesPremiumAccess" | i18n }}
    • {{ "sponsoredFamiliesSharedCollections" | i18n }}
-
-
- - -
-
- - - - - {{ "cannotSponsorSelf" | i18n }} - - - - {{ "invalidEmail" | i18n }} - -
-
- + +
+
+ + {{ "familiesSponsoringOrgSelect" | i18n }} + + + + + +
+
+ + {{ "sponsoredFamiliesEmail" | i18n }}: + + +
+
+ +
-
- - - - - - - + + + + + + + + + + + + +
- - - - - - -
{{ "recipient" | i18n }}{{ "sponsoringOrg" | i18n }}{{ "status" | i18n }}
{{ "recipient" | i18n }}{{ "sponsoringOrg" | i18n }}{{ "status" | i18n }}
-
- {{ "sponsoredFamiliesLeaveCopy" | i18n }} +
+ + +
+

{{ "sponsoredFamiliesLeaveCopy" | i18n }}

diff --git a/apps/web/src/app/admin-console/settings/sponsored-families.component.ts b/apps/web/src/app/admin-console/settings/sponsored-families.component.ts index 3477b9a425..e82849a905 100644 --- a/apps/web/src/app/admin-console/settings/sponsored-families.component.ts +++ b/apps/web/src/app/admin-console/settings/sponsored-families.component.ts @@ -1,8 +1,15 @@ import { Component, OnDestroy, OnInit } from "@angular/core"; -import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms"; +import { + FormBuilder, + FormControl, + FormGroup, + Validators, + AbstractControl, + AsyncValidatorFn, + ValidationErrors, +} from "@angular/forms"; import { firstValueFrom, map, Observable, Subject, takeUntil } from "rxjs"; -import { notAllowedValueAsync } from "@bitwarden/angular/admin-console/validators/not-allowed-value-async.validator"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; @@ -50,9 +57,9 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy { validators: [Validators.required], }), sponsorshipEmail: new FormControl("", { - validators: [Validators.email], + validators: [Validators.email, Validators.required], asyncValidators: [ - notAllowedValueAsync( + this.notAllowedValueAsync( () => firstValueFrom(this.accountService.activeAccount$.pipe(map((a) => a?.email))), true, ), @@ -84,6 +91,15 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy { this.anyActiveSponsorships$ = this.activeSponsorshipOrgs$.pipe(map((orgs) => orgs.length > 0)); this.loading = false; + + this.sponsorshipForm + .get("sponsorshipEmail") + .valueChanges.pipe(takeUntil(this._destroy)) + .subscribe((val) => { + if (this.sponsorshipEmailControl.hasError("email")) { + this.sponsorshipEmailControl.setErrors([{ message: this.i18nService.t("invalidEmail") }]); + } + }); } ngOnDestroy(): void { @@ -91,7 +107,7 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy { this._destroy.complete(); } - async submit() { + submit = async () => { this.formPromise = this.apiService.postCreateSponsorship( this.sponsorshipForm.value.selectedSponsorshipOrgId, { @@ -108,7 +124,7 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy { // eslint-disable-next-line @typescript-eslint/no-floating-promises this.resetForm(); await this.forceReload(); - } + }; async forceReload() { this.loading = true; @@ -127,4 +143,26 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy { get isSelfHosted(): boolean { return this.platformUtilsService.isSelfHost(); } + + notAllowedValueAsync( + valueGetter: () => Promise, + caseInsensitive = false, + ): AsyncValidatorFn { + return async (control: AbstractControl): Promise => { + let notAllowedValue = await valueGetter(); + let controlValue = control.value; + if (caseInsensitive) { + notAllowedValue = notAllowedValue.toLowerCase(); + controlValue = controlValue.toLowerCase(); + } + + if (controlValue === notAllowedValue) { + return { + errors: { + message: this.i18nService.t("cannotSponsorSelf"), + }, + }; + } + }; + } }