2018-02-16 18:34:25 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2019-03-01 16:49:11 +00:00
|
|
|
import Status from './Status';
|
2019-04-29 16:03:52 +00:00
|
|
|
import database, { safeAddListener } from '../../lib/realm';
|
2018-02-16 18:34:25 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class StatusContainer extends React.PureComponent {
|
2018-02-16 18:34:25 +00:00
|
|
|
static propTypes = {
|
2019-04-29 16:03:52 +00:00
|
|
|
id: PropTypes.string,
|
2019-05-22 20:15:35 +00:00
|
|
|
style: PropTypes.any,
|
2019-03-01 16:49:11 +00:00
|
|
|
size: PropTypes.number,
|
2018-06-13 01:29:18 +00:00
|
|
|
offline: PropTypes.bool
|
2018-02-16 18:34:25 +00:00
|
|
|
};
|
|
|
|
|
2019-03-01 16:49:11 +00:00
|
|
|
static defaultProps = {
|
|
|
|
size: 16
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.user = database.memoryDatabase.objects('activeUsers').filtered('id == $0', props.id);
|
|
|
|
this.state = {
|
|
|
|
user: this.user[0] || {}
|
|
|
|
};
|
|
|
|
safeAddListener(this.user, this.updateState);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.user.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:34:25 +00:00
|
|
|
get status() {
|
2019-04-29 16:03:52 +00:00
|
|
|
const { user } = this.state;
|
|
|
|
const { offline } = this.props;
|
|
|
|
if (offline || !user) {
|
2018-07-10 13:40:32 +00:00
|
|
|
return 'offline';
|
2018-06-05 01:17:02 +00:00
|
|
|
}
|
2019-04-29 16:03:52 +00:00
|
|
|
return user.status || 'offline';
|
|
|
|
}
|
|
|
|
|
|
|
|
updateState = () => {
|
|
|
|
if (this.user.length) {
|
|
|
|
this.setState({ user: this.user[0] });
|
|
|
|
}
|
2018-02-16 18:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-03-01 16:49:11 +00:00
|
|
|
const { style, size } = this.props;
|
|
|
|
return <Status size={size} style={style} status={this.status} />;
|
2018-02-16 18:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
offline: !state.meteor.connected
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(StatusContainer);
|