aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/PaymentStore.js
blob: 9e348d14e0287702ea72b9451c4222c8a0908c42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { action, observable, computed } from 'mobx';

import Store from './lib/Store';
import CachedRequest from './lib/CachedRequest';
import Request from './lib/Request';
import { gaEvent } from '../lib/analytics';

export default class PaymentStore extends Store {
  @observable plansRequest = new CachedRequest(this.api.payment, 'plans');
  @observable createHostedPageRequest = new Request(this.api.payment, 'getHostedPage');
  @observable createDashboardUrlRequest = new Request(this.api.payment, 'getDashboardUrl');
  @observable ordersDataRequest = new CachedRequest(this.api.payment, 'getOrders');

  constructor(...args) {
    super(...args);

    this.actions.payment.createHostedPage.listen(this._createHostedPage.bind(this));
    this.actions.payment.createDashboardUrl.listen(this._createDashboardUrl.bind(this));
  }

  @computed get plan() {
    if (this.plansRequest.isError) {
      return {};
    }
    return this.plansRequest.execute().result || {};
  }

  @computed get orders() {
    return this.ordersDataRequest.execute().result || [];
  }

  @action _createHostedPage({ planId }) {
    const request = this.createHostedPageRequest.execute(planId);

    gaEvent('Payment', 'createHostedPage', planId);

    return request;
  }

  @action _createDashboardUrl() {
    const request = this.createDashboardUrlRequest.execute();

    gaEvent('Payment', 'createDashboardUrl');

    return request;
  }
}