1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-22 11:45:59 +01:00

migrate update license component (#8652)

This commit is contained in:
vinith-kovan 2024-05-22 00:02:09 +05:30 committed by GitHub
parent acb153520e
commit cdf93df898
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 46 deletions

View File

@ -1,20 +1,39 @@
<form #form (ngSubmit)="submit()" [appApiAction]="formPromise" ngNativeValidate>
<div class="form-group">
<label for="file" class="sr-only">{{ "licenseFile" | i18n }}</label>
<input type="file" id="file" class="form-control-file" name="file" required />
<small class="form-text text-muted">{{
<form [formGroup]="updateLicenseForm" [bitSubmit]="submit">
<bit-form-field>
<bit-label>{{ "licenseFile" | i18n }}</bit-label>
<div>
<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"
| i18n
: (!organizationId
? "bitwarden_premium_license.json"
: "bitwarden_organization_license.json")
}}</small>
</div>
<button type="submit" class="btn btn-primary btn-submit" [disabled]="form.loading">
<i class="bwi bwi-spinner bwi-spin" title="{{ 'loading' | i18n }}" aria-hidden="true"></i>
<span>{{ "submit" | i18n }}</span>
}}</bit-hint>
</bit-form-field>
<button type="submit" buttonType="primary" bitButton bitFormButton>
{{ "submit" | i18n }}
</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 }}
</button>
</form>

View File

@ -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<void>;
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 = <HTMLInputElement>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,10 +48,8 @@ export class UpdateLicenseComponent {
);
return;
}
try {
const fd = new FormData();
fd.append("license", files[0]);
fd.append("license", files);
let updatePromise: Promise<void | unknown> = null;
if (this.organizationId == null) {
@ -60,12 +69,9 @@ export class UpdateLicenseComponent {
this.i18nService.t("licenseUploadSuccess"),
);
this.onUpdated.emit();
} catch (e) {
this.logService.error(e);
}
}
};
cancel() {
cancel = () => {
this.onCanceled.emit();
}
};
}