Files
ThreadNet-Web/src/components/views/settings/account/PhoneNumbers.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

320 lines
11 KiB
TypeScript
Raw Normal View History

2019-01-23 14:16:18 -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-23 14:16:18 -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";
2021-10-22 17:23:32 -05:00
import { IThreepid, ThreepidMedium } from "matrix-js-sdk/src/@types/threepids";
import { logger } from "matrix-js-sdk/src/logger";
2021-06-29 13:11:58 +01:00
import { _t } from "../../../../languageHandler";
import { MatrixClientPeg } from "../../../../MatrixClientPeg";
import Field from "../../elements/Field";
import AccessibleButton from "../../elements/AccessibleButton";
import AddThreepid from "../../../../AddThreepid";
import CountryDropdown from "../../auth/CountryDropdown";
import Modal from "../../../../Modal";
2021-09-21 12:21:51 +02:00
import ErrorDialog from "../../dialogs/ErrorDialog";
import { PhoneNumberCountryDefinition } from "../../../../phonenumber";
2019-01-23 14:16:18 -07:00
/*
TODO: Improve the UX for everything in here.
This is a copy/paste of EmailAddresses, mostly.
*/
// TODO: Combine EmailAddresses and PhoneNumbers to be 3pid agnostic
2021-09-21 12:21:51 +02:00
interface IExistingPhoneNumberProps {
msisdn: IThreepid;
onRemoved: (phoneNumber: IThreepid) => void;
}
2019-01-23 14:16:18 -07:00
2021-09-21 12:21:51 +02:00
interface IExistingPhoneNumberState {
verifyRemove: boolean;
}
export class ExistingPhoneNumber extends React.Component<IExistingPhoneNumberProps, IExistingPhoneNumberState> {
public constructor(props: IExistingPhoneNumberProps) {
2021-09-21 12:21:51 +02:00
super(props);
2019-01-23 14:16:18 -07:00
this.state = {
verifyRemove: false,
};
}
2021-09-21 12:21:51 +02:00
private onRemove = (e: React.MouseEvent): void => {
2019-01-23 14:16:18 -07:00
e.stopPropagation();
e.preventDefault();
2021-06-29 13:11:58 +01:00
this.setState({ verifyRemove: true });
2019-01-23 14:16:18 -07:00
};
2021-09-21 12:21:51 +02:00
private onDontRemove = (e: React.MouseEvent): void => {
2019-01-23 14:16:18 -07:00
e.stopPropagation();
e.preventDefault();
2021-06-29 13:11:58 +01:00
this.setState({ verifyRemove: false });
2019-01-23 14:16:18 -07:00
};
2021-09-21 12:21:51 +02:00
private onActuallyRemove = (e: React.MouseEvent): void => {
2019-01-23 14:16:18 -07:00
e.stopPropagation();
e.preventDefault();
MatrixClientPeg.get()
.deleteThreePid(this.props.msisdn.medium, this.props.msisdn.address)
.then(() => {
return this.props.onRemoved(this.props.msisdn);
})
.catch((err) => {
2021-10-15 16:30:53 +02:00
logger.error("Unable to remove contact information: " + err);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
2019-01-23 14:16:18 -07:00
title: _t("Unable to remove contact information"),
description: err && err.message ? err.message : _t("Operation failed"),
2022-12-12 12:24:14 +01:00
});
2019-01-23 14:16:18 -07:00
});
};
2021-09-21 12:21:51 +02:00
public render(): JSX.Element {
2019-01-23 14:16:18 -07:00
if (this.state.verifyRemove) {
return (
<div className="mx_ExistingPhoneNumber">
<span className="mx_ExistingPhoneNumber_promptText">
{_t("Remove %(phone)s?", { phone: this.props.msisdn.address })}
2019-01-23 14:16:18 -07:00
</span>
2021-04-27 17:23:27 +02:00
<AccessibleButton
2021-09-21 12:21:51 +02:00
onClick={this.onActuallyRemove}
2021-04-27 17:23:27 +02:00
kind="danger_sm"
className="mx_ExistingPhoneNumber_confirmBtn"
>
{_t("Remove")}
2019-01-23 14:16:18 -07:00
</AccessibleButton>
2021-04-27 17:23:27 +02:00
<AccessibleButton
2021-09-21 12:21:51 +02:00
onClick={this.onDontRemove}
2021-04-27 17:23:27 +02:00
kind="link_sm"
className="mx_ExistingPhoneNumber_confirmBtn"
>
{_t("Cancel")}
2019-01-23 14:16:18 -07:00
</AccessibleButton>
</div>
);
}
return (
<div className="mx_ExistingPhoneNumber">
<span className="mx_ExistingPhoneNumber_address">+{this.props.msisdn.address}</span>
2021-09-21 12:21:51 +02:00
<AccessibleButton onClick={this.onRemove} kind="danger_sm">
{_t("Remove")}
2019-08-05 16:34:04 +01:00
</AccessibleButton>
2019-01-23 14:16:18 -07:00
</div>
);
}
}
2021-09-21 12:21:51 +02:00
interface IProps {
msisdns: IThreepid[];
onMsisdnsChange: (phoneNumbers: Partial<IThreepid>[]) => void;
}
2021-09-21 12:21:51 +02:00
interface IState {
verifying: boolean;
verifyError: string;
verifyMsisdn: string;
addTask: any; // FIXME: When AddThreepid is TSfied
continueDisabled: boolean;
phoneCountry: string;
newPhoneNumber: string;
newPhoneNumberCode: string;
}
export default class PhoneNumbers extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);
2019-01-23 14:16:18 -07:00
this.state = {
verifying: false,
2021-09-21 12:21:51 +02:00
verifyError: null,
2019-01-23 14:16:18 -07:00
verifyMsisdn: "",
addTask: null,
continueDisabled: false,
phoneCountry: "",
newPhoneNumber: "",
newPhoneNumberCode: "",
2019-01-23 14:16:18 -07:00
};
}
2021-09-21 12:21:51 +02:00
private onRemoved = (address: IThreepid): void => {
const msisdns = this.props.msisdns.filter((e) => e !== address);
this.props.onMsisdnsChange(msisdns);
2019-01-23 14:16:18 -07:00
};
2021-09-21 12:21:51 +02:00
private onChangeNewPhoneNumber = (e: React.ChangeEvent<HTMLInputElement>): void => {
this.setState({
newPhoneNumber: e.target.value,
});
};
2021-09-21 12:21:51 +02:00
private onChangeNewPhoneNumberCode = (e: React.ChangeEvent<HTMLInputElement>): void => {
this.setState({
newPhoneNumberCode: e.target.value,
});
};
2021-09-21 12:21:51 +02:00
private onAddClick = (e: React.MouseEvent | React.FormEvent): void => {
2019-01-23 14:16:18 -07:00
e.stopPropagation();
e.preventDefault();
if (!this.state.newPhoneNumber) return;
2019-01-23 14:16:18 -07:00
const phoneNumber = this.state.newPhoneNumber;
2019-01-23 14:16:18 -07:00
const phoneCountry = this.state.phoneCountry;
const task = new AddThreepid();
2021-06-29 13:11:58 +01:00
this.setState({ verifying: true, continueDisabled: true, addTask: task });
2019-01-23 14:16:18 -07:00
task.addMsisdn(phoneCountry, phoneNumber)
.then((response) => {
2021-06-29 13:11:58 +01:00
this.setState({ continueDisabled: false, verifyMsisdn: response.msisdn });
2019-01-23 14:16:18 -07:00
})
.catch((err) => {
2021-10-15 16:30:53 +02:00
logger.error("Unable to add phone number " + phoneNumber + " " + err);
2021-06-29 13:11:58 +01:00
this.setState({ verifying: false, continueDisabled: false, addTask: null });
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
2019-01-23 14:16:18 -07:00
title: _t("Error"),
description: err && err.message ? err.message : _t("Operation failed"),
2022-12-12 12:24:14 +01:00
});
2019-01-23 14:16:18 -07:00
});
};
2021-09-21 12:21:51 +02:00
private onContinueClick = (e: React.MouseEvent | React.FormEvent): void => {
2019-01-23 14:16:18 -07:00
e.stopPropagation();
e.preventDefault();
2021-06-29 13:11:58 +01:00
this.setState({ continueDisabled: true });
const token = this.state.newPhoneNumberCode;
const address = this.state.verifyMsisdn;
this.state.addTask
.haveMsisdnToken(token)
.then(([finished]) => {
let newPhoneNumber = this.state.newPhoneNumber;
if (finished) {
2021-09-21 12:21:51 +02:00
const msisdns = [...this.props.msisdns, { address, medium: ThreepidMedium.Phone }];
this.props.onMsisdnsChange(msisdns);
newPhoneNumber = "";
}
2019-01-23 14:16:18 -07:00
this.setState({
addTask: null,
continueDisabled: false,
verifying: false,
verifyMsisdn: "",
verifyError: null,
newPhoneNumber,
newPhoneNumberCode: "",
2019-01-23 14:16:18 -07:00
});
2022-12-12 12:24:14 +01:00
})
2019-01-23 14:16:18 -07:00
.catch((err) => {
2021-06-29 13:11:58 +01:00
this.setState({ continueDisabled: false });
2019-01-23 14:16:18 -07:00
if (err.errcode !== "M_THREEPID_AUTH_FAILED") {
2021-10-15 16:30:53 +02:00
logger.error("Unable to verify phone number: " + err);
2022-06-14 17:51:51 +01:00
Modal.createDialog(ErrorDialog, {
2019-01-23 14:16:18 -07:00
title: _t("Unable to verify phone number."),
description: err && err.message ? err.message : _t("Operation failed"),
2022-12-12 12:24:14 +01:00
});
2019-01-23 14:16:18 -07:00
} else {
2021-06-29 13:11:58 +01:00
this.setState({ verifyError: _t("Incorrect verification code") });
2019-01-23 14:16:18 -07:00
}
});
};
2021-09-21 12:21:51 +02:00
private onCountryChanged = (country: PhoneNumberCountryDefinition): void => {
this.setState({ phoneCountry: country.iso2 });
2019-01-23 14:16:18 -07:00
};
2021-09-21 12:21:51 +02:00
public render(): JSX.Element {
const existingPhoneElements = this.props.msisdns.map((p) => {
2021-09-21 12:21:51 +02:00
return <ExistingPhoneNumber msisdn={p} onRemoved={this.onRemoved} key={p.address} />;
2019-01-23 14:16:18 -07:00
});
let addVerifySection = (
2021-09-21 12:21:51 +02:00
<AccessibleButton onClick={this.onAddClick} kind="primary">
{_t("Add")}
2019-01-23 14:16:18 -07:00
</AccessibleButton>
);
if (this.state.verifying) {
const msisdn = this.state.verifyMsisdn;
addVerifySection = (
2019-03-12 15:39:30 +00:00
<div>
<div>
{_t(
"A text message has been sent to +%(msisdn)s. " +
"Please enter the verification code it contains.",
{ msisdn: msisdn },
)}
2019-03-12 15:39:30 +00:00
<br />
{this.state.verifyError}
2019-03-12 15:39:30 +00:00
</div>
2021-09-21 12:21:51 +02:00
<form onSubmit={this.onContinueClick} autoComplete="off" noValidate={true}>
<Field
type="text"
label={_t("Verification code")}
autoComplete="off"
disabled={this.state.continueDisabled}
value={this.state.newPhoneNumberCode}
2021-09-21 12:21:51 +02:00
onChange={this.onChangeNewPhoneNumberCode}
/>
2021-04-27 17:23:27 +02:00
<AccessibleButton
2021-09-21 12:21:51 +02:00
onClick={this.onContinueClick}
2021-04-27 17:23:27 +02:00
kind="primary"
disabled={this.state.continueDisabled || this.state.newPhoneNumberCode.length === 0}
2021-04-27 17:23:27 +02:00
>
{_t("Continue")}
2019-03-12 15:39:30 +00:00
</AccessibleButton>
</form>
</div>
2019-01-23 14:16:18 -07:00
);
}
2021-09-21 12:21:51 +02:00
const phoneCountry = (
<CountryDropdown
onOptionChange={this.onCountryChanged}
className="mx_PhoneNumbers_country"
value={this.state.phoneCountry}
disabled={this.state.verifying}
isSmall={true}
showPrefix={true}
/>
);
2019-01-23 14:16:18 -07:00
return (
<div className="mx_PhoneNumbers">
{existingPhoneElements}
2021-09-21 12:21:51 +02:00
<form onSubmit={this.onAddClick} autoComplete="off" noValidate={true} className="mx_PhoneNumbers_new">
2019-01-23 14:16:18 -07:00
<div className="mx_PhoneNumbers_input">
<Field
type="text"
label={_t("Phone Number")}
autoComplete="off"
disabled={this.state.verifying}
2020-05-25 13:40:05 +01:00
prefixComponent={phoneCountry}
value={this.state.newPhoneNumber}
2021-09-21 12:21:51 +02:00
onChange={this.onChangeNewPhoneNumber}
/>
2019-01-23 14:16:18 -07:00
</div>
</form>
{addVerifySection}
2019-01-23 14:16:18 -07:00
</div>
);
}
}