2015-10-07 17:19:29 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-10-07 17:19:29 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2015-11-20 11:57:04 +00:00
|
|
|
var React = require('react');
|
2015-11-26 17:10:36 +00:00
|
|
|
var sdk = require('../../../index');
|
|
|
|
var MatrixClientPeg = require("../../../MatrixClientPeg");
|
2015-10-07 17:19:29 +00:00
|
|
|
|
2015-11-26 17:10:36 +00:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ChangeDisplayName',
|
2015-10-07 17:19:29 +00:00
|
|
|
|
2016-07-29 16:35:48 +00:00
|
|
|
_getDisplayName: function() {
|
2015-10-07 17:19:29 +00:00
|
|
|
var cli = MatrixClientPeg.get();
|
2016-07-29 16:35:48 +00:00
|
|
|
return cli.getProfileInfo(cli.credentials.userId).then(function(result) {
|
2016-03-19 02:45:06 +00:00
|
|
|
var displayname = result.displayname;
|
|
|
|
if (!displayname) {
|
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
displayname = "Guest " + MatrixClientPeg.get().getUserIdLocalpart();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
displayname = MatrixClientPeg.get().getUserIdLocalpart();
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 16:35:48 +00:00
|
|
|
return displayname;
|
2015-10-07 17:19:29 +00:00
|
|
|
}, function(error) {
|
2016-07-29 16:35:48 +00:00
|
|
|
throw new Error("Failed to fetch display name");
|
2015-10-07 17:19:29 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-07-29 16:35:48 +00:00
|
|
|
_changeDisplayName: function(new_displayname) {
|
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
return cli.setDisplayName(new_displayname).catch(function(e) {
|
|
|
|
throw new Error("Failed to set display name");
|
2015-10-07 17:19:29 +00:00
|
|
|
});
|
|
|
|
},
|
2015-11-26 17:10:36 +00:00
|
|
|
|
|
|
|
render: function() {
|
2016-07-29 16:35:48 +00:00
|
|
|
var EditableTextContainer = sdk.getComponent('elements.EditableTextContainer');
|
|
|
|
return (
|
|
|
|
<EditableTextContainer
|
|
|
|
getInitialValue={this._getDisplayName}
|
|
|
|
placeholder="No display name"
|
2017-03-02 17:29:06 +00:00
|
|
|
blurToSubmit={true}
|
2016-07-29 16:35:48 +00:00
|
|
|
onSubmit={this._changeDisplayName} />
|
|
|
|
);
|
2015-11-26 17:10:36 +00:00
|
|
|
}
|
|
|
|
});
|