2018-06-28 23:17:14 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-06-29 05:05:49 +02:00
|
|
|
EventEmitter,
|
|
|
|
Output,
|
2018-06-29 04:27:32 +02:00
|
|
|
ViewChild,
|
2018-06-28 23:17:14 +02:00
|
|
|
} 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';
|
2018-06-28 23:17:14 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
|
2018-06-29 04:27:32 +02:00
|
|
|
import { PaymentComponent } from './payment.component';
|
|
|
|
|
2018-06-28 23:17:14 +02:00
|
|
|
@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
|
|
|
|
2018-06-28 23:17:14 +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) { }
|
2018-06-28 23:17:14 +02:00
|
|
|
|
|
|
|
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 { }
|
2018-06-28 23:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2018-06-28 23:17:14 +02:00
|
|
|
}
|