element-web/src/controllers/pages/MatrixChat.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

// should be atomised
2015-06-12 13:59:33 +00:00
var Loader = require("react-loader");
var mxCliPeg = require("../../MatrixClientPeg");
var dis = require("../../dispatcher");
module.exports = {
getInitialState: function() {
return {
2015-06-12 13:59:33 +00:00
logged_in: !!(mxCliPeg.get() && mxCliPeg.get().credentials),
ready: false
};
},
2015-06-12 13:59:33 +00:00
componentDidMount: function() {
this.dispatcherRef = dis.register(this.onAction);
2015-06-12 13:59:33 +00:00
if (this.state.logged_in) {
this.startMatrixClient();
}
this.focusComposer = false;
2015-06-12 13:59:33 +00:00
},
componentWillUnmount: function() {
dis.unregister(this.dispatcherRef);
},
componentDidUpdate: function() {
if (this.focusComposer) {
dis.dispatch({action: 'focus_composer'});
this.focusComposer = false;
}
},
onAction: function(payload) {
switch (payload.action) {
case 'logout':
this.setState({
logged_in: false,
ready: false
});
2015-06-16 13:37:15 +00:00
mxCliPeg.get().removeAllListeners();
mxCliPeg.replace(null);
break;
2015-06-12 17:01:38 +00:00
case 'view_room':
this.setState({
currentRoom: payload.room_id
});
this.focusComposer = true;
2015-06-12 17:01:38 +00:00
break;
}
},
onLoggedIn: function() {
this.setState({logged_in: true});
2015-06-12 13:59:33 +00:00
this.startMatrixClient();
},
startMatrixClient: function() {
var cli = mxCliPeg.get();
var that = this;
cli.on('syncComplete', function() {
var firstRoom = null;
if (cli.getRooms() && cli.getRooms().length) {
firstRoom = cli.getRooms()[0].roomId;
}
that.setState({ready: true, currentRoom: firstRoom});
dis.dispatch({action: 'focus_composer'});
2015-06-12 13:59:33 +00:00
});
cli.startClient();
},
};