-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
110 lines (95 loc) · 2.46 KB
/
App.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { Text } from 'react-native';
import { GiftedChat, Bubble } from 'react-native-gifted-chat';
import { ChatManager, TokenProvider } from "pusher-chatkit-client/react-native";
//import messages from './messages';
class App extends Component {
state = {
messages: [],
room: '',
}
componentDidMount() {
this.setState(() => {
return {
messages: [],
};
});
const tokenProvider = new TokenProvider({
url: "CHANGE_TO_YOUR_URL",
userId: "ridhwan"
});
const chatManager = new ChatManager({
instanceLocator: 'CHANGE_TO_YOUR_INSTANCE_LOCATOR',
userId: "ridhwan",
tokenProvider: tokenProvider
});
chatManager.connect({
delegate: {
addedToRoom: (room) => {
console.log(`Added to room ${room.name}`);
},
},
onSuccess: (currentUser) => {
const roomToSubscribeTo = currentUser.rooms[0];
this.setState({ room :roomToSubscribeTo, currentUser });
let arrMsg = [];
currentUser.subscribeToRoom(
roomToSubscribeTo,
{
newMessage: (message) => {
arrMsg = {
text: message.text,
user: {
_id: 1,
name: message.sender.name,
},
_id: Math.round(Math.random() * 1000000),
createdAt: message.createdAt,
};
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, arrMsg),
}))
}
}
);
},
onError: (error) => {
console.log("Error on connection");
}
});
}
onSend(messages = []) {
// this.setState(previousState => ({
// messages: GiftedChat.append(previousState.messages, messages),
// }))
this.state.currentUser.sendMessage(
{
text: messages[0].text,
roomId: this.state.room.id,
},
(messageId) => {
console.log("Success!", messageId);
},
(error) => {
console.log("Error", error);
}
)
}
render() {
return (
<GiftedChat
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: 1,
}}
/>
)
}
}
export default App;