From cdf93df8980da8c64fb9442270e119381c2ff88a Mon Sep 17 00:00:00 2001 From: vinith-kovan <156108204+vinith-kovan@users.noreply.github.com> Date: Wed, 22 May 2024 00:02:09 +0530 Subject: [PATCH] migrate update license component (#8652) --- .../shared/update-license.component.html | 41 +++++++--- .../shared/update-license.component.ts | 76 ++++++++++--------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/apps/web/src/app/billing/shared/update-license.component.html b/apps/web/src/app/billing/shared/update-license.component.html index 56058b158e8..37eaa30c64c 100644 --- a/apps/web/src/app/billing/shared/update-license.component.html +++ b/apps/web/src/app/billing/shared/update-license.component.html @@ -1,20 +1,39 @@ -
-
- - - {{ + + + {{ "licenseFile" | i18n }} +
+ + {{ this.licenseFile ? this.licenseFile.name : ("noFileChosen" | i18n) }} +
+ + {{ "licenseFileDesc" | i18n : (!organizationId ? "bitwarden_premium_license.json" : "bitwarden_organization_license.json") - }}
-
- -
diff --git a/apps/web/src/app/billing/shared/update-license.component.ts b/apps/web/src/app/billing/shared/update-license.component.ts index 2c24565d710..30b5983090b 100644 --- a/apps/web/src/app/billing/shared/update-license.component.ts +++ b/apps/web/src/app/billing/shared/update-license.component.ts @@ -1,9 +1,9 @@ import { Component, EventEmitter, Input, Output } from "@angular/core"; +import { FormBuilder, Validators } from "@angular/forms"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; -import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; @Component({ @@ -17,19 +17,30 @@ export class UpdateLicenseComponent { @Output() onCanceled = new EventEmitter(); formPromise: Promise; - + title: string = this.i18nService.t("updateLicense"); + updateLicenseForm = this.formBuilder.group({ + file: [null, Validators.required], + }); + licenseFile: File = null; constructor( private apiService: ApiService, private i18nService: I18nService, private platformUtilsService: PlatformUtilsService, - private logService: LogService, private organizationApiService: OrganizationApiServiceAbstraction, + private formBuilder: FormBuilder, ) {} - - async submit() { - const fileEl = document.getElementById("file") as HTMLInputElement; - const files = fileEl.files; - if (files == null || files.length === 0) { + protected setSelectedFile(event: Event) { + const fileInputEl = event.target; + const file: File = fileInputEl.files.length > 0 ? fileInputEl.files[0] : null; + this.licenseFile = file; + } + submit = async () => { + this.updateLicenseForm.markAllAsTouched(); + if (this.updateLicenseForm.invalid) { + return; + } + const files = this.licenseFile; + if (files == null) { this.platformUtilsService.showToast( "error", this.i18nService.t("errorOccurred"), @@ -37,35 +48,30 @@ export class UpdateLicenseComponent { ); return; } + const fd = new FormData(); + fd.append("license", files); - try { - const fd = new FormData(); - fd.append("license", files[0]); - - let updatePromise: Promise = null; - if (this.organizationId == null) { - updatePromise = this.apiService.postAccountLicense(fd); - } else { - updatePromise = this.organizationApiService.updateLicense(this.organizationId, fd); - } - - this.formPromise = updatePromise.then(() => { - return this.apiService.refreshIdentityToken(); - }); - - await this.formPromise; - this.platformUtilsService.showToast( - "success", - null, - this.i18nService.t("licenseUploadSuccess"), - ); - this.onUpdated.emit(); - } catch (e) { - this.logService.error(e); + let updatePromise: Promise = null; + if (this.organizationId == null) { + updatePromise = this.apiService.postAccountLicense(fd); + } else { + updatePromise = this.organizationApiService.updateLicense(this.organizationId, fd); } - } - cancel() { + this.formPromise = updatePromise.then(() => { + return this.apiService.refreshIdentityToken(); + }); + + await this.formPromise; + this.platformUtilsService.showToast( + "success", + null, + this.i18nService.t("licenseUploadSuccess"), + ); + this.onUpdated.emit(); + }; + + cancel = () => { this.onCanceled.emit(); - } + }; }