InterestContainer.js 5.73 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 interestActions from '../reducers/interest/interestActions';

import Interest from '../components/interest/Interest';


const actions = [
    interestActions,
];

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

        this._onInterestLike = this._onInterestLike.bind(this);
        this._onInterestRefresh = this._onInterestRefresh.bind(this);
        this._onInterestEndReached = this._onInterestEndReached.bind(this);
        this._onPressInterestBrand = this._onPressInterestBrand.bind(this);
        this._onPressInterestActivity = this._onPressInterestActivity.bind(this);
        this._onPressInterestProduct = this._onPressInterestProduct.bind(this);
        this._onInterestLogin = this._onInterestLogin.bind(this);
        this._interestListTipRemove = this._interestListTipRemove.bind(this);

        this.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                this.interest && this.interest.trigggePullToRefresh();
            }
        );

        this.subscription2 = NativeAppEventEmitter.addListener(
            'UserDidLoginEvent',
            (reminder) => {
                this.interest && this.interest.trigggePullToRefresh();
                this.props.actions.showLoginTip(false);
            }
        );

        this.subscription3 = NativeAppEventEmitter.addListener(
            'UserDidLogoutEvent',
            (reminder) => {
                this.interest && this.interest.trigggePullToRefresh();
                this.props.actions.showLoginTip(true);
            }
        );
    }

    componentDidMount() {
        this.props.actions.loadCachedInterestList();
        this.props.actions.shouldShowLoginTip();
    }

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

    _onInterestRefresh() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.resetListPageInfo();
            this.props.actions.interestList(true);
        });
    }

    _onInterestEndReached() {
        InteractionManager.runAfterInteractions(() => {
            this.props.actions.interestList(false);
        });
    }

    _onInterestLike(item, index, bliked = false) {
        if (!bliked) {
            this.props.actions.addFavorite(item, index);
        } else {
            NativeModules.YH_CommonHelper.jumpToBrand(item.toJS());
        }

        let params = {
            CAT_NUM: parseInt(index) + 1 + '',
            BR_ID: item.get('brand_id', ''),
        };
        // console.log('YB_CATEGORY_BRAND_TAB_FAV')
        // console.log(params)
        NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_TAB_FAV', params);
    }

    _interestListTipRemove() {
        this.props.actions.interestListTipRemove();
    }

    _onPressInterestBrand(item, index) {
        // let url = item.get('url', '');
        NativeModules.YH_CommonHelper.jumpToBrand(item.toJS());

        let params = {
            CAT_NUM: parseInt(index) + 1 + '',
            NAV_NUM: '1',
            BR_ID: item.get('brand_id', ''),
        };
        // console.log('YB_CATEGORY_BRAND_LIST_ATT')
        // console.log(params)
        NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_ATT', params);
    }

    _onPressInterestProduct(item, index, section) {
        let url = item.url ? item.url : '';
        NativeModules.YH_CommonHelper.jumpWithUrl(url);

        let params = {
            CAT_NUM: parseInt(section) + 1 + '',
            NAV_NUM: parseInt(index) + 1 + '',
        };
        // console.log('YB_CATEGORY_BRAND_LIST_ATT')
        // console.log(params)
        NativeModules.YH_CommonHelper.logEvent('YB_CATEGORY_BRAND_LIST_ATT', params);
    }

    _onPressInterestActivity(url, index) {

        NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    _onInterestLogin() {
        ReactNative.NativeModules.YH_CommonHelper.login()
        .then(data => {

        })
        .catch(error => {

        });
    }

    render() {
        let {interest} = this.props;
        return (
            <View style={styles.container}>
                <Interest
                    ref={(c) => {
                        this.interest = c;
                    }}
                    data={interest}
                    onInterestLike={this._onInterestLike}
                    onRefresh={this._onInterestRefresh}
                    onEndReached={this._onInterestEndReached}
                    onPressBrand={this._onPressInterestBrand}
                    onPressProduct={this._onPressInterestProduct}
                    onPressActivity={this._onPressInterestActivity}
                    onInterestLogin={this._onInterestLogin}
                    interestListTipRemove={this._interestListTipRemove}
                />
            </View>
        );
    }
}

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

});

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