HomeContainer.js 4.38 KB
'use strict';

import React from 'react';
import ReactNative 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 * as messageActions from '../reducers/message/messageActions';
import CONFIG from '../constants/config';

const {
    Component,
} = React;

const {
    StatusBar,
    ScrollView,
    View,
    StyleSheet,
    Dimensions,
    Platform,
} = ReactNative;

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

/**
 *  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.refoundStats,
                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() {

        this.props.actions.overview(this.props.home.shopId);
        this.props.actions.brandRank(this.props.home.shopId);
        this.props.actions.requestUnreadMsgNum(this.props.home.shopId, 'N');

        // this.props.actions.overview(1200);
        // this.props.actions.brandRank(1200);
    }

    _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: '个名次',
                hasTrendData: this.props.home.overview.rankHasTrend,
            }
        ];

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

        return (
            <View style={styles.container}>
                <StatusBar
                    hidden={false}
                    barStyle={'light-content'}
                />
                <Home
                    section1={section1}
                    section2={section2}
                    section3={this.category}
                    onPressCategory={this._onPressCategory}
                />
            </View>
        );
    }
}

let {width, height} = Dimensions.get('window');
let navbarHeight = (Platform.OS === 'android') ? 50 : 64;

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

});

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