aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/subscription/SubscriptionPopup.js
blob: 0f6f0260ff25ffae82792119a9bd2cd48dd89065 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import Webview from 'react-electron-web-view';
import ms from 'ms';

import Button from '../ui/Button';

const messages = defineMessages({
  buttonCancel: {
    id: 'subscriptionPopup.buttonCancel',
    defaultMessage: '!!!Cancel',
  },
  buttonDone: {
    id: 'subscriptionPopup.buttonDone',
    defaultMessage: '!!!Done',
  },
});

export default @observer class SubscriptionPopup extends Component {
  static propTypes = {
    url: PropTypes.string.isRequired,
    closeWindow: PropTypes.func.isRequired,
    completeCheck: PropTypes.func.isRequired,
    isCompleted: PropTypes.bool.isRequired,
  };

  static contextTypes = {
    intl: intlShape,
  };

  state = {
    isFakeLoading: false,
  };

  // We delay the window closing a bit in order to give
  // the Recurly webhook a few seconds to do it's magic
  delayedCloseWindow() {
    this.setState({
      isFakeLoading: true,
    });

    setTimeout(() => {
      this.props.closeWindow();
    }, ms('4s'));
  }

  render() {
    const {
      url, closeWindow, completeCheck, isCompleted,
    } = this.props;
    const { intl } = this.context;

    return (
      <div className="subscription-popup">
        <div className="subscription-popup__content">
          <Webview
            className="subscription-popup__webview"

            autosize
            src={encodeURI(url)}
            onDidNavigate={completeCheck}
            // onNewWindow={(event, url, frameName, options) =>
            //   openWindow({ event, url, frameName, options })}
          />
        </div>
        <div className="subscription-popup__toolbar franz-form">
          <Button
            label={intl.formatMessage(messages.buttonCancel)}
            buttonType="secondary"
            onClick={closeWindow}
            disabled={isCompleted}
          />
          <Button
            label={intl.formatMessage(messages.buttonDone)}
            onClick={() => this.delayedCloseWindow()}
            disabled={!isCompleted}
            loaded={!this.state.isFakeLoading}
          />
        </div>
      </div>
    );
  }
}