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

62 lines
1.9 KiB
TypeScript
Raw Normal View History

import {
Component,
2018-06-29 05:05:49 +02:00
EventEmitter,
Output,
2018-06-29 04:27:32 +02:00
ViewChild,
} from '@angular/core';
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';
2018-06-29 04:27:32 +02:00
import { PaymentComponent } from './payment.component';
@Component({
selector: 'app-premium',
templateUrl: 'premium.component.html',
})
2018-06-29 04:27:32 +02:00
export class PremiumComponent {
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
2018-06-29 05:05:49 +02:00
@Output() onPremiumPurchased = new EventEmitter();
2018-06-29 04:27:32 +02:00
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) { }
async submit() {
try {
2018-06-29 05:05:49 +02:00
this.formPromise = this.paymentComponent.createPaymentToken().then((token) => {
const fd = new FormData();
fd.append('paymentToken', token);
fd.append('additionalStorageGb', (this.additionalStorage || 0).toString());
return this.apiService.postPremium(fd);
}).then(() => {
return this.finalizePremium();
});
await this.formPromise;
} catch { }
}
get additionalStorageTotal(): number {
return this.storageGbPrice * this.additionalStorage;
}
get total(): number {
return this.additionalStorageTotal + this.premiumPrice;
}
2018-06-29 05:05:49 +02:00
private async finalizePremium() {
await this.apiService.refreshIdentityToken();
this.analytics.eventTrack.next({ action: 'Signed Up Premium' });
2018-06-29 05:11:47 +02:00
this.toasterService.popAsync('success', null, this.i18nService.t('premiumUpdated'));
2018-06-29 05:05:49 +02:00
this.onPremiumPurchased.emit();
}
}