|
| 1 | +/* |
| 2 | +Copyright 2017 Vector Creations Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import q from 'q'; |
| 18 | +import React from 'react'; |
| 19 | +import sdk from '../../../index'; |
| 20 | +import MatrixClientPeg from '../../../MatrixClientPeg'; |
| 21 | +import classnames from 'classnames'; |
| 22 | +import KeyCode from '../../../KeyCode'; |
| 23 | +import Email from '../../../email'; |
| 24 | +import AddThreepid from '../../../AddThreepid'; |
| 25 | +import { _t, _tJsx } from '../../../languageHandler'; |
| 26 | +import Modal from '../../../Modal'; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Prompt the user to set an email address. |
| 31 | + * |
| 32 | + * On success, `onFinished(true)` is called. |
| 33 | + */ |
| 34 | +export default React.createClass({ |
| 35 | + displayName: 'SetEmailDialog', |
| 36 | + propTypes: { |
| 37 | + onFinished: React.PropTypes.func.isRequired, |
| 38 | + }, |
| 39 | + |
| 40 | + getInitialState: function() { |
| 41 | + return { |
| 42 | + emailAddress: null, |
| 43 | + emailBusy: false, |
| 44 | + }; |
| 45 | + }, |
| 46 | + |
| 47 | + componentDidMount: function() { |
| 48 | + }, |
| 49 | + |
| 50 | + onEmailAddressChanged: function(value) { |
| 51 | + this.setState({ |
| 52 | + emailAddress: value, |
| 53 | + }); |
| 54 | + }, |
| 55 | + |
| 56 | + onSubmit: function() { |
| 57 | + const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); |
| 58 | + const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog"); |
| 59 | + |
| 60 | + const emailAddress = this.state.emailAddress; |
| 61 | + if (!Email.looksValid(emailAddress)) { |
| 62 | + Modal.createDialog(ErrorDialog, { |
| 63 | + title: _t("Invalid Email Address"), |
| 64 | + description: _t("This doesn't appear to be a valid email address"), |
| 65 | + }); |
| 66 | + return; |
| 67 | + } |
| 68 | + this._addThreepid = new AddThreepid(); |
| 69 | + // we always bind emails when registering, so let's do the |
| 70 | + // same here. |
| 71 | + this._addThreepid.addEmailAddress(emailAddress, true).done(() => { |
| 72 | + Modal.createDialog(QuestionDialog, { |
| 73 | + title: _t("Verification Pending"), |
| 74 | + description: _t( |
| 75 | + "Please check your email and click on the link it contains. Once this " + |
| 76 | + "is done, click continue.", |
| 77 | + ), |
| 78 | + button: _t('Continue'), |
| 79 | + onFinished: this.onEmailDialogFinished, |
| 80 | + }); |
| 81 | + }, (err) => { |
| 82 | + this.setState({emailBusy: false}); |
| 83 | + console.error("Unable to add email address " + emailAddress + " " + err); |
| 84 | + Modal.createDialog(ErrorDialog, { |
| 85 | + title: _t("Unable to add email address"), |
| 86 | + description: ((err && err.message) ? err.message : _t("Operation failed")), |
| 87 | + }); |
| 88 | + }); |
| 89 | + this.setState({emailBusy: true}); |
| 90 | + }, |
| 91 | + |
| 92 | + onEmailDialogFinished: function(ok) { |
| 93 | + if (ok) { |
| 94 | + this.verifyEmailAddress(); |
| 95 | + } else { |
| 96 | + this.setState({emailBusy: false}); |
| 97 | + } |
| 98 | + }, |
| 99 | + |
| 100 | + verifyEmailAddress: function() { |
| 101 | + this._addThreepid.checkEmailLinkClicked().done(() => { |
| 102 | + this.props.onFinished(true); |
| 103 | + }, (err) => { |
| 104 | + this.setState({emailBusy: false}); |
| 105 | + if (err.errcode == 'M_THREEPID_AUTH_FAILED') { |
| 106 | + const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog"); |
| 107 | + const message = _t("Unable to verify email address.") + " " + |
| 108 | + _t("Please check your email and click on the link it contains. Once this is done, click continue."); |
| 109 | + Modal.createDialog(QuestionDialog, { |
| 110 | + title: _t("Verification Pending"), |
| 111 | + description: message, |
| 112 | + button: _t('Continue'), |
| 113 | + onFinished: this.onEmailDialogFinished, |
| 114 | + }); |
| 115 | + } else { |
| 116 | + const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); |
| 117 | + console.error("Unable to verify email address: " + err); |
| 118 | + Modal.createDialog(ErrorDialog, { |
| 119 | + title: _t("Unable to verify email address."), |
| 120 | + description: ((err && err.message) ? err.message : _t("Operation failed")), |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | + }, |
| 125 | + |
| 126 | + render: function() { |
| 127 | + const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog'); |
| 128 | + const Spinner = sdk.getComponent('elements.Spinner'); |
| 129 | + const EditableText = sdk.getComponent('elements.EditableText'); |
| 130 | + |
| 131 | + const emailInput = this.state.emailBusy ? <Spinner /> : <EditableText |
| 132 | + className="mx_SetEmailDialog_email_input" |
| 133 | + placeholder={ _t("Email address") } |
| 134 | + placeholderClassName="mx_SetEmailDialog_email_input_placeholder" |
| 135 | + blurToCancel={ false } |
| 136 | + onValueChanged={ this.onEmailAddressChanged } />; |
| 137 | + |
| 138 | + return ( |
| 139 | + <BaseDialog className="mx_SetEmailDialog" |
| 140 | + onFinished={this.props.onFinished} |
| 141 | + title={this.props.title} |
| 142 | + > |
| 143 | + <div className="mx_Dialog_content"> |
| 144 | + <p> |
| 145 | + { _t('This will allow you to reset your password and receive notifications.') } |
| 146 | + </p> |
| 147 | + { emailInput } |
| 148 | + </div> |
| 149 | + <div className="mx_Dialog_buttons"> |
| 150 | + <input className="mx_Dialog_primary" |
| 151 | + type="submit" |
| 152 | + value={_t("Continue")} |
| 153 | + onClick={this.onSubmit} |
| 154 | + /> |
| 155 | + <input |
| 156 | + type="submit" |
| 157 | + value={_t("Cancel")} |
| 158 | + onClick={this.props.onFinished} |
| 159 | + /> |
| 160 | + </div> |
| 161 | + </BaseDialog> |
| 162 | + ); |
| 163 | + }, |
| 164 | +}); |
0 commit comments