vn-verdnaturachat/app/containers/status.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { StyleSheet, View, ViewPropTypes } from 'react-native';
import { STATUS_COLORS } from '../constants/colors';
const styles = StyleSheet.create({
status: {
borderRadius: 16,
width: 16,
height: 16
}
});
@connect(state => ({
2018-06-05 01:17:02 +00:00
activeUsers: state.activeUsers,
2018-06-13 01:29:18 +00:00
user: state.login.user,
offline: !state.meteor.connected
}))
export default class Status extends React.Component {
static propTypes = {
style: ViewPropTypes.style,
id: PropTypes.string,
2018-06-13 01:29:18 +00:00
activeUsers: PropTypes.object,
user: PropTypes.object,
offline: PropTypes.bool
};
shouldComponentUpdate(nextProps) {
2018-06-05 01:17:02 +00:00
const { id: userId, user } = this.props;
if (user.id === userId) {
2018-06-13 01:29:18 +00:00
if (nextProps.offline !== this.props.offline) {
return true;
}
2018-06-05 01:17:02 +00:00
return (nextProps.user && nextProps.user.status !== user.status);
}
return (nextProps.activeUsers[userId] && nextProps.activeUsers[userId].status) !== this.status;
}
get status() {
2018-06-13 01:29:18 +00:00
const { id: userId, user, offline } = this.props;
2018-06-05 01:17:02 +00:00
if (user.id === userId) {
2018-06-13 01:29:18 +00:00
if (offline) {
return 'offline';
}
2018-06-05 01:17:02 +00:00
return user.status || 'offline';
}
return (this.props.activeUsers && this.props.activeUsers[userId] && this.props.activeUsers[userId].status) || 'offline';
}
render() {
return (<View style={[styles.status, this.props.style, { backgroundColor: STATUS_COLORS[this.status] }]} />);
}
}