/* Copyright 2017 Vector Creations Ltd 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 sdk from '../../../index'; import AddThreepid from '../../../AddThreepid'; import WithMatrixClient from '../../../wrappers/WithMatrixClient'; import Modal from '../../../Modal'; export default WithMatrixClient(React.createClass({ displayName: 'AddPhoneNumber', propTypes: { matrixClient: React.PropTypes.object.isRequired, onThreepidAdded: React.PropTypes.func, }, getInitialState: function() { return { busy: false, phoneCountry: null, phoneNumber: "", msisdn_add_pending: false, }; }, componentWillMount: function() { this._addThreepid = null; this._addMsisdnInput = null; this._unmounted = false; }, componentWillUnmount: function() { this._unmounted = true; }, _onPhoneCountryChange: function(phoneCountry) { this.setState({ phoneCountry: phoneCountry }); }, _onPhoneNumberChange: function(ev) { this.setState({ phoneNumber: ev.target.value }); }, _onAddMsisdnEditFinished: function(value, shouldSubmit) { if (!shouldSubmit) return; this._addMsisdn(); }, _onAddMsisdnSubmit: function(ev) { ev.preventDefault(); this._addMsisdn(); }, _collectAddMsisdnInput: function(e) { this._addMsisdnInput = e; }, _addMsisdn: function() { const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); this._addThreepid = new AddThreepid(); // we always bind phone numbers when registering, so let's do the // same here. this._addThreepid.addMsisdn(this.state.phoneCountry, this.state.phoneNumber, true).then((resp) => { this._promptForMsisdnVerificationCode(resp.msisdn); }).catch((err) => { console.error("Unable to add phone number: " + err); let msg = err.message; Modal.createDialog(ErrorDialog, { title: "Error", description: msg, }); }).finally(() => { if (this._unmounted) return; this.setState({msisdn_add_pending: false}); }).done(); this._addMsisdnInput.blur(); this.setState({msisdn_add_pending: true}); }, _promptForMsisdnVerificationCode:function (msisdn, err) { if (this._unmounted) return; const TextInputDialog = sdk.getComponent("dialogs.TextInputDialog"); let msgElements = [