OutletContainer.js 5.82 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 outletActions from '../reducers/outlet/outletActions';
import Outlet from '../components/outlet/Outlet';

const actions = [
    outletActions,
];

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 OutletContainer extends Component {
    constructor(props) {
        super(props);
        this._getOutletHomeResource = this._getOutletHomeResource.bind(this);
        this._onPressProduct = this._onPressProduct.bind(this);
        this._onPressProductListProduct = this._onPressProductListProduct.bind(this);
        this._onPressFilter = this._onPressFilter.bind(this);
        this._getOutletActivityList = this._getOutletActivityList.bind(this);
        this._onEndReached = this._onEndReached.bind(this);
        this._setActivityFliter = this._setActivityFliter.bind(this);
        this._showToast = this._showToast.bind(this);
        this._hideToastMessage = this._hideToastMessage.bind(this);
        this._onSelectChannel = this._onSelectChannel.bind(this);
        this._onChangeTab = this._onChangeTab.bind(this);

        this.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                this._onSelectChannel(reminder.channel);
            }
        );
    }

    componentDidMount() {
        this.props.actions.getCategory();
    }

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

    _onChangeTab(sort_name){
        let params = {
              TAB_ID: sort_name,
        }
        ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_OUTLET_TAB_C', params);
    }
    _onSelectChannel(channel){
        this.props.actions.setChannel(channel);
    }

    _onEndReached(content_code,categoryNavigationItem) {
        let list = categoryNavigationItem?categoryNavigationItem.list:null;
        if (categoryNavigationItem) {
            this.props.actions.getProductList(content_code,categoryNavigationItem);
        }
    }

    _getOutletHomeResource(content_code,ptr){
        this.props.actions.getOutletHomeResource(content_code,ptr);
    }

    _getOutletActivityList(content_code,yh_channel,type,tabLabel){
        this.props.actions.getOutletActivityList(content_code,yh_channel,type);
        let params = {
              TAB_ID: tabLabel,
        }
        ReactNative.NativeModules.YH_CommonHelper.logEvent('kAnalyticsYB_OUTLET_HOME_PAGE_L', params);
    }

    _setActivityFliter(content_code,activityMore) {
        this.props.actions.setActivityFliter(content_code,activityMore);
    }

    _onPressProduct(url,tabLabel,rowId=0,sectionId=0,templateName,template_id){
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
        if (templateName) {
            let params = {
                  TAB_ID: tabLabel,
                  F_ID: template_id,
                  F_NAME: templateName,
                  F_URL: url,
                  F_INDEX: parseInt(sectionId),
                  I_INDEX: parseInt(rowId)+1,
            }
            ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_OUTLET_FLR_C', params);
        }
    }

    _onPressProductListProduct(product,rowId=0,tabLabel,initialPage) {
      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);

      let productId = product && product.get('product_id', 0);
      let params = {
            TAB_ID: tabLabel,
            SORT_TYPE: initialPage,
            PRD_ID: productId,
            PRD_NUM: parseInt(rowId)+1,
      }
      ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_OUTLET_GOODS_LIST_C', params);
    }

    _showToast(value){
        this.props.actions.showToastMessage(value);
    }

    _hideToastMessage(){
        this.props.actions.hideToastMessage();
    }

    _onPressFilter(content_code,value,categoryNavigationItem){
        this.props.actions.onPressFilter(content_code,value);
        let list = categoryNavigationItem.list;
        if (!list) {
            this.props.actions.getProductList(content_code,categoryNavigationItem);
        }
    }

    render() {

        let {
            categoryList,
            isShowToast,
            toastMessage,
        } = this.props.outlet;

        return (
            <Outlet
                categoryList = {categoryList}
                isShowToast = {isShowToast}
                toastMessage = {toastMessage}
                getOutletHomeResource = {this._getOutletHomeResource}
                setActivityFliter = {this._setActivityFliter}
                getOutletActivityList = {this._getOutletActivityList}
                onPressProduct = {this._onPressProduct}
                onPressProductListProduct = {this._onPressProductListProduct}
                onPressFilter = {this._onPressFilter}
                onEndReached = {this._onEndReached}
                showToast = {this._showToast}
                hideToastMessage = {this._hideToastMessage}
                onChangeTab = {this._onChangeTab}
            />
        );
    }
}

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

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