bitwarden-estensione-browser/apps/web/src/app/settings/adjust-storage.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-06-30 05:41:35 +02:00
import { Component, EventEmitter, Input, Output, ViewChild } from "@angular/core";
2019-08-11 01:51:49 +02:00
import { ActivatedRoute, Router } from "@angular/router";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { StorageRequest } from "@bitwarden/common/models/request/storageRequest";
import { PaymentResponse } from "@bitwarden/common/models/response/paymentResponse";
import { PaymentComponent } from "./payment.component";
2018-06-30 05:41:35 +02:00
@Component({
selector: "app-adjust-storage",
templateUrl: "adjust-storage.component.html",
})
export class AdjustStorageComponent {
@Input() storageGbPrice = 0;
@Input() add = true;
2018-07-16 23:17:07 +02:00
@Input() organizationId: string;
2018-06-30 05:41:35 +02:00
@Input() interval = "year";
@Output() onAdjusted = new EventEmitter<number>();
@Output() onCanceled = new EventEmitter();
@ViewChild(PaymentComponent, { static: true }) paymentComponent: PaymentComponent;
2018-06-30 05:41:35 +02:00
storageAdjustment = 0;
formPromise: Promise<any>;
2018-06-30 05:41:35 +02:00
constructor(
private apiService: ApiService,
private i18nService: I18nService,
2021-12-07 20:41:45 +01:00
private platformUtilsService: PlatformUtilsService,
private router: Router,
private activatedRoute: ActivatedRoute,
private logService: LogService
) {}
2018-06-30 05:41:35 +02:00
async submit() {
try {
const request = new StorageRequest();
request.storageGbAdjustment = this.storageAdjustment;
if (!this.add) {
request.storageGbAdjustment *= -1;
}
let paymentFailed = false;
const action = async () => {
let response: Promise<PaymentResponse>;
if (this.organizationId == null) {
response = this.formPromise = this.apiService.postAccountStorage(request);
} else {
response = this.formPromise = this.apiService.postOrganizationStorage(
this.organizationId,
2021-12-17 15:57:11 +01:00
request
);
}
const result = await response;
if (result != null && result.paymentIntentClientSecret != null) {
2021-12-17 15:57:11 +01:00
try {
await this.paymentComponent.handleStripeCardPayment(
result.paymentIntentClientSecret,
2021-12-17 15:57:11 +01:00
null
);
} catch {
paymentFailed = true;
2021-12-17 15:57:11 +01:00
}
}
};
this.formPromise = action();
await this.formPromise;
2018-06-30 05:41:35 +02:00
this.onAdjusted.emit(this.storageAdjustment);
if (paymentFailed) {
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
2021-12-17 15:57:11 +01:00
"warning",
null,
2021-12-07 20:41:45 +01:00
this.i18nService.t("couldNotChargeCardPayInvoice"),
{ timeout: 10000 }
2021-12-17 15:57:11 +01:00
);
2019-08-11 01:51:49 +02:00
this.router.navigate(["../billing"], { relativeTo: this.activatedRoute });
2021-12-17 15:57:11 +01:00
} else {
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
2021-12-17 15:57:11 +01:00
"success",
null,
this.i18nService.t("adjustedStorage", request.storageGbAdjustment.toString())
2021-12-17 15:57:11 +01:00
);
}
} catch (e) {
this.logService.error(e);
2018-06-30 05:41:35 +02:00
}
2021-12-17 15:57:11 +01:00
}
2018-06-30 05:41:35 +02:00
cancel() {
this.onCanceled.emit();
}
get adjustedStorageTotal(): number {
return this.storageGbPrice * this.storageAdjustment;
}
}