2019-02-18 21:28:23 +01:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
|
|
|
import { Router } from "@angular/router";
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { SubscriptionResponse } from "jslib-common/models/response/subscriptionResponse";
|
2019-02-18 21:28:23 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from "jslib-common/abstractions/log.service";
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
|
|
import { TokenService } from "jslib-common/abstractions/token.service";
|
2019-02-18 21:28:23 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-user-subscription",
|
|
|
|
templateUrl: "user-subscription.component.html",
|
|
|
|
})
|
|
|
|
export class UserSubscriptionComponent implements OnInit {
|
|
|
|
loading = false;
|
|
|
|
firstLoaded = false;
|
|
|
|
adjustStorageAdd = true;
|
|
|
|
showAdjustStorage = false;
|
|
|
|
showUpdateLicense = false;
|
|
|
|
sub: SubscriptionResponse;
|
|
|
|
selfHosted = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
cancelPromise: Promise<any>;
|
|
|
|
reinstatePromise: Promise<any>;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
constructor(
|
|
|
|
private tokenService: TokenService,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private router: Router,
|
|
|
|
private logService: LogService
|
|
|
|
) {
|
2019-02-18 21:28:23 +01:00
|
|
|
this.selfHosted = platformUtilsService.isSelfHost();
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
async ngOnInit() {
|
|
|
|
await this.load();
|
|
|
|
this.firstLoaded = true;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
async load() {
|
|
|
|
if (this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
if (this.tokenService.getPremium()) {
|
|
|
|
this.loading = true;
|
|
|
|
this.sub = await this.apiService.getUserSubscription();
|
|
|
|
} else {
|
|
|
|
this.router.navigate(["/settings/premium"]);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
this.loading = false;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
async reinstate() {
|
|
|
|
if (this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-09-19 22:34:44 +02:00
|
|
|
if (this.usingInAppPurchase) {
|
|
|
|
this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("manageSubscriptionFromStore"),
|
|
|
|
this.i18nService.t("cancelSubscription"),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
try {
|
|
|
|
this.reinstatePromise = this.apiService.postReinstatePremium();
|
|
|
|
await this.reinstatePromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast("success", null, this.i18nService.t("reinstated"));
|
2019-02-18 21:28:23 +01:00
|
|
|
this.load();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-10-20 18:30:04 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
async cancel() {
|
|
|
|
if (this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-09-19 22:34:44 +02:00
|
|
|
if (this.usingInAppPurchase) {
|
|
|
|
this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("manageSubscriptionFromStore"),
|
|
|
|
this.i18nService.t("cancelSubscription"),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
try {
|
|
|
|
this.cancelPromise = this.apiService.postCancelPremium();
|
|
|
|
await this.cancelPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("canceledSubscription")
|
|
|
|
);
|
2019-02-18 21:28:23 +01:00
|
|
|
this.load();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2019-02-18 21:28:23 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
downloadLicense() {
|
|
|
|
if (this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
const licenseString = JSON.stringify(this.sub.license, null, 2);
|
|
|
|
this.platformUtilsService.saveFile(
|
|
|
|
window,
|
|
|
|
licenseString,
|
|
|
|
null,
|
|
|
|
"bitwarden_premium_license.json"
|
|
|
|
);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
updateLicense() {
|
|
|
|
if (this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.showUpdateLicense = true;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
closeUpdateLicense(load: boolean) {
|
|
|
|
this.showUpdateLicense = false;
|
|
|
|
if (load) {
|
|
|
|
this.load();
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
adjustStorage(add: boolean) {
|
2019-09-19 22:34:44 +02:00
|
|
|
if (this.usingInAppPurchase) {
|
|
|
|
this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("cannotPerformInAppPurchase"),
|
|
|
|
this.i18nService.t(add ? "addStorage" : "removeStorage"),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-18 21:28:23 +01:00
|
|
|
this.adjustStorageAdd = add;
|
|
|
|
this.showAdjustStorage = true;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
closeStorage(load: boolean) {
|
|
|
|
this.showAdjustStorage = false;
|
|
|
|
if (load) {
|
|
|
|
this.load();
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
get subscriptionMarkedForCancel() {
|
|
|
|
return (
|
|
|
|
this.subscription != null && !this.subscription.cancelled && this.subscription.cancelAtEndDate
|
|
|
|
);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
get subscription() {
|
|
|
|
return this.sub != null ? this.sub.subscription : null;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
get nextInvoice() {
|
|
|
|
return this.sub != null ? this.sub.upcomingInvoice : null;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
get storagePercentage() {
|
|
|
|
return this.sub != null && this.sub.maxStorageGb
|
|
|
|
? +(100 * (this.sub.storageGb / this.sub.maxStorageGb)).toFixed(2)
|
|
|
|
: 0;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-02-18 21:28:23 +01:00
|
|
|
get storageProgressWidth() {
|
|
|
|
return this.storagePercentage < 5 ? 5 : 0;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2019-09-19 22:34:44 +02:00
|
|
|
get usingInAppPurchase() {
|
|
|
|
return this.sub != null ? this.sub.usingInAppPurchase : false;
|
|
|
|
}
|
2019-02-18 21:28:23 +01:00
|
|
|
}
|