aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/auth/Import.js
blob: 281ceda97af3dc20f6f46dea2217dca5997b177b (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
import { Component } from 'react';
import PropTypes from 'prop-types';
import { observer, PropTypes as MobxPropTypes } from 'mobx-react';
import { defineMessages, injectIntl } from 'react-intl';
import { Link } from 'react-router';
import classnames from 'classnames';

import Form from '../../lib/Form';
import Toggle from '../ui/Toggle';
import Button from '../ui/Button';

const messages = defineMessages({
  headline: {
    id: 'import.headline',
    defaultMessage: 'Import your Ferdium 4 services',
  },
  notSupportedHeadline: {
    id: 'import.notSupportedHeadline',
    defaultMessage: 'Services not yet supported in Ferdium 5',
  },
  submitButtonLabel: {
    id: 'import.submit.label',
    defaultMessage: 'Import {count} services',
  },
  skipButtonLabel: {
    id: 'import.skip.label',
    defaultMessage: 'I want to add services manually',
  },
});

class Import extends Component {
  static propTypes = {
    services: MobxPropTypes.arrayOrObservableArray.isRequired,
    onSubmit: PropTypes.func.isRequired,
    isSubmitting: PropTypes.bool.isRequired,
    inviteRoute: PropTypes.string.isRequired,
  };

  componentDidMount() {
    const config = {
      fields: {
        import: [
          ...this.props.services
            .filter(s => s.recipe)
            .map(s => ({
              fields: {
                add: {
                  default: true,
                  options: s,
                },
              },
            })),
        ],
      },
    };

    this.form = new Form(config, this.props.intl);
  }

  submit(e) {
    const { services } = this.props;
    e.preventDefault();
    this.form.submit({
      onSuccess: form => {
        const servicesImport = form
          .values()
          .import.map(
            (value, i) => !value.add || services.filter(s => s.recipe)[i],
          )
          .filter(s => typeof s !== 'boolean');

        this.props.onSubmit({ services: servicesImport });
      },
      onError: () => {},
    });
  }

  render() {
    const { intl } = this.props;
    const { services, isSubmitting, inviteRoute } = this.props;

    const availableServices = services.filter(s => s.recipe);
    const unavailableServices = services.filter(s => !s.recipe);

    return (
      <div className="auth__scroll-container">
        <div className="auth__container auth__container--signup">
          <form
            className="franz-form auth__form"
            onSubmit={e => this.submit(e)}
          >
            <img src="./assets/images/logo.svg" className="auth__logo" alt="" />
            <h1>{intl.formatMessage(messages.headline)}</h1>
            <table className="service-table available-services">
              <tbody>
                {this.form.$('import').map((service, i) => (
                  <tr key={service.id} className="service-table__row">
                    <td className="service-table__toggle">
                      <Toggle field={service.$('add')} showLabel={false} />
                    </td>
                    <td className="service-table__column-icon">
                      <img
                        src={
                          availableServices[i].custom_icon ||
                          availableServices[i].recipe.icons.svg
                        }
                        className={classnames({
                          'service-table__icon': true,
                          'has-custom-icon': availableServices[i].custom_icon,
                        })}
                        alt=""
                      />
                    </td>
                    <td className="service-table__column-name">
                      {availableServices[i].name !== ''
                        ? availableServices[i].name
                        : availableServices[i].recipe.name}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
            {unavailableServices.length > 0 && (
              <div className="unavailable-services">
                <strong>
                  {intl.formatMessage(messages.notSupportedHeadline)}
                </strong>
                <p>
                  {services
                    .filter(s => !s.recipe)
                    .map((service, i) => (
                      <span key={service.id}>
                        {service.name !== '' ? service.name : service.service}
                        {unavailableServices.length > i + 1 ? ', ' : ''}
                      </span>
                    ))}
                </p>
              </div>
            )}

            {isSubmitting ? (
              <Button
                className="auth__button is-loading"
                label={`${intl.formatMessage(messages.submitButtonLabel)} ...`}
                loaded={false}
                disabled
              />
            ) : (
              <Button
                type="submit"
                className="auth__button"
                label={intl.formatMessage(messages.submitButtonLabel)}
              />
            )}
            <Link
              to={inviteRoute}
              className="franz-form__button franz-form__button--secondary auth__button auth__button--skip"
            >
              {intl.formatMessage(messages.skipButtonLabel)}
            </Link>
          </form>
        </div>
      </div>
    );
  }
}

export default injectIntl(observer(Import));