-
Notifications
You must be signed in to change notification settings - Fork 77
/
chat.js
112 lines (97 loc) · 2.4 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TouchableHighlight,
View,
TextInput,
ListView,
ScrollView,
Dimensions,
Image
} from 'react-native';
import { GiftedChat, Bubble } from 'react-native-gifted-chat';
class Chat extends Component {
constructor(props) {
super(props);
this.state ={
info: 'Initializing',
user: {
_id: 2
},
messages: [
{
_id: 1,
text: 'Hello developer',
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
avatar: 'https://facebook.github.io/react/img/logo_og.png',
},
},
{
_id: 2,
text: 'Hello dev',
createdAt: new Date(),
user: {
_id: 3,
name: 'atyenoria',
avatar: 'https://facebook.github.io/react/img/logo_og.png',
},
},
],
};
}
onSend(messages = []) {
this.setState((previousState) => ({
messages: GiftedChat.append(previousState.messages, messages),
}));
}
componentDidMount(){
}
renderName = (props) => {
const { user: self } = this.state
const { user = {} } = props.currentMessage
const { user: pUser = {} } = props.previousMessage
const isSameUser = pUser._id === user._id
const isSelf = user._id === self._id
const shouldNotRenderName = isSameUser
return shouldNotRenderName ? (
<View />
) : (
<Text
style={[ isSelf ? styles.selfUser : styles.otherUser ]}>
{user.name}
</Text>
)
}
renderBubble = (props) => {
return (
<View>
{this.renderName(props)}
<Bubble {...props} />
</View>
)
}
render() {
return(
<GiftedChat
messages={this.state.messages}
renderBubble={this.renderBubble}
onSend={(messages) => this.onSend(messages)}
user={{
_id: 1,
}}
/>
)
}
}
const styles = StyleSheet.create({
selfView: {
width: 200,
height: 150,
}
});
export default Chat