SearchContainer.js 5.14 KB
'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') {
        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);