bitwarden-estensione-browser/src/app/settings/premium.component.ts

110 lines
4.0 KiB
TypeScript
Raw Normal View History

import {
Component,
OnInit,
2018-06-29 04:27:32 +02:00
ViewChild,
} from '@angular/core';
import { Router } from '@angular/router';
2018-06-29 05:05:49 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { ApiService } from 'jslib/abstractions/api.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-07-05 14:39:22 +02:00
import { SyncService } from 'jslib/abstractions/sync.service';
import { TokenService } from 'jslib/abstractions/token.service';
2018-08-31 23:42:19 +02:00
import { UserService } from 'jslib/abstractions/user.service';
2018-06-29 04:27:32 +02:00
import { PaymentComponent } from './payment.component';
@Component({
selector: 'app-premium',
templateUrl: 'premium.component.html',
})
export class PremiumComponent implements OnInit {
2018-06-29 04:27:32 +02:00
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
2018-08-31 23:42:19 +02:00
canAccessPremium = false;
selfHosted = false;
premiumPrice = 10;
storageGbPrice = 4;
additionalStorage = 0;
2018-06-29 05:05:49 +02:00
formPromise: Promise<any>;
constructor(private apiService: ApiService, private i18nService: I18nService,
private analytics: Angulartics2, private toasterService: ToasterService,
platformUtilsService: PlatformUtilsService, private tokenService: TokenService,
2018-07-05 14:39:22 +02:00
private router: Router, private messagingService: MessagingService,
2018-08-31 23:42:19 +02:00
private syncService: SyncService, private userService: UserService) {
this.selfHosted = platformUtilsService.isSelfHost();
}
async ngOnInit() {
2018-08-31 23:42:19 +02:00
this.canAccessPremium = await this.userService.canAccessPremium();
const premium = await this.tokenService.getPremium();
if (premium) {
this.router.navigate(['/settings/subscription']);
return;
}
}
async submit() {
let files: FileList = null;
if (this.selfHosted) {
const fileEl = document.getElementById('file') as HTMLInputElement;
files = fileEl.files;
if (files == null || files.length === 0) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('selectFile'));
return;
}
}
try {
if (this.selfHosted) {
if (!this.tokenService.getEmailVerified()) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('verifyEmailFirst'));
return;
}
2018-06-29 05:05:49 +02:00
const fd = new FormData();
fd.append('license', files[0]);
this.formPromise = this.apiService.postAccountLicense(fd).then(() => {
return this.finalizePremium();
});
} else {
2019-02-19 23:06:01 +01:00
this.formPromise = this.paymentComponent.createPaymentToken().then((result) => {
const fd = new FormData();
2019-02-19 23:06:01 +01:00
fd.append('paymentMethodType', result[1].toString());
fd.append('paymentToken', result[0]);
fd.append('additionalStorageGb', (this.additionalStorage || 0).toString());
return this.apiService.postPremium(fd);
}).then(() => {
return this.finalizePremium();
});
}
2018-06-29 05:05:49 +02:00
await this.formPromise;
} catch { }
}
async finalizePremium() {
2018-08-08 19:44:01 +02:00
await this.apiService.refreshIdentityToken();
await this.syncService.fullSync(true);
this.analytics.eventTrack.next({ action: 'Signed Up Premium' });
this.toasterService.popAsync('success', null, this.i18nService.t('premiumUpdated'));
this.messagingService.send('purchasedPremium');
this.router.navigate(['/settings/subscription']);
}
get additionalStorageTotal(): number {
return this.storageGbPrice * this.additionalStorage;
}
get total(): number {
return this.additionalStorageTotal + this.premiumPrice;
}
}