BrowseContainer.js 3.76 KB
'use strict'

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

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import * as browseActions from '../reducers/browse/browseActions';

import Browse from '../components/browse/Browse';


const actions = [
    browseActions,
];

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

        this.onPressCategory = this.onPressCategory.bind(this);
        this.onPressProduct = this.onPressProduct.bind(this);
        this.onPressFindSimilar = this.onPressFindSimilar.bind(this);
        this.onPressDelete = this.onPressDelete.bind(this);
        this.onPressGuangGuang = this.onPressGuangGuang.bind(this);
        this.onEndReached = this.onEndReached.bind(this);

        this.subscription = NativeAppEventEmitter.addListener(
            'ClearHistoryEvent',
            (reminder) => {
                let curCategoryID = this.props.browse.curCategoryID;
                this.props.actions.clearHistory(curCategoryID);
            }
        );
    }

    onPressGuangGuang() {
        ReactNative.NativeModules.YH_RecorderHelper.onPressGuangGuang();
    }

    componentDidMount() {
        this.props.actions.historySortList();
        this.props.actions.historyList(0);
    }

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

    onPressCategory(data, index) {
        this.props.actions.setSelectedCategory(data.get('category_id'), index, data.get('category_name'));
    }

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

        let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${productSkn}","from_page_name":"${Platform.OS === 'ios'?'iFP_MineBrowseHistory':'aFP_MineBrowseHistory'}"}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    }

    onPressFindSimilar(data) {
        if (!data) {
            return;
        }
        let product = {
            product_name: data.get('product_name', ''),
            product_id: data.get('product_id', 0),
            product_skn: data.get('product_skn', 0),
            default_images: data.get('image', ''),
            market_price: data.get('market_price', 0),
            sales_price: data.get('sales_price', 0),
        };
        ReactNative.NativeModules.YH_CommonHelper.jumpFindSimilar(product);
        // ReactNative.NativeModules.YH_CommonHelper.setSimilarGuiderVisible(false);
    }

    onPressDelete(data, index) {
        this.props.actions.deleteOneHistory(data.get('product_skn'), index);
    }

    onEndReached() {
        this.props.actions.historyList();
    }

    render() {

        return (
            <Browse
                data={this.props.browse}
                onPressCategory={this.onPressCategory}
                onPressProduct={this.onPressProduct}
                onPressFindSimilar={this.onPressFindSimilar}
                onPressDelete={this.onPressDelete}
                onPressGuangGuang={this.onPressGuangGuang}
                onEndReached={this.onEndReached}
            />
        );
    }
}

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