HomeContainer.js 6.86 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.onPressVipBuy = this.onPressVipBuy.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.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                this.props.actions.setChannel(reminder.channel);
                this.home && this.home.trigggePullToRefresh();
            }
        );
    }

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

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

    onRefresh() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.fetchFloor();
        });
    }

    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(data, index=0) {
        if(data && data.url){
            this.jumpWithUrl(data.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) {
        console.log(shopId)
    }

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

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

    onPressVipBuy() {

    }

    onPressVipProduct(productId,productName) {
        console.log('productId=' + productId + " productName=" + productName);
    }

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

    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}
                    onPressVipBuy={this.onPressVipBuy}
                    onPressVipProduct={this.onPressVipProduct}
                    onPressAppIconItem={this.onPressAppIconItem}
                    onPressHotBrandItem={this.onPressHotBrandItem}
                    onPressShopRecommendItem={this.onPressShopRecommendItem}
                    onPressShopFavorite={this.onPressShopFavorite}
                    onPressTitleMore={this.onPressTitleMore}
                />
            </View>
        );
    }
}

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

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

});

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