HomeContainer.js
8.71 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict';
import React from 'react';
import ReactNative from 'react-native';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import Immutable, {Map, List} from 'immutable';
import {Actions} from 'react-native-router-flux';
import Home from '../components/home/Home';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';
import * as userActions from '../reducers/user/userActions';
import * as postingActions from '../reducers/posting/postingActions';
const {
View,
StyleSheet,
Dimensions,
Platform,
NativeModules,
InteractionManager,
NativeAppEventEmitter,
} = ReactNative;
/**
* ## Actions
* 3 of our actions will be available as ```actions```
*/
const actions = [
homeActions,
appActions,
postingActions,
userActions,
];
/**
* Save that state
*/
function mapStateToProps(state) {
return {
...state
};
}
/**
* Bind all the functions from the ```actions``` and bind them with
* ```dispatch```
*/
function mapDispatchToProps(dispatch) {
const creators = Map()
.merge(...actions)
.filter(value => typeof value === 'function')
.toObject();
return {
actions: bindActionCreators(creators, dispatch),
dispatch
};
}
let {width, height} = Dimensions.get('window');
let navbarHeight = (Platform.OS === 'android') ? 50 : 64;
class HomeContainer extends React.Component {
constructor(props) {
super(props);
this._onPressBanner = this._onPressBanner.bind(this);
this._onPressNotice = this._onPressNotice.bind(this);
this._onPressSection = this._onPressSection.bind(this);
this._onPressPost = this._onPressPost.bind(this);
this._onPressAvatar = this._onPressAvatar.bind(this);
this._onPressSectionTag = this._onPressSectionTag.bind(this);
this._onPressComment = this._onPressComment.bind(this);
this._onPressLike = this._onPressLike.bind(this);
this._onRefresh = this._onRefresh.bind(this);
this._onEndReached = this._onEndReached.bind(this);
this._onUploadRetry = this._onUploadRetry.bind(this);
this._onUploadDelete = this._onUploadDelete.bind(this);
this._onUploadSuccess = this._onUploadSuccess.bind(this);
this._onSaveingTheWorld = this._onSaveingTheWorld.bind(this);
this.triggerPullToRefresh = this.triggerPullToRefresh.bind(this);
if (parseInt(this.props.app.container) === 1) {
this.subscription = NativeAppEventEmitter.addListener(
'ChannelReminder',
(reminder) => {
this.props.actions.setChannel(reminder.channel);
Actions.refresh({channel: reminder.channel});
}
);
}
this.subscription2 = NativeAppEventEmitter.addListener(
'UserDidLoginReminder',
(reminder) => {
let uid = reminder.uid;
this.props.actions.setUid(uid);
this.props.actions.syncUserWithSSOUid(uid);
this.triggerPullToRefresh();
}
);
this.subscription3 = NativeAppEventEmitter.addListener(
'UserDidLogoutReminder',
(reminder) => {
this.props.actions.userDidLogout();
this.triggerPullToRefresh();
}
);
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
this.props.actions.syncUserInfo(false);
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.showNativeTabBar) {
ReactNative.NativeModules.YH_CommunityHelper.showTabBar();
Actions.refresh({key: 'Home', showNativeTabBar: false});
}
}
componentWillUnmount() {
if (parseInt(this.props.app.container) === 1) {
this.subscription && this.subscription.remove();
}
this.subscription2 && this.subscription2.remove();
this.subscription3 && this.subscription3.remove();
}
triggerPullToRefresh() {
this.home && this.home.triggerPullToRefresh();
}
_onPressBanner(url) {
this.props.actions.goAction(url);
}
_onPressNotice(url) {
this.props.actions.goAction(url);
}
_onPressSection(section) {
this.props.actions.goToSection(section);
}
_onPressPost(id) {
this.props.actions.goToPost(id);
}
_onPressAvatar(uid) {
this.props.actions.goToUserOrMe(uid);
}
_onPressSectionTag(section) {
this.props.actions.goToSection(section, this.props.navigationState.name);
}
_onPressComment(id) {
this.props.actions.goToPost(id);
}
_onPressLike(post) {
this.props.actions.likeOperation(post);
}
_onRefresh() {
InteractionManager.runAfterInteractions(() => {
this.props.actions.bannerNoticeSection();
this.props.actions.recommendation(true);
});
}
_onEndReached() {
InteractionManager.runAfterInteractions(() => {
this.props.actions.recommendation(false);
});
}
_onUploadSuccess() {
this.props.actions.resetState();
}
_onUploadRetry() {
this.props.actions.startPosting(this.props.posting.assets.toJS());
console.log('upload retry!');
}
_onUploadDelete() {
this.props.actions.resetState();
}
_onSaveingTheWorld() {
this.props.actions.goToUser();
}
render() {
let {inPosting, postFail, finishedCount, postPercent, assets, postingBannerShow, uploadState,} = this.props.posting;
let assetsData = assets.toJS();
let percent = postPercent;
let uri = '';
if (assetsData.length) {
uri = assetsData[0].uri;
} else {
percent = 1;
}
let progressing = [];
if (postingBannerShow) {
progressing = [{
uploadState,//(0发布中,1发布成功,2发布失败)
progress:percent,//(0~1)
image:uri,
postingBannerShow,
}];
}
progressing = Immutable.fromJS(progressing);
let {banner, notice, section, recommendation, ptr, sync, error, isFetching} = this.props.home;
let bannerData = Immutable.fromJS([banner.list]);
let noticeData = notice.open === 'Y' ? Immutable.fromJS([notice.list]) : List();
let sectionData = Immutable.fromJS([section]);
let recommendationData = Immutable.fromJS(recommendation.list);
let {profile} = this.props.user;
let containerStyle = parseInt(this.props.app.container) === 1 ? null : {marginBottom: 64};
return (
<View style={[styles.container, containerStyle]}>
<Home
ref={(c) => {
this.home = c;
}}
progressing={progressing}
banner={bannerData}
bannerDuration={banner.duration}
notice={noticeData}
noticeOpen={notice.open}
noticeDuration={notice.duration}
section={sectionData}
recommendation={recommendationData}
user={profile}
onPressBanner={this._onPressBanner}
onPressNotice={this._onPressNotice}
onPressSection={this._onPressSection}
onPressPost={this._onPressPost}
onPressAvatar={this._onPressAvatar}
onPressSectionTag={this._onPressSectionTag}
onPressComment={this._onPressComment}
onPressLike={this._onPressLike}
onRefresh={this._onRefresh}
isFetching={recommendation.isFetching}
ptr={ptr}
endReached={recommendation.endReached}
isRefreshing={isFetching}
isLoadingMore={!ptr && recommendation.isFetching}
onEndReached={this._onEndReached}
onUploadSuccess={this._onUploadSuccess}
onUploadRetry={this._onUploadRetry}
onUploadDelete={this._onUploadDelete}
isSyncFetching={sync.isFetching}
onSaveingTheWorld={this._onSaveingTheWorld}
ptrError={error}
/>
</View>
);
}
}
let styles = StyleSheet.create({
container: {
top: navbarHeight,
// height: height - navbarHeight - 100,
marginBottom: 64 + 49,
flex: 1,
},
});
export default connect(mapStateToProps, mapDispatchToProps)(HomeContainer);