aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/auth/Invite.js
blob: fa83837acf922c6da1724dc66ed3d528ce162218 (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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import { Link } from 'react-router';

import Form from '../../lib/Form';
import { email } from '../../helpers/validation-helpers';
import Input from '../ui/Input';
import Button from '../ui/Button';

const messages = defineMessages({
  headline: {
    id: 'invite.headline.friends',
    defaultMessage: '!!!Invite 3 of your friends or colleagues',
  },
  nameLabel: {
    id: 'invite.name.label',
    defaultMessage: '!!!Name',
  },
  emailLabel: {
    id: 'invite.email.label',
    defaultMessage: '!!!Email address',
  },
  submitButtonLabel: {
    id: 'invite.submit.label',
    defaultMessage: '!!!Send invites',
  },
  skipButtonLabel: {
    id: 'invite.skip.label',
    defaultMessage: '!!!I want to do this later',
  },
});

@observer
export default class Invite extends Component {
  static propTypes = {
    onSubmit: PropTypes.func.isRequired,
    from: PropTypes.string,
  };

  static defaultProps = {
    from: '/',
  };

  static contextTypes = {
    intl: intlShape,
  };

  form = new Form({
    fields: {
      invite: [...Array(3).fill({
        fields: {
          name: {
            label: this.context.intl.formatMessage(messages.nameLabel),
            placeholder: this.context.intl.formatMessage(messages.nameLabel),
          },
          email: {
            label: this.context.intl.formatMessage(messages.emailLabel),
            placeholder: this.context.intl.formatMessage(messages.emailLabel),
            validators: [email],
          },
        },
      })],
    },
  }, this.context.intl);

  submit(e) {
    e.preventDefault();
    this.form.submit({
      onSuccess: (form) => {
        this.props.onSubmit({ invites: form.values().invite });
      },
      onError: () => {},
    });
  }

  render() {
    const { form } = this;
    const { intl } = this.context;
    const { from } = this.props;

    const atLeastOneEmailAddress = form.$('invite')
      .map(invite => invite.$('email').value)
      .some(emailValue => emailValue.trim() !== '');

    return (
      <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>
          {form.$('invite').map(invite => (
            <div className="grid" key={invite.key}>
              <div className="grid__row">
                <Input field={invite.$('name')} showLabel={false} />
                <Input field={invite.$('email')} showLabel={false} />
              </div>
            </div>
          ))}
          <Button
            type="submit"
            className="auth__button"
            disabled={!atLeastOneEmailAddress}
            label={intl.formatMessage(messages.submitButtonLabel)}
          />
          <Link
            to={from || '/'}
            className="franz-form__button franz-form__button--secondary auth__button auth__button--skip"
          >
            {intl.formatMessage(messages.skipButtonLabel)}
          </Link>
        </form>
      </div>
    );
  }
}