aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/auth/Password.tsx
blob: 03adc38593857522b15f22144bb89d4c3c94987b (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
import { Component, FormEvent } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';

import { noop } from 'lodash';
import Form from '../../lib/Form';
import { required, email } from '../../helpers/validation-helpers';
import Input from '../ui/input/index';
import Button from '../ui/button';
import Link from '../ui/Link';
import Infobox from '../ui/Infobox';
import globalMessages from '../../i18n/globalMessages';
import { H1 } from '../ui/headline';

const messages = defineMessages({
  headline: {
    id: 'password.headline',
    defaultMessage: 'Reset password',
  },
  emailLabel: {
    id: 'password.email.label',
    defaultMessage: 'Email address',
  },
  successInfo: {
    id: 'password.successInfo',
    defaultMessage: 'Your new password was sent to your email address',
  },
  noUser: {
    id: 'password.noUser',
    defaultMessage: 'No user with that email address was found',
  },
  signupLink: {
    id: 'password.link.signup',
    defaultMessage: 'Create a free account',
  },
  loginLink: {
    id: 'password.link.login',
    defaultMessage: 'Sign in to your account',
  },
});

interface IProps extends WrappedComponentProps {
  onSubmit: (...args: any[]) => void;
  isSubmitting: boolean;
  signupRoute: string;
  loginRoute: string;
  status: string[];
}

@observer
class Password extends Component<IProps> {
  form: Form;

  constructor(props: IProps) {
    super(props);

    this.form = new Form({
      fields: {
        email: {
          label: this.props.intl.formatMessage(messages.emailLabel),
          value: '',
          validators: [required, email],
        },
      },
    });
  }

  submit(e: FormEvent<HTMLFormElement>): void {
    e.preventDefault();
    this.form.submit({
      onSuccess: form => {
        this.props.onSubmit(form.values());
      },
      onError: noop,
    });
  }

  render() {
    const { form } = this;
    const { isSubmitting, signupRoute, loginRoute, status, intl } = this.props;

    return (
      <div className="auth__container">
        <form className="franz-form auth__form" onSubmit={e => this.submit(e)}>
          <Link to="/auth/welcome">
            <img src="./assets/images/logo.svg" className="auth__logo" alt="" />
          </Link>
          <H1>{intl.formatMessage(messages.headline)}</H1>
          {status.length > 0 && status.includes('sent') && (
            <Infobox type="success" icon="checkbox-marked-circle-outline">
              {intl.formatMessage(messages.successInfo)}
            </Infobox>
          )}
          <Input {...form.$('email').bind()} focus />
          {status.length > 0 && status.includes('no-user') && (
            <p className="error-message center">
              {intl.formatMessage(messages.noUser)}
            </p>
          )}
          {isSubmitting ? (
            <Button
              className="auth__button is-loading"
              buttonType="secondary"
              label={`${intl.formatMessage(globalMessages.submit)} ...`}
              loaded={false}
              onClick={noop}
              disabled
            />
          ) : (
            <Button
              type="submit"
              className="auth__button"
              label={intl.formatMessage(globalMessages.submit)}
              onClick={noop}
            />
          )}
        </form>
        <div className="auth__links">
          <Link to={loginRoute}>{intl.formatMessage(messages.loginLink)}</Link>
          <Link to={signupRoute}>
            {intl.formatMessage(messages.signupLink)}
          </Link>
        </div>
      </div>
    );
  }
}

export default injectIntl(Password);