-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChatScreen.js
101 lines (84 loc) · 2.19 KB
/
ChatScreen.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
import React from 'react';
import { GiftedChat } from 'react-native-gifted-chat';
import { images } from '../constants';
// components
import CustomAccessoryBar from '../components/CustomAccessoryBar';
import CustomMessage from '../components/CustomMessage';
class ChatScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
firstMessage: true,
messages: []
};
this.onSend = this.onSend.bind(this);
this.autoRespond = this.autoRespond.bind(this);
}
componentDidMount() {
this.setState({
messages: [
{
_id: 1,
text: 'Hello developer!',
createdAt: new Date(),
user: {
_id: 1,
avatar: images.profile,
name: 'Caleb Nance'
}
}
]
});
}
onSend(messages = []) {
const { firstMessage } = this.state;
// add user image
const formattedMessage = messages;
// if first message, add avatar image
if (firstMessage) {
formattedMessage[0].user.avatar = images.user2;
formattedMessage[0].user.name = 'Developer';
formattedMessage[0].user._id = 3;
this.setState({
firstMessage: false
});
// auto-respond
this.autoRespond();
}
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, formattedMessage)
}));
}
autoRespond() {
const responseMsg = [
{
_id: 4,
text:
'Thanks for checking this app clone out!\rSee more app clones on my github:\rhttps://github.com/calebnance/',
createdAt: new Date(),
user: {
avatar: images.profile,
name: 'Caleb Nance'
}
}
];
setTimeout(() => {
this.onSend(responseMsg);
}, 2000);
}
render() {
const { messages } = this.state;
return (
<GiftedChat
alwaysShowSend
messages={messages}
onSend={msgs => this.onSend(msgs)}
renderAccessory={props => <CustomAccessoryBar {...props} />}
renderMessage={props => <CustomMessage {...props} />}
renderSend={() => null}
user={{ _id: 1 }}
/>
);
}
}
export default ChatScreen;