vn-verdnaturachat/app/presentation/RoomItem.js

170 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-08-09 01:40:55 +00:00
import React from 'react';
2017-11-08 20:23:46 +00:00
import moment from 'moment';
2017-08-09 01:40:55 +00:00
import PropTypes from 'prop-types';
import { View, Text, StyleSheet } from 'react-native';
2018-02-14 20:34:45 +00:00
import { emojify } from 'react-emojione';
import { connect } from 'react-redux';
2017-09-01 20:20:34 +00:00
import Avatar from '../containers/Avatar';
import Touch from '../utils/touch/index'; //eslint-disable-line
2017-08-13 03:19:14 +00:00
2017-08-09 01:40:55 +00:00
const styles = StyleSheet.create({
2017-08-09 18:01:54 +00:00
container: {
flexDirection: 'row',
2017-12-11 20:37:33 +00:00
paddingHorizontal: 16,
2018-02-14 20:34:45 +00:00
paddingVertical: 12,
alignItems: 'flex-start',
borderBottomWidth: 0.5,
borderBottomColor: '#ddd'
2017-08-09 18:01:54 +00:00
},
number: {
2018-02-14 20:34:45 +00:00
minWidth: 25,
borderRadius: 4,
backgroundColor: '#1d74f5',
2017-08-09 18:01:54 +00:00
color: '#fff',
overflow: 'hidden',
fontSize: 14,
2018-02-14 20:34:45 +00:00
paddingVertical: 4,
paddingHorizontal: 5,
2018-02-14 20:34:45 +00:00
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center'
2017-08-09 18:01:54 +00:00
},
2017-11-08 20:23:46 +00:00
roomNameView: {
2017-08-17 19:31:27 +00:00
flex: 1,
2018-02-14 20:34:45 +00:00
height: '100%',
2017-08-17 19:31:27 +00:00
marginLeft: 16,
marginRight: 4
},
2017-11-08 20:23:46 +00:00
roomName: {
flex: 1,
2018-02-14 20:34:45 +00:00
fontSize: 18,
color: '#444',
fontWeight: 'bold',
marginRight: 8
},
lastMessage: {
flex: 1,
flexShrink: 1,
2017-11-08 20:23:46 +00:00
fontSize: 16,
2018-02-14 20:34:45 +00:00
color: '#444',
marginRight: 8
// margin: 0
2017-11-08 20:23:46 +00:00
},
alert: {
fontWeight: 'bold'
},
favorite: {
// backgroundColor: '#eee'
},
2018-02-14 20:34:45 +00:00
row: {
width: '100%',
2017-11-08 20:23:46 +00:00
flex: 1,
2018-02-14 20:34:45 +00:00
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
update: {
2017-11-08 20:23:46 +00:00
fontSize: 10,
2018-02-14 20:34:45 +00:00
color: '#888',
alignItems: 'center',
justifyContent: 'center'
2017-08-09 01:40:55 +00:00
}
});
2018-02-14 20:34:45 +00:00
const renderNumber = (unread, userMentions) => {
if (!unread || unread <= 0) {
return;
}
if (unread >= 1000) {
unread = '999+';
}
if (userMentions > 0) {
unread = `@ ${ unread }`;
}
return (
<Text style={styles.number}>
{ unread }
</Text>
);
};
@connect(state => ({
StoreLastMessage: state.settings.Store_Last_Message
}))
2017-08-09 01:40:55 +00:00
export default class RoomItem extends React.PureComponent {
static propTypes = {
2017-08-17 19:31:27 +00:00
type: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
2017-11-18 20:17:24 +00:00
_updatedAt: PropTypes.instanceOf(Date),
2018-01-19 21:06:55 +00:00
lastMessage: PropTypes.object,
favorite: PropTypes.bool,
alert: PropTypes.bool,
2017-08-17 19:31:27 +00:00
unread: PropTypes.number,
userMentions: PropTypes.number,
2017-08-22 01:24:41 +00:00
baseUrl: PropTypes.string,
onPress: PropTypes.func
}
2017-08-17 19:31:27 +00:00
get icon() {
2017-08-17 19:31:27 +00:00
const { type, name, baseUrl } = this.props;
2018-02-14 20:34:45 +00:00
return <Avatar text={name} baseUrl={baseUrl} size={56} type={type} />;
2017-08-09 01:40:55 +00:00
}
2017-08-13 03:19:14 +00:00
formatDate = date => moment(date).calendar(null, {
2017-12-11 20:37:33 +00:00
lastDay: '[Yesterday]',
sameDay: 'h:mm A',
lastWeek: 'dddd',
sameElse: 'MMM D'
})
2017-08-09 01:40:55 +00:00
render() {
const {
2018-01-19 21:06:55 +00:00
favorite, alert, unread, userMentions, name, lastMessage, _updatedAt
} = this.props;
2017-11-13 12:49:19 +00:00
2017-12-11 20:37:33 +00:00
const date = this.formatDate(_updatedAt);
2018-02-14 20:34:45 +00:00
console.log('do we have a last message?', lastMessage);
2018-01-19 21:06:55 +00:00
2017-12-11 20:37:33 +00:00
let accessibilityLabel = name;
if (unread === 1) {
accessibilityLabel += `, ${ unread } alert`;
} else if (unread > 1) {
accessibilityLabel += `, ${ unread } alerts`;
}
if (userMentions > 0) {
accessibilityLabel += ', you were mentioned';
}
accessibilityLabel += `, last message ${ date }`;
2017-08-09 01:40:55 +00:00
return (
<Touch onPress={this.props.onPress} underlayColor='#FFFFFF' activeOpacity={0.5} accessibilityLabel={accessibilityLabel} accessibilityTraits='selected'>
<View style={[styles.container, favorite && styles.favorite]}>
{this.icon}
<View style={styles.roomNameView}>
2018-02-14 20:34:45 +00:00
<View style={styles.row}>
<Text style={styles.roomName} ellipsizeMode='tail' numberOfLines={1}>{ name }</Text>
{_updatedAt ? <Text style={styles.update} ellipsizeMode='tail' numberOfLines={1}>{ date }</Text> : null}
</View>
<View style={styles.row}>
<Text style={[styles.lastMessage, alert && styles.alert]} ellipsizeMode='tail' numberOfLines={1}>{!this.props.StoreLastMessage ? '' : lastMessage ? `${ lastMessage.u.username }: ${ emojify(lastMessage.msg, { output: 'unicode' }) }` : 'No Message'}</Text>
{renderNumber(unread, userMentions)}
</View>
</View>
2017-11-08 20:23:46 +00:00
</View>
</Touch>
2017-08-09 01:40:55 +00:00
);
}
}