HomeContainer.js 10.7 KB
'use strict'

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

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as homeActions from '../reducers/home/homeActions';
import * as appActions from '../reducers/app/appActions';

import Home from '../components/home/Home';

const actions = [
    homeActions,
    appActions
];

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 HomeContainer extends Component {
    constructor(props) {
        super(props);

        this.onRefresh = this.onRefresh.bind(this);
        this.onEndReached = this.onEndReached.bind(this);
        this.onPressAnnounceItem = this.onPressAnnounceItem.bind(this);
        this.onPressSlideItem = this.onPressSlideItem.bind(this);
        this.onPressPicItem = this.onPressPicItem.bind(this);
        this.onPressImageItem = this.onPressImageItem.bind(this);
        this.onPressRecommendSlideItem = this.onPressRecommendSlideItem.bind(this);
        this.onPressRecommendListItem = this.onPressRecommendListItem.bind(this);
        this.onPressCustomBrandItem = this.onPressCustomBrandItem.bind(this);
        this.onPressTrendArticleItem = this.onPressTrendArticleItem.bind(this);
        this.onPressTrendRecommendItem = this.onPressTrendRecommendItem.bind(this);
        this.onPressTrendTopicItem = this.onPressTrendTopicItem.bind(this);
        this.onPressBrandItem = this.onPressBrandItem.bind(this);
        this.onPressVipBannerItem = this.onPressVipBannerItem.bind(this);
        this.onPressVipProduct = this.onPressVipProduct.bind(this);
        this.onPressAppIconItem = this.onPressAppIconItem.bind(this);
        this.onPressHotBrandItem = this.onPressHotBrandItem.bind(this);
        this.onPressShopRecommendItem = this.onPressShopRecommendItem.bind(this);
        this.onPressShopFavorite = this.onPressShopFavorite.bind(this);
        this.onPressTitleMore = this.onPressTitleMore.bind(this);
        this.onClickSort = this.onClickSort.bind(this);
        this.onClickRecommendProduct = this.onClickRecommendProduct.bind(this);
        this.onLongPressProduct = this.onLongPressProduct.bind(this);
        this.onPressFindSimilar = this.onPressFindSimilar.bind(this);
        this.onPressDislike = this.onPressDislike.bind(this);
        this.onPressProductListProduct = this.onPressProductListProduct.bind(this);
        this.onPressToProductDetail = this.onPressToProductDetail.bind(this);
        this.onPressReloadData = this.onPressReloadData.bind(this);

        this.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                //增加了IOS的判断,Android不需要此方法,因为Android首页的四个频道为四个不同的原生界面,调用此方法会导致重复渲染数据
                if (Platform.OS === 'ios') {
                    this.props.actions.setChannel(reminder.channel);
                    this.home && this.home.trigggePullToRefresh(true);
                }
            }
        );
        this.subscription2 = NativeAppEventEmitter.addListener(
            'UserDidLoginEvent',
            (reminder) => {
                this.home && this.home.trigggePullToRefresh();
            }
        );

        this.subscription3 = NativeAppEventEmitter.addListener(
            'UserDidLogoutEvent',
            (reminder) => {
                this.home && this.home.trigggePullToRefresh();
            }
        );

        this.subscription4 = NativeAppEventEmitter.addListener(
            'ABTestDidChangeEvent',
            (reminder) => {
                this.home && this.home.trigggePullToRefresh();
            }
        );

        this.subscription5 = NativeAppEventEmitter.addListener(
            'UserDidRegisterEvent',
            (reminder) => {
                this.home && this.home.trigggePullToRefresh();
            }
        );
    }

    componentDidMount() {
        this.props.actions.loadChannelCachedData();
        this.home && this.home.trigggePullToRefresh();
    }

    componentWillUnmount() {
        this.subscription && this.subscription.remove();
        this.subscription2 && this.subscription2.remove();
        this.subscription3 && this.subscription3.remove();
        this.subscription4 && this.subscription4.remove();
        this.subscription5 && this.subscription5.remove();
    }

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

    onPressFindSimilar(product) {
        if (!product) {
            return;
        }
        ReactNative.NativeModules.YH_CommonHelper.jumpFindSimilar(product);
    }

    onPressDislike(product, similarIndex) {
        this.props.actions.setSimilarIndex(-1);
        let product_skn = product && product.get('product_skn', 0);
        if (!product_skn) {
            return;
        }
        this.props.actions.fetchDislike(similarIndex, product_skn);
    }

    onPressProductListProduct(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);
    }
    onRefresh() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.fetchFloor();
        });
    }

    onClickRecommendProduct() {
        this.props.actions.selectedRecommendProduct();
    }

    onEndReached() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.fetchHomeFavoriteList();
        });
    }

    jumpWithUrl(url) {
        if (!url) {
            return;
        }

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

    onPressAnnounceItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressAppIconItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressHotBrandItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressSlideItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressSlideItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressPicItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressImageItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressToProductDetail(data, index){
        let productSkn = data && data.get('product_skn', 0);
        if (!productSkn) {
          return;
        }

        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${productSkn}"}}`;
        this.jumpWithUrl(url);
    }

    onPressRecommendSlideItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressRecommendListItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressCustomBrandItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressTrendArticleItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressTrendRecommendItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressTrendTopicItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressShopRecommendItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressShopFavorite(shopId, index) {
        this.props.actions.favoriteOperation(shopId);
    }

    onPressBrandItem(url, index) {
        this.jumpWithUrl(url);
    }

    onPressVipBannerItem(url, index) {
        this.jumpWithUrl(url);
    }


    onPressVipProduct(productId,productSkn) {
        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${productSkn}"}}`;
        this.jumpWithUrl(url);
    }

    onPressTitleMore(url, index) {
        this.jumpWithUrl(url);
    }

    onClickSort(index) {
        this.props.actions.selecLifeStyleProductIndex(index);
    }

    onPressReloadData() {
        this.props.actions.fetchFloor();
    }

    render() {
        let {app, home} = this.props;
        return (
            <View style={styles.container}>
                <Home
                    ref={(c) => {
                        this.home = c;
                    }}
                    data={home}
                    onRefresh={this.onRefresh}
                    onEndReached={this.onEndReached}
                    channel={app.channel}
                    onPressAnnounceItem={this.onPressAnnounceItem}
                    onPressSlideItem={this.onPressSlideItem}
                    onPressPicItem={this.onPressPicItem}
                    onPressImageItem={this.onPressImageItem}
                    onPressRecommendSlideItem={this.onPressRecommendSlideItem}
                    onPressRecommendListItem={this.onPressRecommendListItem}
                    onPressCustomBrandItem={this.onPressCustomBrandItem}
                    onPressTrendArticleItem={this.onPressTrendArticleItem}
                    onPressTrendRecommendItem={this.onPressTrendRecommendItem}
                    onPressTrendTopicItem={this.onPressTrendTopicItem}
                    onPressBrandItem={this.onPressBrandItem}
                    onPressVipBannerItem={this.onPressVipBannerItem}
                    onPressVipProduct={this.onPressVipProduct}
                    onPressAppIconItem={this.onPressAppIconItem}
                    onPressHotBrandItem={this.onPressHotBrandItem}
                    onPressShopRecommendItem={this.onPressShopRecommendItem}
                    onPressShopFavorite={this.onPressShopFavorite}
                    onPressTitleMore={this.onPressTitleMore}
                    onClickSort={this.onClickSort}
                    onLongPressProduct={this.onLongPressProduct}
                    onPressFindSimilar={this.onPressFindSimilar}
                    onPressDislike={this.onPressDislike}
                    onPressProductListProduct={this.onPressProductListProduct}
                    onClickRecommendProduct={this.onClickRecommendProduct}
                    onPressToProductDetail={this.onPressToProductDetail}
                    onPressReloadData={this.onPressReloadData}
                />
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');

let styles = StyleSheet.create({
    container: {
        width,
        height: height - 64 - 49,
    },

});

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