vn-verdnaturachat/app/containers/Status/index.js

59 lines
1.2 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Status from './Status';
2019-04-29 16:03:52 +00:00
import database, { safeAddListener } from '../../lib/realm';
class StatusContainer extends React.PureComponent {
static propTypes = {
2019-04-29 16:03:52 +00:00
id: PropTypes.string,
style: PropTypes.any,
size: PropTypes.number,
2018-06-13 01:29:18 +00:00
offline: PropTypes.bool
};
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();
}
get status() {
2019-04-29 16:03:52 +00:00
const { user } = this.state;
const { offline } = this.props;
if (offline || !user) {
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] });
}
}
render() {
const { style, size } = this.props;
return <Status size={size} style={style} status={this.status} />;
}
}
const mapStateToProps = state => ({
offline: !state.meteor.connected
});
export default connect(mapStateToProps)(StatusContainer);