-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.js
72 lines (63 loc) · 2.24 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
import React, { useState, createRef } from 'react';
import { Text, StyleSheet, TextInput, ScrollView, SafeAreaView, View } from 'react-native';
import { connect } from 'react-redux';
import { addChatItem } from './actions';
import BottomNavigation from './BottomNavigation';
import styles from './styles';
import Screen from './Screen';
import { randomString } from './randomString';
const ChatItem = ({ item }) => {
return (
<Text style={styles.chatScreen.chatItem.usernameTextStyle}>{item.user}:
<Text style={styles.chatScreen.chatItem.textStyle}>{item.text}</Text>
</Text>
)
}
const Chat = ({ generalSettings, personalSettings, chatItems, onAddChatItem, navigation }) => {
const [internalText, setInternalText] = useState('');
const scrollViewRef = createRef();
// const fontScaleFactor = personalSettings.fontScaleFactor || 1;
const handleSubmit = (text) => {
onAddChatItem({
id: randomString(10),
user: personalSettings.userName || 'unknown',
text: text
})
setInternalText('');
}
return (
<Screen screenName="Chat" navigation={navigation}>
<ScrollView
style={{ flex: 1 }}
ref={scrollViewRef}
onContentSizeChange={(width, height) => {
scrollViewRef.current && scrollViewRef.current.scrollTo({y: height})
}}
>
{
chatItems.map(item => (
<ChatItem item={item} key={item.id} />
))
}
</ScrollView>
<TextInput
style={styles.chatScreen.inputTextStyle}
onChangeText={text => setInternalText(text)}
value={internalText}
onSubmitEditing={(evt) => handleSubmit(evt.nativeEvent.text)}
/>
</Screen>
)
}
const mapStateToProps = state => ({
chatItems: state.chatItems,
generalSettings: state.generalSettings,
personalSettings: state.personalSettings
})
const mapDispatchToProps = dispatch => ({
onAddChatItem: item => dispatch(addChatItem(item))
})
export default connect(
mapStateToProps,
mapDispatchToProps
)(Chat)