-
Notifications
You must be signed in to change notification settings - Fork 62
/
PmList.js
223 lines (197 loc) · 6.02 KB
/
PmList.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import React, { Component } from 'react';
import { connect } from 'react-redux';
import {
View,
Text,
Image,
AlertIOS,
ScrollView
} from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import GiftedChatSendButton from '~/common/vendor/components/GiftedChatSendButton';
import GiftedChatLoadEarlierButton from '~/common/vendor/components/GiftedChatLoadEarlierButton';
import GiftedChatMessageText from '~/common/vendor/components/GiftedChatMessageText';
import LoadingSpinner from '~/components/LoadingSpinner/LoadingSpinner';
import {
fetchPmList,
cancelPmList,
resetPmList,
resetPmListResponseStatus
} from '~/modules/message/pmList/pmList.ducks';
import { PRIVATE_MESSAGE_POLL_FREQUENCY } from '~/config/app';
import api from '~/services/api';
import mainStyles from '~/common/styles/Main.style';
import styles from './PmList.style';
const LOGIN_USER_ID = Symbol();
class PmList extends Component {
static navigationOptions = ({ navigation }) => {
const { title } = navigation.state.params;
return {
title
};
}
constructor(props) {
super(props);
const { userId } = this.props.navigation.state.params;
this.userId = userId;
this.state = {
messages: [],
isPublishing: false
};
}
componentDidMount() {
this.fetchPmList();
// Fetch new private messages every 20 secs.
this.timer = setInterval(() => { this.fetchPmList(); }, 1000 * PRIVATE_MESSAGE_POLL_FREQUENCY);
}
componentWillUnmount() {
this.props.cancelPmList();
this.props.resetPmList();
// Tear down timer.
this.timer && clearInterval(this.timer);
}
fetchPmList() {
this.props.fetchPmList({
userId: this.userId,
page: 1
});
}
setUpTitle(newUserName) {
const userName = this.props.pmList.user.name;
if (userName !== newUserName) {
this.props.navigation.setParams({ title: newUserName });
}
}
componentWillReceiveProps(nextProps) {
const { pmList } = nextProps;
this.setUpTitle(pmList.user.name);
// Handle private messages.
const { isPublishing } = this.state;
if (isPublishing) { return; }
if (pmList.isRefreshing) { return; }
// Translation from Redux store props to component state.
if (pmList.response && pmList.response.rs) {
this.setState({
messages: pmList.list
});
this.props.resetPmListResponseStatus();
}
}
loadEarlierMessages(page) {
this.props.fetchPmList({
userId: this.userId,
page
});
}
onSend({ messages, toUserId }) {
this.setState(previousState => {
return {
messages: GiftedChat.append(previousState.messages, Object.assign({}, messages[0], { isNew: true }))
};
});
this.setState({ isPublishing: true });
api.sendMessage({
newMessage: messages[0],
toUserId
}).then(response => {
if (response.error && response.error.message === 'Network request failed') {
// No network.
this.setState(previousState => {
return {
messages: previousState.messages.filter(message => !message.isNew)
};
});
return;
}
const { rs, errcode } = response.data;
if (rs) {
// This is workaround to fix #14.
// In produciton, sometimes the new sent message can not be displayed
// in bubble list even it has been sent successfully, but it's very
// hard to be reproduced in development mode. As we know, JavaScript
// thread performance suffers greatly when running in development mode,
// so the workaround can not only fix the weird issue here, but also can
// give user a better ux with customized ticks `发送中...`, which seems
// like the best solution now.
setTimeout(() => { this.fetchPmList(); }, 1000 * 3);
} else if (errcode) {
// The time between sending two messages is too short.
this.fetchPmList();
AlertIOS.alert('提示', errcode);
}
}).finally(() => {
this.setState({ isPublishing: false });
});
}
render() {
const {
navigation,
pmList: {
isRefreshing,
hasPrev,
user,
page
}
} = this.props;
const { isPublishing } = this.state;
if (isRefreshing && page === 0) {
return (
<LoadingSpinner />
);
}
const messages = this.state.messages.map(item => {
if (item.isNew) { return item; }
return {
_id: item.mid,
text: item.content,
createdAt: new Date(+item.time),
user: {
_id: (item.sender === user.id) && item.sender || LOGIN_USER_ID,
avatar: (item.sender === user.id) && user.avatar
}
};
});
return (
<View style={mainStyles.container}>
<GiftedChat
style={mainStyles.container}
locale={'zh-cn'}
placeholder='请输入私信内容'
sendButtonLabel='发送'
loadEarlierLabel='点击加载较早的消息'
isLoadingEarlier={isRefreshing && page > 1}
loadEarlier={hasPrev}
renderAvatarOnTop={true}
onLoadEarlier={() => this.loadEarlierMessages(page + 1)}
onSend={messages => this.onSend({
messages,
toUserId: user.id
})}
renderSend={props => <GiftedChatSendButton {...props} />}
renderLoadEarlier={props => <GiftedChatLoadEarlierButton {...props} />}
renderMessageText={props => <GiftedChatMessageText {...props} />}
renderTicks={message => {
if (!message.isNew) { return; }
return (
<View style={styles.tickView}>
{isPublishing && <Text style={styles.tick}>发布中...</Text>}
</View>
);
}}
messages={messages}
user={{ _id: LOGIN_USER_ID }} />
</View>
);
}
}
function mapStateToProps({ pmList }) {
return {
pmList
};
}
export default connect(mapStateToProps, {
fetchPmList,
cancelPmList,
resetPmList,
resetPmListResponseStatus
})(PmList);