summaryrefslogtreecommitdiffstats
path: root/webapp/components/password_reset_send_link.jsx
blob: 65d9439bd0eab7507091db08de072f575416ad64 (plain)
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
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import $ from 'jquery';
import ReactDOM from 'react-dom';
import * as Utils from 'utils/utils.jsx';
import client from 'utils/web_client.jsx';

import {FormattedMessage, FormattedHTMLMessage} from 'react-intl';

import React from 'react';
import {Link} from 'react-router';

class PasswordResetSendLink extends React.Component {
    constructor(props) {
        super(props);

        this.handleSendLink = this.handleSendLink.bind(this);

        this.state = {
            error: '',
            updateText: ''
        };
    }
    handleSendLink(e) {
        e.preventDefault();

        var email = ReactDOM.findDOMNode(this.refs.email).value.trim().toLowerCase();
        if (!email || !Utils.isEmail(email)) {
            this.setState({
                error: (
                    <FormattedMessage
                        id={'password_send.error'}
                        defaultMessage={'Please enter a valid email address.'}
                    />
                )
            });
            return;
        }

        // End of error checking clear error
        this.setState({
            error: ''
        });

        client.sendPasswordReset(
            email,
            () => {
                this.setState({
                    error: null,
                    updateText: (
                        <div className='reset-form alert alert-success'>
                            <FormattedHTMLMessage
                                id='password_send.link'
                                defaultMessage='<p>A password reset link has been sent to <b>{email}</b></p>'
                                values={{
                                    email
                                }}
                            />
                            <FormattedMessage
                                id={'password_send.checkInbox'}
                                defaultMessage={'Please check your inbox.'}
                            />
                        </div>
                    )
                });
                $(ReactDOM.findDOMNode(this.refs.reset_form)).hide();
            },
            (err) => {
                this.setState({
                    error: err.message,
                    update_text: null
                });
            }
        );
    }
    render() {
        var error = null;
        if (this.state.error) {
            error = <div className='form-group has-error'><label className='control-label'>{this.state.error}</label></div>;
        }

        var formClass = 'form-group';
        if (error) {
            formClass += ' has-error';
        }

        return (
            <div>
                <div className='signup-header'>
                    <Link to='/'>
                        <span className='fa fa-chevron-left'/>
                        <FormattedMessage
                            id='web.header.back'
                        />
                    </Link>
                </div>
                <div className='col-sm-12'>
                    <div className='signup-team__container'>
                        <h3>
                            <FormattedMessage
                                id='password_send.title'
                                defaultMessage='Password Reset'
                            />
                        </h3>
                        {this.state.updateText}
                        <form
                            onSubmit={this.handleSendLink}
                            ref='reset_form'
                        >
                            <p>
                                <FormattedMessage
                                    id='password_send.description'
                                    defaultMessage='To reset your password, enter the email address you used to sign up'
                                />
                            </p>
                            <div className={formClass}>
                                <input
                                    type='email'
                                    className='form-control'
                                    name='email'
                                    ref='email'
                                    placeholder={Utils.localizeMessage(
                                        'password_send.email',
                                        'Email'
                                    )}
                                    spellCheck='false'
                                />
                            </div>
                            {error}
                            <button
                                type='submit'
                                className='btn btn-primary'
                            >
                                <FormattedMessage
                                    id='password_send.reset'
                                    defaultMessage='Reset my password'
                                />
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        );
    }
}

PasswordResetSendLink.defaultProps = {
};
PasswordResetSendLink.propTypes = {
    params: React.PropTypes.object.isRequired
};

export default PasswordResetSendLink;