1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/app/settings/user-billing.component.ts

162 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-06-28 20:05:04 +02:00
import {
Component,
OnInit,
} from '@angular/core';
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';
2018-06-28 20:05:04 +02:00
@Component({
selector: 'app-user-billing',
templateUrl: 'user-billing.component.html',
})
export class UserBillingComponent implements OnInit {
premium = false;
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;
2018-06-29 22:55:54 +02:00
billing: BillingResponse;
paymentMethodType = PaymentMethodType;
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,
2018-06-30 20:14:52 +02:00
private analytics: Angulartics2, private toasterService: ToasterService) {
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-06-28 20:05:04 +02:00
this.premium = this.tokenService.getPremium();
2018-06-29 22:55:54 +02:00
if (this.premium) {
this.loading = true;
this.billing = await this.apiService.getUserBilling();
}
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;
}
}
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;
}
get charges() {
return this.billing != null ? this.billing.charges : null;
}
get storagePercentage() {
return this.billing != null ? +(100 * (this.billing.storageGb / this.billing.maxStorageGb)).toFixed(2) : 0;
2018-06-28 20:05:04 +02:00
}
}