ProductContainer.js 5.51 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 productActions from '../reducers/product/productActions';

import Product from '../components/product/Product';


const actions = [
    productActions,
];

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 ProductContainer 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.onPressTab = this.onPressTab.bind(this);
        this.setEditedIndex = this.setEditedIndex.bind(this);
        this.onPressGuangGuang = this.onPressGuangGuang.bind(this);

        this.subscription = NativeAppEventEmitter.addListener(
            'EditProductEvent',
            (reminder) => {
                let {edit} = reminder
                this.props.actions.setEditing(edit);
                if (!edit) {
                    this.props.actions.setEditedIndex(-1);
                }
                let {currentTab} = this.props.product;
                if (currentTab == 'global' && edit) {
                    NativeModules.YH_CommonHelper.logEvent('YB_GLOBAL_MY_FAVPRD_EDIT', {});
                }
            }
        );
    }

    componentDidMount() {
        this.props.actions.commonList();
        this.props.actions.globalList();
    }

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

    onPressCategory(data, index) {
        let {editing} = this.props.product;
        if (editing) {
            this.props.actions.setEditedIndex(-1);
        }
        this.props.actions.setSelectedCategory(data.get('category_id'), index, data.get('category_name'));
    }

    onPressProduct(data) {
        let {editing} = this.props.product;
        if (editing) {
            this.props.actions.setEditedIndex(-1);
            return;
        }
        let productSkn = data.get('product_skn', 0);
        if (!productSkn) {
            return;
        }
        let {currentTab} = this.props.product;
        if (currentTab == 'common') {
            let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${productSkn}","from_page_name":"${Platform.OS === 'ios'?'iFP_MineCollectionGoods':'aFP_MineCollectionGoods'}"}}`;
            ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
        }
        if (currentTab == 'global') {
            let url = `http://m.yohobuy.com?openby:yohobuy={"action":"go.globalpurchase","params":{"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);
    }

    onPressDelete(data, index) {
        let {currentTab} = this.props.product;
        if (currentTab == 'common') {
            this.props.actions.deleteCommon(data.get('product_id'), index);
        }
        if (currentTab == 'global') {
            this.props.actions.deleteGlobal(data.get('product_skn'), index);
        }
    }

    onPressTab(tab) {
        this.props.actions.setProductTab(tab);
        this.props.actions.setEditedIndex(-1);
        if (tab == 'common') {
            let {commonProduct} = this.props.product;
            let show = commonProduct.get('productList').size > 0;
            ReactNative.NativeModules.YH_RecorderHelper.setRightEditButtonVisiblity(show);
        }
        if (tab == 'global') {
            let {globalProduct} = this.props.product;
            let show = globalProduct.get('productList').size > 0;
            ReactNative.NativeModules.YH_RecorderHelper.setRightEditButtonVisiblity(show);
        }
    }

    setEditedIndex(rowID) {
        this.props.actions.setEditedIndex(rowID);
    }

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

        return (
            <Product
                data={this.props.product}
                onPressCategory={this.onPressCategory}
                onPressProduct={this.onPressProduct}
                onPressFindSimilar={this.onPressFindSimilar}
                onPressDelete={this.onPressDelete}
                onPressTab={this.onPressTab}
                setEditedIndex={this.setEditedIndex}
                onPressGuangGuang={this.onPressGuangGuang}
            />
        );
    }
}

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