-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChatScreen.js
93 lines (77 loc) · 2.23 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
import React, { Component } from 'react';
import {View, ListView, Text, TextInput, TouchableOpacity} from 'react-native';
import * as firebase from 'firebase';
import _ from 'lodash';
const config = {
apiKey: "AIzaSyDlGqFXQ6TenRQeIUq7oEh5gBUbK6t_rhY",
authDomain: "hackathonchat.firebaseapp.com",
databaseURL: "https://hackathonchat.firebaseio.com",
storageBucket: "hackathonchat.appspot.com",
messagingSenderId: "1058980599391"
};
const firebaseApp = firebase.initializeApp(config);
export default class ChatScreen extends Component{
constructor(props) {
super(props);
this.state={
ds: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
message: ''
}
this.db = firebaseApp.database().ref('/channels/' + props.channel);
}
componentDidMount() {
this.db.on('value', (snap) => {
const messages = [];
snap.forEach((item) => {
messages.push(item.val());
})
_.sortBy(messages, 'timestamp');
this.setState({
ds: this.state.ds.cloneWithRows(messages)
})
})
}
send() {
this.db.push({
sender: this.props.name,
text: this.state.message,
timestamp: Date.now()
})
this.setState({message:''})
}
renderRow(row) {
return (
<View style={{padding: 10}}>
<Text style={{fontSize: 16, color: 'green'}}>
{row.sender}
</Text>
<Text style={{fontSize: 20}}>
{row.text}
</Text>
</View>
)
}
render() {
return (
<View style={{flex: 1}}>
<ListView
style={{flex: 1}}
dataSource={this.state.ds}
renderRow={(row)=>this.renderRow(row)}
/>
<View style={{flexDirection: 'row'}}>
<TextInput
style={{flex: 1, height: 40, borderWidth: 1, borderColor: 'grey', alignSelf: 'center'}}
onChangeText={(message) => this.setState({message})}
value={this.state.message}
/>
<TouchableOpacity onPress={() => this.send()}>
<Text style={[{color: 'blue', padding: 10}]}>
Send!
</Text>
</TouchableOpacity>
</View>
</View>
)
}
}