mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 05:43:41 +00:00
migrate update license component (#8652)
This commit is contained in:
@@ -1,20 +1,39 @@
|
|||||||
<form #form (ngSubmit)="submit()" [appApiAction]="formPromise" ngNativeValidate>
|
<form [formGroup]="updateLicenseForm" [bitSubmit]="submit">
|
||||||
<div class="form-group">
|
<bit-form-field>
|
||||||
<label for="file" class="sr-only">{{ "licenseFile" | i18n }}</label>
|
<bit-label>{{ "licenseFile" | i18n }}</bit-label>
|
||||||
<input type="file" id="file" class="form-control-file" name="file" required />
|
<div>
|
||||||
<small class="form-text text-muted">{{
|
<button bitButton type="button" buttonType="secondary" (click)="fileSelector.click()">
|
||||||
|
{{ "chooseFile" | i18n }}
|
||||||
|
</button>
|
||||||
|
{{ this.licenseFile ? this.licenseFile.name : ("noFileChosen" | i18n) }}
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
bitInput
|
||||||
|
#fileSelector
|
||||||
|
type="file"
|
||||||
|
formControlName="file"
|
||||||
|
(change)="setSelectedFile($event)"
|
||||||
|
hidden
|
||||||
|
/>
|
||||||
|
<bit-hint>{{
|
||||||
"licenseFileDesc"
|
"licenseFileDesc"
|
||||||
| i18n
|
| i18n
|
||||||
: (!organizationId
|
: (!organizationId
|
||||||
? "bitwarden_premium_license.json"
|
? "bitwarden_premium_license.json"
|
||||||
: "bitwarden_organization_license.json")
|
: "bitwarden_organization_license.json")
|
||||||
}}</small>
|
}}</bit-hint>
|
||||||
</div>
|
</bit-form-field>
|
||||||
<button type="submit" class="btn btn-primary btn-submit" [disabled]="form.loading">
|
<button type="submit" buttonType="primary" bitButton bitFormButton>
|
||||||
<i class="bwi bwi-spinner bwi-spin" title="{{ 'loading' | i18n }}" aria-hidden="true"></i>
|
{{ "submit" | i18n }}
|
||||||
<span>{{ "submit" | i18n }}</span>
|
|
||||||
</button>
|
</button>
|
||||||
<button *ngIf="showCancel" type="button" class="btn btn-outline-secondary" (click)="cancel()">
|
<button
|
||||||
|
bitButton
|
||||||
|
*ngIf="showCancel"
|
||||||
|
bitFormButton
|
||||||
|
buttonType="secondary"
|
||||||
|
type="button"
|
||||||
|
[bitAction]="cancel"
|
||||||
|
>
|
||||||
{{ "cancel" | i18n }}
|
{{ "cancel" | i18n }}
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Component, EventEmitter, Input, Output } from "@angular/core";
|
import { Component, EventEmitter, Input, Output } from "@angular/core";
|
||||||
|
import { FormBuilder, Validators } from "@angular/forms";
|
||||||
|
|
||||||
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
||||||
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
|
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
|
||||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
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";
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -17,19 +17,30 @@ export class UpdateLicenseComponent {
|
|||||||
@Output() onCanceled = new EventEmitter();
|
@Output() onCanceled = new EventEmitter();
|
||||||
|
|
||||||
formPromise: Promise<void>;
|
formPromise: Promise<void>;
|
||||||
|
title: string = this.i18nService.t("updateLicense");
|
||||||
|
updateLicenseForm = this.formBuilder.group({
|
||||||
|
file: [null, Validators.required],
|
||||||
|
});
|
||||||
|
licenseFile: File = null;
|
||||||
constructor(
|
constructor(
|
||||||
private apiService: ApiService,
|
private apiService: ApiService,
|
||||||
private i18nService: I18nService,
|
private i18nService: I18nService,
|
||||||
private platformUtilsService: PlatformUtilsService,
|
private platformUtilsService: PlatformUtilsService,
|
||||||
private logService: LogService,
|
|
||||||
private organizationApiService: OrganizationApiServiceAbstraction,
|
private organizationApiService: OrganizationApiServiceAbstraction,
|
||||||
|
private formBuilder: FormBuilder,
|
||||||
) {}
|
) {}
|
||||||
|
protected setSelectedFile(event: Event) {
|
||||||
async submit() {
|
const fileInputEl = <HTMLInputElement>event.target;
|
||||||
const fileEl = document.getElementById("file") as HTMLInputElement;
|
const file: File = fileInputEl.files.length > 0 ? fileInputEl.files[0] : null;
|
||||||
const files = fileEl.files;
|
this.licenseFile = file;
|
||||||
if (files == null || files.length === 0) {
|
}
|
||||||
|
submit = async () => {
|
||||||
|
this.updateLicenseForm.markAllAsTouched();
|
||||||
|
if (this.updateLicenseForm.invalid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const files = this.licenseFile;
|
||||||
|
if (files == null) {
|
||||||
this.platformUtilsService.showToast(
|
this.platformUtilsService.showToast(
|
||||||
"error",
|
"error",
|
||||||
this.i18nService.t("errorOccurred"),
|
this.i18nService.t("errorOccurred"),
|
||||||
@@ -37,35 +48,30 @@ export class UpdateLicenseComponent {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const fd = new FormData();
|
||||||
|
fd.append("license", files);
|
||||||
|
|
||||||
try {
|
let updatePromise: Promise<void | unknown> = null;
|
||||||
const fd = new FormData();
|
if (this.organizationId == null) {
|
||||||
fd.append("license", files[0]);
|
updatePromise = this.apiService.postAccountLicense(fd);
|
||||||
|
} else {
|
||||||
let updatePromise: Promise<void | unknown> = null;
|
updatePromise = this.organizationApiService.updateLicense(this.organizationId, fd);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
this.onCanceled.emit();
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user