2019-01-22 15:18:14 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-08-06 12:43:30 +01:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-01-22 15:18:14 -07:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { _t } from "../../../../languageHandler";
|
|
|
|
|
import { MatrixClientPeg } from "../../../../MatrixClientPeg";
|
2019-08-06 13:03:15 +01:00
|
|
|
import Field from "../../elements/Field";
|
|
|
|
|
import AccessibleButton from "../../elements/AccessibleButton";
|
|
|
|
|
import * as Email from "../../../../email";
|
|
|
|
|
import AddThreepid from "../../../../AddThreepid";
|
2019-12-19 18:19:56 -07:00
|
|
|
import * as sdk from '../../../../index';
|
2019-09-11 11:33:58 +01:00
|
|
|
import Modal from '../../../../Modal';
|
2021-06-29 13:11:58 +01:00
|
|
|
import { replaceableComponent } from "../../../../utils/replaceableComponent";
|
2019-01-22 15:18:14 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
TODO: Improve the UX for everything in here.
|
|
|
|
|
It's very much placeholder, but it gets the job done. The old way of handling
|
|
|
|
|
email addresses in user settings was to use dialogs to communicate state, however
|
|
|
|
|
due to our dialog system overriding dialogs (causing unmounts) this creates problems
|
|
|
|
|
for a sane UX. For instance, the user could easily end up entering an email address
|
|
|
|
|
and receive a dialog to verify the address, which then causes the component here
|
|
|
|
|
to forget what it was doing and ultimately fail. Dialogs are still used in some
|
|
|
|
|
places to communicate errors - these should be replaced with inline validation when
|
|
|
|
|
that is available.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export class ExistingEmailAddress extends React.Component {
|
|
|
|
|
static propTypes = {
|
|
|
|
|
email: PropTypes.object.isRequired,
|
|
|
|
|
onRemoved: PropTypes.func.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
verifyRemove: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onRemove = (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ verifyRemove: true });
|
2019-01-22 15:18:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_onDontRemove = (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ verifyRemove: false });
|
2019-01-22 15:18:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_onActuallyRemove = (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
MatrixClientPeg.get().deleteThreePid(this.props.email.medium, this.props.email.address).then(() => {
|
|
|
|
|
return this.props.onRemoved(this.props.email);
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
|
console.error("Unable to remove contact information: " + err);
|
|
|
|
|
Modal.createTrackedDialog('Remove 3pid failed', '', ErrorDialog, {
|
|
|
|
|
title: _t("Unable to remove contact information"),
|
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (this.state.verifyRemove) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx_ExistingEmailAddress">
|
|
|
|
|
<span className="mx_ExistingEmailAddress_promptText">
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Remove %(email)s?", { email: this.props.email.address } ) }
|
2019-01-22 15:18:14 -07:00
|
|
|
</span>
|
2021-04-27 17:23:27 +02:00
|
|
|
<AccessibleButton
|
|
|
|
|
onClick={this._onActuallyRemove}
|
|
|
|
|
kind="danger_sm"
|
|
|
|
|
className="mx_ExistingEmailAddress_confirmBtn"
|
|
|
|
|
>
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Remove") }
|
2019-01-22 15:18:14 -07:00
|
|
|
</AccessibleButton>
|
2021-04-27 17:23:27 +02:00
|
|
|
<AccessibleButton
|
|
|
|
|
onClick={this._onDontRemove}
|
|
|
|
|
kind="link_sm"
|
|
|
|
|
className="mx_ExistingEmailAddress_confirmBtn"
|
|
|
|
|
>
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Cancel") }
|
2019-01-22 15:18:14 -07:00
|
|
|
</AccessibleButton>
|
|
|
|
|
</div>
|
2019-01-23 09:41:07 -07:00
|
|
|
);
|
2019-01-22 15:18:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx_ExistingEmailAddress">
|
2021-07-19 22:43:11 +01:00
|
|
|
<span className="mx_ExistingEmailAddress_email">{ this.props.email.address }</span>
|
2019-08-05 16:34:04 +01:00
|
|
|
<AccessibleButton onClick={this._onRemove} kind="danger_sm">
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Remove") }
|
2019-08-05 16:34:04 +01:00
|
|
|
</AccessibleButton>
|
2019-01-22 15:18:14 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 20:20:07 -07:00
|
|
|
@replaceableComponent("views.settings.account.EmailAddresses")
|
2019-01-22 15:18:14 -07:00
|
|
|
export default class EmailAddresses extends React.Component {
|
2019-09-11 11:33:58 +01:00
|
|
|
static propTypes = {
|
|
|
|
|
emails: PropTypes.array.isRequired,
|
|
|
|
|
onEmailsChange: PropTypes.func.isRequired,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2019-01-22 15:18:14 -07:00
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
verifying: false,
|
|
|
|
|
addTask: null,
|
|
|
|
|
continueDisabled: false,
|
2019-03-12 16:17:21 +00:00
|
|
|
newEmailAddress: "",
|
2019-01-22 15:18:14 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onRemoved = (address) => {
|
2019-09-11 11:33:58 +01:00
|
|
|
const emails = this.props.emails.filter((e) => e !== address);
|
|
|
|
|
this.props.onEmailsChange(emails);
|
2019-01-22 15:18:14 -07:00
|
|
|
};
|
|
|
|
|
|
2019-03-12 16:17:21 +00:00
|
|
|
_onChangeNewEmailAddress = (e) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
newEmailAddress: e.target.value,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-22 15:18:14 -07:00
|
|
|
_onAddClick = (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2019-03-12 16:17:21 +00:00
|
|
|
if (!this.state.newEmailAddress) return;
|
2019-01-22 15:18:14 -07:00
|
|
|
|
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2019-03-12 16:17:21 +00:00
|
|
|
const email = this.state.newEmailAddress;
|
2019-01-22 15:18:14 -07:00
|
|
|
|
|
|
|
|
// TODO: Inline field validation
|
|
|
|
|
if (!Email.looksValid(email)) {
|
|
|
|
|
Modal.createTrackedDialog('Invalid email address', '', ErrorDialog, {
|
|
|
|
|
title: _t("Invalid Email Address"),
|
|
|
|
|
description: _t("This doesn't appear to be a valid email address"),
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const task = new AddThreepid();
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ verifying: true, continueDisabled: true, addTask: task });
|
2019-01-22 15:18:14 -07:00
|
|
|
|
2019-09-19 15:50:18 +01:00
|
|
|
task.addEmailAddress(email).then(() => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ continueDisabled: false });
|
2019-01-22 15:18:14 -07:00
|
|
|
}).catch((err) => {
|
|
|
|
|
console.error("Unable to add email address " + email + " " + err);
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ verifying: false, continueDisabled: false, addTask: null });
|
2019-01-22 15:18:14 -07:00
|
|
|
Modal.createTrackedDialog('Unable to add email address', '', ErrorDialog, {
|
|
|
|
|
title: _t("Unable to add email address"),
|
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_onContinueClick = (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ continueDisabled: true });
|
2021-02-09 11:39:36 +05:30
|
|
|
this.state.addTask.checkEmailLinkClicked().then(([finished]) => {
|
2021-02-14 18:21:12 +05:30
|
|
|
let newEmailAddress = this.state.newEmailAddress;
|
2021-02-09 11:39:36 +05:30
|
|
|
if (finished) {
|
|
|
|
|
const email = this.state.newEmailAddress;
|
|
|
|
|
const emails = [
|
|
|
|
|
...this.props.emails,
|
|
|
|
|
{ address: email, medium: "email" },
|
|
|
|
|
];
|
|
|
|
|
this.props.onEmailsChange(emails);
|
2021-02-14 18:21:12 +05:30
|
|
|
newEmailAddress = "";
|
2021-02-09 11:39:36 +05:30
|
|
|
}
|
2019-01-22 15:18:14 -07:00
|
|
|
this.setState({
|
|
|
|
|
addTask: null,
|
|
|
|
|
continueDisabled: false,
|
|
|
|
|
verifying: false,
|
2021-02-14 18:21:12 +05:30
|
|
|
newEmailAddress,
|
2019-01-22 15:18:14 -07:00
|
|
|
});
|
|
|
|
|
}).catch((err) => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ continueDisabled: false });
|
2019-10-07 17:43:51 +02:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
|
if (err.errcode === 'M_THREEPID_AUTH_FAILED') {
|
|
|
|
|
Modal.createTrackedDialog("Email hasn't been verified yet", "", ErrorDialog, {
|
|
|
|
|
title: _t("Your email address hasn't been verified yet"),
|
2019-10-08 18:39:35 +02:00
|
|
|
description: _t("Click the link in the email you received to verify " +
|
2019-10-07 17:43:51 +02:00
|
|
|
"and then click continue again."),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2019-10-08 18:56:13 +01:00
|
|
|
console.error("Unable to verify email address: ", err);
|
2019-01-22 15:18:14 -07:00
|
|
|
Modal.createTrackedDialog('Unable to verify email address', '', ErrorDialog, {
|
|
|
|
|
title: _t("Unable to verify email address."),
|
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-09-11 11:33:58 +01:00
|
|
|
const existingEmailElements = this.props.emails.map((e) => {
|
2019-01-23 09:41:07 -07:00
|
|
|
return <ExistingEmailAddress email={e} onRemoved={this._onRemoved} key={e.address} />;
|
2019-01-22 15:18:14 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let addButton = (
|
|
|
|
|
<AccessibleButton onClick={this._onAddClick} kind="primary">
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Add") }
|
2019-01-22 15:18:14 -07:00
|
|
|
</AccessibleButton>
|
|
|
|
|
);
|
|
|
|
|
if (this.state.verifying) {
|
|
|
|
|
addButton = (
|
2021-04-27 17:23:27 +02:00
|
|
|
<div>
|
2021-07-19 22:43:11 +01:00
|
|
|
<div>{ _t("We've sent you an email to verify your address. Please follow the instructions there and then click the button below.") }</div>
|
2021-04-27 17:23:27 +02:00
|
|
|
<AccessibleButton
|
|
|
|
|
onClick={this._onContinueClick}
|
|
|
|
|
kind="primary"
|
|
|
|
|
disabled={this.state.continueDisabled}
|
|
|
|
|
>
|
2021-07-19 22:43:11 +01:00
|
|
|
{ _t("Continue") }
|
2021-04-27 17:23:27 +02:00
|
|
|
</AccessibleButton>
|
|
|
|
|
</div>
|
2019-01-22 15:18:14 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx_EmailAddresses">
|
2021-07-19 22:43:11 +01:00
|
|
|
{ existingEmailElements }
|
2021-04-27 17:23:27 +02:00
|
|
|
<form
|
|
|
|
|
onSubmit={this._onAddClick}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
noValidate={true}
|
|
|
|
|
className="mx_EmailAddresses_new"
|
|
|
|
|
>
|
2020-03-29 22:59:15 +01:00
|
|
|
<Field
|
2019-03-12 16:17:21 +00:00
|
|
|
type="text"
|
|
|
|
|
label={_t("Email Address")}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
disabled={this.state.verifying}
|
|
|
|
|
value={this.state.newEmailAddress}
|
|
|
|
|
onChange={this._onChangeNewEmailAddress}
|
|
|
|
|
/>
|
2021-07-19 22:43:11 +01:00
|
|
|
{ addButton }
|
2019-01-22 15:18:14 -07:00
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|