aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/PaymentStore.js
blob: 69e6eb9c3602d7949c337705ba9016adcc1e4e81 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { action, observable, computed } from 'mobx';
import { remote } from 'electron';

import Store from './lib/Store';
import CachedRequest from './lib/CachedRequest';
import Request from './lib/Request';

const { BrowserWindow } = remote;

export default class PaymentStore extends Store {
  @observable plansRequest = new CachedRequest(this.api.payment, 'plans');

  @observable createHostedPageRequest = new Request(this.api.payment, 'getHostedPage');

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

    this.actions.payment.createHostedPage.listen(this._createHostedPage.bind(this));
    this.actions.payment.upgradeAccount.listen(this._upgradeAccount.bind(this));
  }

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

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

    return request;
  }

  @action _upgradeAccount({ planId, onCloseWindow = () => null }) {
    let hostedPageURL = this.stores.features.features.subscribeURL;

    const parsedUrl = new URL(hostedPageURL);
    const params = new URLSearchParams(parsedUrl.search.slice(1));

    params.set('plan', planId);

    hostedPageURL = this.stores.user.getAuthURL(`${parsedUrl.origin}${parsedUrl.pathname}?${params.toString()}`);

    const win = new BrowserWindow({
      parent: remote.getCurrentWindow(),
      modal: true,
      title: '🔒 Upgrade Your Franz Account',
      width: 800,
      height: window.innerHeight - 100,
      maxWidth: 800,
      minWidth: 600,
      autoHideMenuBar: true,
      webPreferences: {
        nodeIntegration: true,
        webviewTag: true,
      },
    });
    win.loadURL(`file://${__dirname}/../index.html#/payment/${encodeURIComponent(hostedPageURL)}`);

    win.on('closed', () => {
      this.stores.user.getUserInfoRequest.invalidate({ immediately: true });
      this.stores.features.featuresRequest.invalidate({ immediately: true });

      onCloseWindow();
    });
  }
}