bitwarden-estensione-browser/src/app/settings/user-billing.component.ts

186 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-06-28 20:05:04 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { Router } from '@angular/router';
2018-06-28 20:05:04 +02:00
2018-06-29 22:55:54 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { BillingResponse } from 'jslib/models/response/billingResponse';
import { ApiService } from 'jslib/abstractions/api.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-06-28 20:05:04 +02:00
import { TokenService } from 'jslib/abstractions/token.service';
2018-06-29 22:55:54 +02:00
import { PaymentMethodType } from 'jslib/enums/paymentMethodType';
2019-02-09 06:19:54 +01:00
import { TransactionType } from 'jslib/enums/transactionType';
2018-06-29 22:55:54 +02:00
2018-06-28 20:05:04 +02:00
@Component({
selector: 'app-user-billing',
templateUrl: 'user-billing.component.html',
})
export class UserBillingComponent implements OnInit {
2018-06-29 22:55:54 +02:00
loading = false;
firstLoaded = false;
2018-06-30 05:41:35 +02:00
adjustStorageAdd = true;
showAdjustStorage = false;
2018-06-30 19:36:39 +02:00
showAdjustPayment = false;
showUpdateLicense = false;
2018-06-29 22:55:54 +02:00
billing: BillingResponse;
paymentMethodType = PaymentMethodType;
2019-02-09 06:19:54 +01:00
transactionType = TransactionType;
2018-06-30 20:14:52 +02:00
selfHosted = false;
2018-06-28 20:05:04 +02:00
2018-06-29 22:55:54 +02:00
cancelPromise: Promise<any>;
reinstatePromise: Promise<any>;
constructor(private tokenService: TokenService, private apiService: ApiService,
private platformUtilsService: PlatformUtilsService, private i18nService: I18nService,
private analytics: Angulartics2, private toasterService: ToasterService,
private router: Router) {
2018-06-30 20:14:52 +02:00
this.selfHosted = platformUtilsService.isSelfHost();
}
2018-06-28 20:05:04 +02:00
async ngOnInit() {
2018-06-29 22:55:54 +02:00
await this.load();
this.firstLoaded = true;
2018-06-29 05:05:49 +02:00
}
2018-06-29 22:55:54 +02:00
async load() {
if (this.loading) {
return;
}
2018-08-29 05:17:58 +02:00
if (this.tokenService.getPremium()) {
2018-06-29 22:55:54 +02:00
this.loading = true;
this.billing = await this.apiService.getUserBilling();
} else {
this.router.navigate(['/settings/premium']);
return;
2018-06-29 22:55:54 +02:00
}
2018-06-29 22:55:54 +02:00
this.loading = false;
}
async reinstate() {
if (this.loading) {
return;
}
const confirmed = await this.platformUtilsService.showDialog(this.i18nService.t('reinstateConfirmation'),
this.i18nService.t('reinstateSubscription'), this.i18nService.t('yes'), this.i18nService.t('cancel'));
if (!confirmed) {
return;
}
try {
this.reinstatePromise = this.apiService.postReinstatePremium();
await this.reinstatePromise;
this.analytics.eventTrack.next({ action: 'Reinstated Premium' });
this.toasterService.popAsync('success', null, this.i18nService.t('reinstated'));
this.load();
} catch { }
}
async cancel() {
if (this.loading) {
return;
}
const confirmed = await this.platformUtilsService.showDialog(this.i18nService.t('cancelConfirmation'),
this.i18nService.t('cancelSubscription'), this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
return;
}
try {
this.cancelPromise = this.apiService.postCancelPremium();
await this.cancelPromise;
this.analytics.eventTrack.next({ action: 'Canceled Premium' });
this.toasterService.popAsync('success', null, this.i18nService.t('canceledSubscription'));
this.load();
} catch { }
}
2018-06-30 20:14:52 +02:00
downloadLicense() {
2018-06-29 22:55:54 +02:00
if (this.loading) {
return;
}
const licenseString = JSON.stringify(this.billing.license, null, 2);
this.platformUtilsService.saveFile(window, licenseString, null, 'bitwarden_premium_license.json');
}
2018-06-30 20:14:52 +02:00
updateLicense() {
if (this.loading) {
return;
}
this.showUpdateLicense = true;
}
closeUpdateLicense(load: boolean) {
this.showUpdateLicense = false;
if (load) {
this.load();
}
2018-06-30 20:14:52 +02:00
}
2018-06-29 22:55:54 +02:00
adjustStorage(add: boolean) {
2018-06-30 05:41:35 +02:00
this.adjustStorageAdd = add;
this.showAdjustStorage = true;
}
2018-06-30 19:36:39 +02:00
closeStorage(load: boolean) {
2018-06-30 05:41:35 +02:00
this.showAdjustStorage = false;
2018-06-30 19:36:39 +02:00
if (load) {
this.load();
}
2018-06-29 22:55:54 +02:00
}
changePayment() {
2018-06-30 19:36:39 +02:00
this.showAdjustPayment = true;
}
2018-06-29 22:55:54 +02:00
2018-06-30 19:36:39 +02:00
closePayment(load: boolean) {
this.showAdjustPayment = false;
if (load) {
this.load();
}
2018-06-29 22:55:54 +02:00
}
get subscriptionMarkedForCancel() {
return this.subscription != null && !this.subscription.cancelled && this.subscription.cancelAtEndDate;
}
get subscription() {
return this.billing != null ? this.billing.subscription : null;
}
get nextInvoice() {
return this.billing != null ? this.billing.upcomingInvoice : null;
}
get paymentSource() {
return this.billing != null ? this.billing.paymentSource : null;
}
2019-02-09 06:19:54 +01:00
get invoices() {
return this.billing != null ? this.billing.invoices : null;
}
get transactions() {
return this.billing != null ? this.billing.transactions : null;
2018-06-29 22:55:54 +02:00
}
get storagePercentage() {
2018-07-16 23:17:07 +02:00
return this.billing != null && this.billing.maxStorageGb ?
+(100 * (this.billing.storageGb / this.billing.maxStorageGb)).toFixed(2) : 0;
}
get storageProgressWidth() {
return this.storagePercentage < 5 ? 5 : 0;
2018-06-28 20:05:04 +02:00
}
}