HomeContainer.js 3.76 KB
'use strict';

import React from 'react-native';

import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';

import {Map} from 'immutable';

import Home from '../components/Home';

import * as homeActions from '../reducers/home/homeActions';
import CONFIG from '../constants/config';

let {
    Component,
    ScrollView,
    View,
    StyleSheet,
    Dimensions
} = React;

/**
 * ## Actions
 * 3 of our actions will be available as ```actions```
 */
const actions = [
    homeActions
];

/**
 *  Save that state
 */
function mapStateToProps(state) {
    return {
        ...state
    };
};

/**
 * Bind all the functions from the ```actions``` and bind them with
 * ```dispatch```
 */
function mapDispatchToProps(dispatch) {

    const creators = Map()
        .merge(...actions)
        .filter(value => typeof value === 'function')
        .toObject();

    return {
        actions: bindActionCreators(creators, dispatch),
        dispatch
    };
}

class HomeContainer extends Component {

    constructor(props) {
        super(props);
    
        this._onPressCategory = this._onPressCategory.bind(this);

        this.category = [
            {
                type: CONFIG.statsKey.saleStats,
                thumb: require('../images/xiaoshou.png'),
                title: '销售统计',
            },
            {
                type: CONFIG.statsKey.returnStats,
                thumb: require('../images/tuihuo.png'),
                title: '退货统计',
            },
            {
                type: CONFIG.statsKey.stockStats,
                thumb: require('../images/kucun.png'),
                title: '库存统计',
            },
            {
                type: CONFIG.statsKey.deliveryStats,
                thumb: require('../images/fahuo.png'),
                title: '发货统计',
            },
            {
                type: CONFIG.statsKey.requestStats,
                thumb: require('../images/qingtui.png'),
                title: '请退统计',
            },
            {
                type: CONFIG.statsKey.accountSettlement,
                thumb: require('../images/duizhang.png'),
                title: '对账结算',
            },
        ];

    }

    componentDidMount() {
        console.log('shopId'+ this.props.home.shopId);
        this.props.actions.overview(this.props.home.shopId);
    }

    _onPressCategory(id) {
        let item = this.category[id];
        this.props.actions.goToStatsPage(item.type);
    }

	render() {

        let section1 = [
            {
                top: '同品类中品牌排名',
                bottom: `${this.props.home.overview.rank}`,
            },
            {
                top: '较上周同期',
                arrowUp: this.props.home.overview.rise,
                bottom: `${this.props.home.overview.riseCount}`,
                small: '个名次',
            }
        ];

        let section2 = [
            {
                top: '今日有效订单的\n商品件数',
                bottom: `${this.props.home.overview.goodsCount}`,
            },
            {
                top: '今日有效订单的\n商品金额(元)',
                bottom: `${this.props.home.overview.goodsCount}`,
            }
        ];

        return (
            <View style={styles.container}>
                <Home 
                    section1={section1}
                    section2={section2}
                    section3={this.category}
                    onPressCategory={this._onPressCategory}
                />
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');

let styles = StyleSheet.create({
    container: {
        top: 64,
        height: height - 64 - 49,
    },
 
});

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