BrowseContainer.js 3.13 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.subscription = NativeAppEventEmitter.addListener(
            'ClearHistoryEvent',
            (reminder) => {
                this.props.actions.clearHistory();
            }
        );
    }

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

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

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

    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}"}}`;
        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);
    }

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

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