SaleStatisticsContainer.js 4.51 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 SaleStatistics from '../components/SaleStatistics';

import * as saleStatisticsActions from '../reducers/saleStatistics/saleStatisticsActions';
import moment from 'moment';
import Immutable, {List, Record} from 'immutable';
import config from '../constants/config';

const {
    Component,
} = React;

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

const actions = [
    saleStatisticsActions
];

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 SalestisticsContainer extends Component {

    constructor(props) {
        super(props);

        this.onChangeDate = this.onChangeDate.bind(this);
    }

    componentDidMount() {
        let params = {
            type: config.dateFilterKey.date,
            reqTime: moment().subtract(1,'days').format('YYYYMMDD'),
            shopId: this.props.home.get('shopId'),
        }
        this.props.actions.saleStats(params);
    }

    componentWillUnmount() {
        this.props.actions.saleStatsClean();
    }

    onChangeDate(selected) {

        let params = {};
        switch (selected.selectMode) {
            case 'day':

                let wrapper = moment(selected.day);
                let dateStr = wrapper.format('YYYYMMDD');
                params = {
                    type: config.dateFilterKey.date,
                    reqTime: dateStr,
                    shopId: this.props.home.get('shopId'),
                }

                break;
            case 'week':
                params = {
                    type: config.dateFilterKey.week,
                    beginTime: moment(selected.from).format('YYYYMMDD'),
                    endTime: moment(selected.to).format('YYYYMMDD'),
                    shopId: this.props.home.get('shopId'),
                }
                break;
            case 'month':

                let monthStr = '';
                if (selected.month>9) {
                    monthStr = `${selected.year}`+`${selected.month}`
                } else {
                    monthStr = `${selected.year}`+'0'+`${selected.month}`
                }
                params = {
                    type: config.dateFilterKey.month,
                    reqTime: monthStr,
                    shopId: this.props.home.get('shopId'),
                }
                break;
            default:

        }

        this.props.actions.saleStats(params);
    }

	render() {

        let section1 = [
            {
                top: '有效订单的商品金额(元)',
                bottom: `${this.props.saleStats.goodsAmount}`,
            },
            {
                top: '环比',
                arrowUp: this.props.saleStats.amountRise,
                bottom: `${this.props.saleStats.amountRisePercent}%`,
                hasTrendData: this.props.saleStats.amountHasTrendData,
            }
        ];

        let section2 = [
            {
                top: '有效订单的商品件数',
                bottom: `${this.props.saleStats.goodsCount}`,
            },
            {
                top: '环比',
                arrowUp: this.props.saleStats.countRise,
                bottom: `${this.props.saleStats.countRisePercent}%`,
                hasTrendData: this.props.saleStats.countHasTrendData,
            }
        ];

        return (
            <View style={styles.container}>
                <SaleStatistics
                    section1={section1}
                    section2={section2}
                    sevenDays={this.props.saleStats.sevenDays.toJS()}
                    trendInSevenDays={this.props.saleStats.trendInSevenDays.toJS()}
                    onChangeDate={this.onChangeDate}
                    isFetching={this.props.saleStats.isFetching}
                />
            </View>
        );
    }
}

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

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

});

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