2018-06-30 05:41:35 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
Output,
|
2019-08-10 19:00:07 +02:00
|
|
|
ViewChild,
|
2018-06-30 05:41:35 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
|
2019-08-11 01:51:49 +02:00
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
|
|
|
|
2018-06-30 05:41:35 +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 { StorageRequest } from 'jslib/models/request/storageRequest';
|
|
|
|
|
2019-08-10 19:00:07 +02:00
|
|
|
import { PaymentResponse } from 'jslib/models/response/paymentResponse';
|
|
|
|
|
|
|
|
import { PaymentComponent } from './payment.component';
|
|
|
|
|
2018-06-30 05:41:35 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-adjust-storage',
|
|
|
|
templateUrl: 'adjust-storage.component.html',
|
|
|
|
})
|
|
|
|
export class AdjustStorageComponent {
|
|
|
|
@Input() storageGbPrice = 0;
|
|
|
|
@Input() add = true;
|
2018-07-16 23:17:07 +02:00
|
|
|
@Input() organizationId: string;
|
2018-06-30 05:41:35 +02:00
|
|
|
@Input() interval = 'year';
|
|
|
|
@Output() onAdjusted = new EventEmitter<number>();
|
|
|
|
@Output() onCanceled = new EventEmitter();
|
|
|
|
|
2019-08-10 19:00:07 +02:00
|
|
|
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
|
|
|
|
|
2018-06-30 05:41:35 +02:00
|
|
|
storageAdjustment = 0;
|
2019-08-10 19:43:47 +02:00
|
|
|
formPromise: Promise<any>;
|
2018-06-30 05:41:35 +02:00
|
|
|
|
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2019-08-11 01:51:49 +02:00
|
|
|
private analytics: Angulartics2, private toasterService: ToasterService,
|
|
|
|
private router: Router, private activatedRoute: ActivatedRoute) { }
|
2018-06-30 05:41:35 +02:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
try {
|
|
|
|
const request = new StorageRequest();
|
|
|
|
request.storageGbAdjustment = this.storageAdjustment;
|
|
|
|
if (!this.add) {
|
|
|
|
request.storageGbAdjustment *= -1;
|
|
|
|
}
|
|
|
|
|
2019-08-10 19:43:47 +02:00
|
|
|
let paymentFailed = false;
|
|
|
|
const action = async () => {
|
|
|
|
let response: Promise<PaymentResponse>;
|
|
|
|
if (this.organizationId == null) {
|
|
|
|
response = this.formPromise = this.apiService.postAccountStorage(request);
|
|
|
|
} else {
|
|
|
|
response = this.formPromise = this.apiService.postOrganizationStorage(this.organizationId, request);
|
|
|
|
}
|
|
|
|
const result = await response;
|
|
|
|
if (result != null && result.paymentIntentClientSecret != null) {
|
|
|
|
try {
|
|
|
|
await this.paymentComponent.handleStripeCardPayment(result.paymentIntentClientSecret, null);
|
|
|
|
} catch {
|
|
|
|
paymentFailed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.formPromise = action();
|
|
|
|
await this.formPromise;
|
2018-06-30 05:41:35 +02:00
|
|
|
this.analytics.eventTrack.next({ action: this.add ? 'Added Storage' : 'Removed Storage' });
|
|
|
|
this.onAdjusted.emit(this.storageAdjustment);
|
2019-08-10 19:43:47 +02:00
|
|
|
if (paymentFailed) {
|
2019-08-11 01:51:49 +02:00
|
|
|
this.toasterService.popAsync({
|
|
|
|
body: this.i18nService.t('couldNotChargeCardPayInvoice'),
|
|
|
|
type: 'warning',
|
|
|
|
timeout: 10000,
|
|
|
|
});
|
|
|
|
this.router.navigate(['../billing'], { relativeTo: this.activatedRoute });
|
2019-08-10 19:43:47 +02:00
|
|
|
} else {
|
|
|
|
this.toasterService.popAsync('success', null,
|
|
|
|
this.i18nService.t('adjustedStorage', request.storageGbAdjustment.toString()));
|
|
|
|
}
|
2018-06-30 05:41:35 +02:00
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.onCanceled.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
get adjustedStorageTotal(): number {
|
|
|
|
return this.storageGbPrice * this.storageAdjustment;
|
|
|
|
}
|
|
|
|
}
|