SearchContainer.js
5.17 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
'use strict'
import React, {Component} from 'react';
import ReactNative, {
StyleSheet,
Dimensions,
Platform,
View,
NativeModules,
InteractionManager,
NativeAppEventEmitter,
} from 'react-native'
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import Immutable, {Map, List} from 'immutable';
import * as searchActions from '../reducers/search/searchActions';
import SearchKeyword from '../components/search/SearchKeyword';
import FuzzySearch from '../components/search/FuzzySearch';
import SearchResult from '../components/search/SearchResult';
import ProductList from '../components/search/ProductList';
import LoadingIndicator from '../../common/components/LoadingIndicator';
const actions = [
searchActions,
];
function mapStateToProps(state) {
return {
...state
};
}
function mapDispatchToProps(dispatch) {
const creators = Map()
.merge(...actions)
.filter(value => typeof value === 'function')
.toObject();
return {
actions: bindActionCreators(creators, dispatch),
dispatch
};
}
class SearchContainer extends Component {
constructor(props) {
super(props);
this._onPressKeyword = this._onPressKeyword.bind(this);
this._onPressClearHistory = this._onPressClearHistory.bind(this);
this._onPressFilter = this._onPressFilter.bind(this);
this._onEndReached = this._onEndReached.bind(this);
this._onPressShop = this._onPressShop.bind(this);
this._onPressProduct = this._onPressProduct.bind(this);
this.subscription = NativeAppEventEmitter.addListener(
"SearchKeywordDidChangeEvent",
(event) => {
this.props.actions.searchKeywordChanged(event.keyword);
}
);
this.subscription2 = NativeAppEventEmitter.addListener(
"SearchButtonDidClickEvent",
(event) => {
this.props.actions.resetListPageInfo();
this.props.actions.searchButtonPressed(event.keyword);
}
);
}
componentDidMount() {
this.props.actions.searchHistory();
this.props.actions.hotKeyword();
ReactNative.NativeModules.YH_SearchHelper.wakeUpSearchBar();
}
componentWillUnmount() {
this.subscription && this.subscription.remove();
this.subscription2 && this.subscription2.remove();
}
_onPressKeyword(keyword) {
this.props.actions.resetListPageInfo();
this.props.actions.searchButtonPressed(keyword);
}
_onPressClearHistory() {
this.props.actions.clearSearchHistory();
}
_onPressFilter(value) {
this.props.actions.resetListPageInfo();
this.props.actions.setFilter(value);
this.props.actions.searchProductList(this.props.search.keyword, true);
}
_onEndReached() {
this.props.actions.searchProductList(this.props.search.keyword);
}
_onPressShop(data) {
ReactNative.NativeModules.YH_SearchHelper.goToBrandShop(data.toJS());
}
_onPressProduct(data, index='0') {
console.log(index)
ReactNative.NativeModules.YH_SearchHelper.goToProductDetail(data.toJS(), index, this.props.search.productList.filter);
}
_renderSearch() {
let {status, keyword, placeholder, searchHistory, hotKeyword, fuzzySearch, jumpUrl, productList} = this.props.search;
if (status == 0) {
return (
<SearchKeyword
history={searchHistory.list}
hot={hotKeyword.list}
onPressKeyword={this._onPressKeyword}
onPressClearHistory={this._onPressClearHistory}
/>
);
} else if (status == 1) {
return (
<FuzzySearch
list={fuzzySearch.list}
onPressKeyword={this._onPressKeyword}
/>
);
} else if (status == 2) {
let isLoadingMore = productList.isFetching && productList.currentPage > 0;
return (
<ProductList
shop={productList.shopOrBrand}
list={productList.list}
isFetching={productList.isFetching}
isLoadingMore={isLoadingMore}
endReached={productList.endReached}
onPressFilter={this._onPressFilter}
onEndReached={this._onEndReached}
onPressShop={this._onPressShop}
onPressProduct={this._onPressProduct}
/>
);
}
return null;
}
render() {
let {jumpUrl, productList} = this.props.search;
let showLoading = jumpUrl.isFetching || (productList.isFetching && productList.currentPage == 0);
return (
<View style={styles.container}>
{this._renderSearch()}
<LoadingIndicator
isVisible={showLoading}
/>
</View>
);
}
}
let styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default connect(mapStateToProps, mapDispatchToProps)(SearchContainer);