ProductListPoolContainer.js 4.93 KB
'use strict'

import React, {Component} from 'react';
import ReactNative, {
    StyleSheet,
    Platform,
    InteractionManager,
    NativeAppEventEmitter,
} from 'react-native'

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as productListPoolActions from '../reducers/productListPool/productListPoolActions';
import ProductListPool from '../components/productListPool/ProductListPool';

const actions = [
    productListPoolActions,
];

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 ProductListPoolContainer extends Component {
    constructor(props) {
        super(props);
        this._onEndReached = this._onEndReached.bind(this);
        this._onLongPressProduct = this._onLongPressProduct.bind(this);
        this._onPressFindSimilar = this._onPressFindSimilar.bind(this);
        this._onPressProductCell = this._onPressProductCell.bind(this);
        this._onPressBanner = this._onPressBanner.bind(this);
        this._onPressProductFilter = this._onPressProductFilter.bind(this);
        this.subscription = NativeAppEventEmitter.addListener(
            'updateProductByFilter',
            (array) => {
                if (array.length > 0) {
                    this.props.actions.setProductFilterFactors(array);
                    this.props.actions.resetListPageInfo();
                    this.props.actions.getProductList(true);
                    this.props.actions.setSimilarIndex(-1);
                }
            }
        );
    }

    componentDidMount() {
        let {
            type
        } = this.props.app;

        if (type == 'YH_ProductListPoolView') {
            this.props.actions.getProductList();
        }else if (type == 'YH_VipProListVC') {
            this.props.actions.getBannerListWithContentCode();
            this.props.actions.getProductList();
        }else if (type == 'YH_OutLetProListVC') {
            this.props.actions.getActivity();
        }else if (type == 'YH_PromotionProductListVC') {
            this.props.actions.getProductList();
        }else if (type == 'YH_InstalmentView') {
            this.props.actions.getProductList();
        }else if (type == 'YH_NewUserView') {
            this.props.actions.getProductList();
        }
    }

    componentWillUnmount() {
        this.subscription && this.subscription.remove();
    }

    _onPressProductFilter(value) {
        this.props.actions.resetListPageInfo();
        this.props.actions.setProductListFilter(value);
        this.props.actions.getProductList(true);
        this.props.actions.setSimilarIndex(-1);
    }

    _onEndReached() {
        this.props.actions.getProductList();
    }

    _onPressBanner (url){
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    _onPressProductCell(product, rowId=0) {
        let productSkn = product && product.get('product_skn', 0);
        if (!productSkn) {
            return;
        }
        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${productSkn}"}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    _onLongPressProduct(rowID) {
        if (rowID) {
            this.props.actions.setSimilarIndex(rowID);
        }
    }

    _onPressFindSimilar(product) {
        if (!product) {
            return;
        }

        ReactNative.NativeModules.YH_CommonHelper.jumpFindSimilar(product.toJS());
    }

    render() {

        let {
            productList,
            similarIndex,
            filterFactors,
            filterNameFactors,
            bannerInfo,
            activityInfo,
        } = this.props.productListPool;

        let {
            type,
            originParams,
        } = this.props.app;

        return (
            <ProductListPool
                type = {type}
                bannerInfo = {bannerInfo}
                activityInfo = {activityInfo}
                similarIndex = {similarIndex}
                productList = {productList}
                originParams = {originParams}
                filterFactors = {filterFactors}
                filterNameFactors = {filterNameFactors}
                onEndReached={this._onEndReached}
                onPressProductCell={this._onPressProductCell}
                onLongPressProduct={this._onLongPressProduct}
                onPressFindSimilar={this._onPressFindSimilar}
                onPressProductFilter={this._onPressProductFilter}
                onPressBanner = {this._onPressBanner}
            />
        );
    }
}

let styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});

export default connect(mapStateToProps, mapDispatchToProps)(ProductListPoolContainer);