aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/subscription/SubscriptionForm.js
blob: 6b60c2af05034a8fc81275238565e01a1b451299 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { observer, PropTypes as MobxPropTypes } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';

import Form from '../../lib/Form';
import Radio from '../ui/Radio';
import Button from '../ui/Button';
import Loader from '../ui/Loader';

import { required } from '../../helpers/validation-helpers';

const messages = defineMessages({
  submitButtonLabel: {
    id: 'subscription.submit.label',
    defaultMessage: '!!!Support the development of Franz',
  },
  paymentSessionError: {
    id: 'subscription.paymentSessionError',
    defaultMessage: '!!!Could not initialize payment form',
  },
  typeFree: {
    id: 'subscription.type.free',
    defaultMessage: '!!!free',
  },
  typeMonthly: {
    id: 'subscription.type.month',
    defaultMessage: '!!!month',
  },
  typeYearly: {
    id: 'subscription.type.year',
    defaultMessage: '!!!year',
  },
  includedFeatures: {
    id: 'subscription.includedFeatures',
    defaultMessage: '!!!The Franz Premium Supporter Account includes',
  },
  features: {
    onpremise: {
      id: 'subscription.features.onpremise.mattermost',
      defaultMessage: '!!!Add on-premise/hosted services like Mattermost',
    },
    noInterruptions: {
      id: 'subscription.features.noInterruptions',
      defaultMessage: '!!!No app delays & nagging to upgrade license',
    },
    proxy: {
      id: 'subscription.features.proxy',
      defaultMessage: '!!!Proxy support for services',
    },
    spellchecker: {
      id: 'subscription.features.spellchecker',
      defaultMessage: '!!!Support for Spellchecker',
    },
    ads: {
      id: 'subscription.features.ads',
      defaultMessage: '!!!No ads, ever!',
    },
    comingSoon: {
      id: 'subscription.features.comingSoon',
      defaultMessage: '!!!coming soon',
    },
  },
  euTaxInfo: {
    id: 'subscription.euTaxInfo',
    defaultMessage: '!!!EU residents: local sales tax may apply',
  },
});

export default @observer class SubscriptionForm extends Component {
  static propTypes = {
    plan: MobxPropTypes.objectOrObservableObject.isRequired,
    isLoading: PropTypes.bool.isRequired,
    handlePayment: PropTypes.func.isRequired,
    retryPlanRequest: PropTypes.func.isRequired,
    isCreatingHostedPage: PropTypes.bool.isRequired,
    error: PropTypes.bool.isRequired,
    showSkipOption: PropTypes.bool,
    skipAction: PropTypes.func,
    skipButtonLabel: PropTypes.string,
    hideInfo: PropTypes.bool.isRequired,
  };

  static defaultProps ={
    content: '',
    showSkipOption: false,
    skipAction: () => null,
    skipButtonLabel: '',
  }

  static contextTypes = {
    intl: intlShape,
  };

  componentWillMount() {
    this.form = this.prepareForm();
  }

  prepareForm() {
    const { intl } = this.context;

    const form = {
      fields: {
        paymentTier: {
          value: 'year',
          validators: [required],
          options: [{
            value: 'month',
            label: `€ ${Object.hasOwnProperty.call(this.props.plan, 'month')
              ? `${this.props.plan.month.price} / ${intl.formatMessage(messages.typeMonthly)}`
              : 'monthly'}`,
          }, {
            value: 'year',
            label: `€ ${Object.hasOwnProperty.call(this.props.plan, 'year')
              ? `${this.props.plan.year.price} / ${intl.formatMessage(messages.typeYearly)}`
              : 'yearly'}`,
          }],
        },
      },
    };

    if (this.props.showSkipOption) {
      form.fields.paymentTier.options.unshift({
        value: 'skip',
        label: `€ 0 / ${intl.formatMessage(messages.typeFree)}`,
      });
    }

    return new Form(form, this.context.intl);
  }

  render() {
    const {
      isLoading,
      isCreatingHostedPage,
      handlePayment,
      retryPlanRequest,
      error,
      showSkipOption,
      skipAction,
      skipButtonLabel,
      hideInfo,
    } = this.props;
    const { intl } = this.context;

    if (error) {
      return (
        <Button
          label="Reload"
          onClick={retryPlanRequest}
          isLoaded={!isLoading}
        />
      );
    }

    return (
      <Loader loaded={!isLoading}>
        <Radio field={this.form.$('paymentTier')} showLabel={false} className="paymentTiers" />
        {!hideInfo && (
          <div className="subscription__premium-info">
            <p>
              <strong>{intl.formatMessage(messages.includedFeatures)}</strong>
            </p>
            <div className="subscription">
              <ul className="subscription__premium-features">
                <li>{intl.formatMessage(messages.features.onpremise)}</li>
                <li>
                  {intl.formatMessage(messages.features.noInterruptions)}
                </li>
                <li>
                  {intl.formatMessage(messages.features.spellchecker)}
                </li>
                <li>
                  {intl.formatMessage(messages.features.proxy)}
                </li>
                <li>
                  {intl.formatMessage(messages.features.ads)}
                </li>
              </ul>
            </div>
          </div>
        )}
        <Fragment>
          {error.code === 'no-payment-session' && (
            <p className="error-message center">{intl.formatMessage(messages.paymentSessionError)}</p>
          )}
        </Fragment>
        {showSkipOption && this.form.$('paymentTier').value === 'skip' ? (
          <Button
            label={skipButtonLabel}
            className="auth__button"
            onClick={skipAction}
          />
        ) : (
          <Button
            label={intl.formatMessage(messages.submitButtonLabel)}
            className="auth__button"
            loaded={!isCreatingHostedPage}
            onClick={() => handlePayment(this.form.$('paymentTier').value)}
          />
        )}
        {this.form.$('paymentTier').value !== 'skip' && (
          <p className="legal">
            {intl.formatMessage(messages.euTaxInfo)}
          </p>
        )}
      </Loader>
    );
  }
}