InterestContainer.js 5.08 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.subscription = NativeAppEventEmitter.addListener(
            'ChannelDidChangeEvent',
            (reminder) => {
                this.props.actions.resetListPageInfo();
                this.props.actions.interestList(true);
            }
        );

        this.subscription2 = NativeAppEventEmitter.addListener(
            'UserDidLoginEvent',
            (reminder) => {
                this.props.actions.resetListPageInfo();
                this.props.actions.interestList(true);
                this.props.actions.showLoginTip(false);
            }
        );

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

    componentDidMount() {
        this.props.actions.loadCachedInterestList();
        this.props.actions.interestList(true);
    }

    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) {
        this.props.actions.addFavorite(item, index);

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

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

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

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

        let params = {
            CAT_NUM: '1',
            NAV_NUM: index + 2 + '',
        };
        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
                    data={interest}
                    onInterestLike={this._onInterestLike}
                    onRefresh={this._onInterestRefresh}
                    onEndReached={this._onInterestEndReached}
                    onPressBrand={this._onPressInterestBrand}
                    onPressProduct={this._onPressInterestProduct}
                    onPressActivity={this._onPressInterestActivity}
                    onInterestLogin={this._onInterestLogin}
                />
            </View>
        );
    }
}

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

});

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