2017-11-21 14:55:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-11-24 20:44:52 +00:00
|
|
|
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
|
2017-11-21 14:55:50 +00:00
|
|
|
import { emojify } from 'react-emojione';
|
|
|
|
import Markdown from 'react-native-easy-markdown'; // eslint-disable-line
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
import { actionsShow } from '../../actions/messages';
|
2017-11-21 14:55:50 +00:00
|
|
|
import Card from './Card';
|
|
|
|
import User from './User';
|
|
|
|
import Avatar from '../Avatar';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
content: {
|
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 1
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
padding: 12,
|
|
|
|
paddingTop: 6,
|
|
|
|
paddingBottom: 6,
|
|
|
|
flexDirection: 'row',
|
|
|
|
transform: [{ scaleY: -1 }]
|
|
|
|
},
|
|
|
|
textInfo: {
|
|
|
|
fontStyle: 'italic',
|
|
|
|
color: '#a0a0a0'
|
|
|
|
},
|
|
|
|
editing: {
|
|
|
|
backgroundColor: '#fff5df'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
@connect(state => ({
|
|
|
|
message: state.messages.message,
|
2017-11-24 20:44:52 +00:00
|
|
|
editing: state.messages.editing
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2017-11-24 20:44:52 +00:00
|
|
|
actionsShow: actionMessage => dispatch(actionsShow(actionMessage))
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
|
|
|
export default class Message extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
item: PropTypes.object.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
Message_TimeFormat: PropTypes.string.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
message: PropTypes.object.isRequired,
|
|
|
|
editing: PropTypes.bool,
|
|
|
|
actionsShow: PropTypes.func
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
onLongPress() {
|
|
|
|
const { item } = this.props;
|
|
|
|
this.props.actionsShow(JSON.parse(JSON.stringify(item)));
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isDeleted() {
|
|
|
|
return !this.props.item.msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
attachments() {
|
|
|
|
return this.props.item.attachments.length ? (
|
|
|
|
<Card
|
|
|
|
data={this.props.item.attachments[0]}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMessageContent() {
|
|
|
|
if (this.isDeleted()) {
|
|
|
|
return <Text style={styles.textInfo}>Message removed</Text>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = emojify(this.props.item.msg, { output: 'unicode' });
|
|
|
|
return (
|
|
|
|
<Markdown>
|
|
|
|
{msg}
|
|
|
|
</Markdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-11-24 20:44:52 +00:00
|
|
|
const {
|
|
|
|
item, message, editing
|
|
|
|
} = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
|
|
|
|
const extraStyle = {};
|
|
|
|
if (item.temp) {
|
|
|
|
extraStyle.opacity = 0.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = item.alias || item.u.username;
|
2017-11-24 20:44:52 +00:00
|
|
|
const isEditing = message._id === item._id && editing;
|
2017-11-21 14:55:50 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
2017-11-24 20:44:52 +00:00
|
|
|
onLongPress={() => this.onLongPress()}
|
2017-11-21 14:55:50 +00:00
|
|
|
disabled={this.isDeleted()}
|
2017-11-24 20:44:52 +00:00
|
|
|
style={[styles.message, extraStyle, isEditing ? styles.editing : null]}
|
2017-11-21 14:55:50 +00:00
|
|
|
>
|
2017-11-24 20:44:52 +00:00
|
|
|
<Avatar
|
|
|
|
style={{ marginRight: 10 }}
|
|
|
|
text={item.avatar ? '' : username}
|
|
|
|
size={40}
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
avatar={item.avatar}
|
|
|
|
/>
|
|
|
|
<View style={[styles.content]}>
|
|
|
|
<User
|
|
|
|
onPress={this._onPress}
|
|
|
|
item={item}
|
|
|
|
Message_TimeFormat={this.props.Message_TimeFormat}
|
2017-11-21 14:55:50 +00:00
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
/>
|
2017-11-24 20:44:52 +00:00
|
|
|
{this.attachments()}
|
|
|
|
{this.renderMessageContent(item)}
|
2017-11-21 14:55:50 +00:00
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|