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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import React from 'react';
import {
FlatList,
View,
StyleSheet,
TouchableOpacity,
} from 'react-native';
import _ from 'lodash';
import {
RkStyleSheet,
RkText,
RkTextInput,
} from 'react-native-ui-kitten';
import { Avatar } from '../../components';
import { FontAwesome } from '../../assets/icons';
import { data } from '../../data';
import NavigationType from '../../config/navigation/propTypes';
const moment = require('moment');
export class ChatList extends React.Component {
static propTypes = {
navigation: NavigationType.isRequired,
};
static navigationOptions = {
title: 'Chats List'.toUpperCase(),
};
state = {
data: {
original: data.getChatList(),
filtered: data.getChatList(),
},
};
extractItemKey = (item) => `${item.withUser.id}`;
onInputChanged = (event) => {
const pattern = new RegExp(event.nativeEvent.text, 'i');
const chats = _.filter(this.state.data.original, chat => {
const filterResult = {
firstName: chat.withUser.firstName.search(pattern),
lastName: chat.withUser.lastName.search(pattern),
};
return filterResult.firstName !== -1 || filterResult.lastName !== -1 ? chat : undefined;
});
this.setState({
data: {
original: this.state.data.original,
filtered: chats,
},
});
};
onItemPressed = (item) => {
const navigationParams = { userId: item.withUser.id };
this.props.navigation.navigate('Chat', navigationParams);
};
renderSeparator = () => (
<View style={styles.separator} />
);
renderInputLabel = () => (
<RkText rkType='awesome'>{FontAwesome.search}</RkText>
);
renderHeader = () => (
<View style={styles.searchContainer}>
<RkTextInput
autoCapitalize='none'
autoCorrect={false}
onChange={this.onInputChanged}
label={this.renderInputLabel()}
rkType='row'
placeholder='Search'
/>
</View>
);
renderItem = ({ item }) => {
const last = item.messages[item.messages.length - 1];
return (
<TouchableOpacity onPress={() => this.onItemPressed(item)}>
<View style={styles.container}>
<Avatar rkType='circle' style={styles.avatar} img={item.withUser.photo} />
<View style={styles.content}>
<View style={styles.contentHeader}>
<RkText rkType='header5'>{`${item.withUser.firstName} ${item.withUser.lastName}`}</RkText>
<RkText rkType='secondary4 hintColor'>
{moment().add(last.time, 'seconds').format('LT')}
</RkText>
</View>
<RkText numberOfLines={2} rkType='primary3 mediumLine' style={{ paddingTop: 5 }}>
{last.text}
</RkText>
</View>
</View>
</TouchableOpacity>
);
};
render = () => (
<FlatList
style={styles.root}
data={this.state.data.filtered}
extraData={this.state}
ListHeaderComponent={this.renderHeader}
ItemSeparatorComponent={this.renderSeparator}
keyExtractor={this.extractItemKey}
renderItem={this.renderItem}
/>
);
}
const styles = RkStyleSheet.create(theme => ({
root: {
backgroundColor: theme.colors.screen.base,
},
searchContainer: {
backgroundColor: theme.colors.screen.bold,
paddingHorizontal: 16,
paddingVertical: 10,
height: 60,
alignItems: 'center',
},
container: {
paddingLeft: 19,
paddingRight: 16,
paddingBottom: 12,
paddingTop: 7,
flexDirection: 'row',
},
content: {
marginLeft: 16,
flex: 1,
},
contentHeader: {
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 6,
},
separator: {
height: StyleSheet.hairlineWidth,
backgroundColor: theme.colors.border.base,
},
}));
|