Analysis.js 4.79 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Suspense } from 'react';
2
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
3
import { Row, Col, Icon, Menu, Dropdown } from 'antd';
4
import GridContent from '@/components/PageHeaderWrapper/GridContent';
5
import { getTimeDistance } from '@/utils/utils';
6
import styles from './Analysis.less';
陈帅's avatar
陈帅 committed
7
import PageLoading from '@/components/PageLoading';
8

陈帅's avatar
陈帅 committed
9 10 11 12 13
const IntroduceRow = React.lazy(() => import('./IntroduceRow'));
const SalesCard = React.lazy(() => import('./SalesCard'));
const TopSearch = React.lazy(() => import('./TopSearch'));
const ProportionSales = React.lazy(() => import('./ProportionSales'));
const OfflineData = React.lazy(() => import('./OfflineData'));
14

Andreas Cederström's avatar
Andreas Cederström committed
15 16 17
@connect(({ chart, loading }) => ({
  chart,
  loading: loading.effects['chart/fetch'],
18
}))
ddcat1115's avatar
ddcat1115 committed
19
class Analysis extends Component {
Anderson's avatar
Anderson committed
20 21 22 23 24 25
  state = {
    salesType: 'all',
    currentTabKey: '',
    rangePickerValue: getTimeDistance('year'),
  };

26
  componentDidMount() {
27
    const { dispatch } = this.props;
28 29 30 31
    this.reqRef = requestAnimationFrame(() => {
      dispatch({
        type: 'chart/fetch',
      });
32
    });
33 34 35
  }

  componentWillUnmount() {
36
    cancelAnimationFrame(this.reqRef);
37 38
  }

jim's avatar
jim committed
39
  handleChangeSalesType = e => {
40 41 42
    this.setState({
      salesType: e.target.value,
    });
niko's avatar
niko committed
43
  };
44

jim's avatar
jim committed
45
  handleTabChange = key => {
46 47 48
    this.setState({
      currentTabKey: key,
    });
niko's avatar
niko committed
49
  };
50

jim's avatar
jim committed
51
  handleRangePickerChange = rangePickerValue => {
52
    const { dispatch } = this.props;
53 54 55
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
56

57
    dispatch({
nikogu's avatar
nikogu committed
58 59
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
60
  };
61

jim's avatar
jim committed
62
  selectDate = type => {
63
    const { dispatch } = this.props;
64 65 66 67
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

68
    dispatch({
69 70
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
71
  };
72

陈帅's avatar
陈帅 committed
73
  isActive = type => {
afc163's avatar
afc163 committed
74 75 76
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
愚道's avatar
愚道 committed
77
      return '';
afc163's avatar
afc163 committed
78
    }
niko's avatar
niko committed
79 80 81 82
    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
afc163's avatar
afc163 committed
83 84
      return styles.currentDate;
    }
愚道's avatar
愚道 committed
85
    return '';
陈帅's avatar
陈帅 committed
86
  };
afc163's avatar
afc163 committed
87

88
  render() {
afc163's avatar
afc163 committed
89 90
    const { rangePickerValue, salesType, currentTabKey } = this.state;
    const { chart, loading } = this.props;
91 92
    const {
      visitData,
afc163's avatar
afc163 committed
93
      visitData2,
94 95 96 97 98 99 100
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
afc163's avatar
afc163 committed
101
    } = chart;
102 103 104 105 106 107
    let salesPieData;
    if (salesType === 'all') {
      salesPieData = salesTypeData;
    } else {
      salesPieData = salesType === 'online' ? salesTypeDataOnline : salesTypeDataOffline;
    }
afc163's avatar
afc163 committed
108 109 110 111 112 113 114
    const menu = (
      <Menu>
        <Menu.Item>操作一</Menu.Item>
        <Menu.Item>操作二</Menu.Item>
      </Menu>
    );

陈帅's avatar
陈帅 committed
115
    const dropdownGroup = (
116
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
117 118 119
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
120 121 122
      </span>
    );

nikogu's avatar
nikogu committed
123 124
    const activeKey = currentTabKey || (offlineData[0] && offlineData[0].name);

125
    return (
jim's avatar
jim committed
126
      <GridContent>
陈帅's avatar
陈帅 committed
127 128 129 130 131 132 133 134 135 136 137 138 139
        <Suspense fallback={<PageLoading />}>
          <IntroduceRow loading={loading} visitData={visitData} />
        </Suspense>
        <Suspense fallback={null}>
          <SalesCard
            rangePickerValue={rangePickerValue}
            salesData={salesData}
            isActive={this.isActive}
            handleRangePickerChange={this.handleRangePickerChange}
            loading={loading}
            selectDate={this.selectDate}
          />
        </Suspense>
140
        <div className={styles.twoColLayout}>
陈帅's avatar
陈帅 committed
141
          <Row gutter={24} type="flex">
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            <Col xl={12} lg={24} md={24} sm={24} xs={24}>
              <Suspense fallback={null}>
                <TopSearch
                  loading={loading}
                  visitData2={visitData2}
                  selectDate={this.selectDate}
                  searchData={searchData}
                  dropdownGroup={dropdownGroup}
                />
              </Suspense>
            </Col>
            <Col xl={12} lg={24} md={24} sm={24} xs={24}>
              <Suspense fallback={null}>
                <ProportionSales
                  dropdownGroup={dropdownGroup}
                  salesType={salesType}
                  loading={loading}
                  salesPieData={salesPieData}
                  handleChangeSalesType={this.handleChangeSalesType}
                />
              </Suspense>
            </Col>
          </Row>
        </div>
陈帅's avatar
陈帅 committed
166 167 168 169 170 171 172 173 174
        <Suspense fallback={null}>
          <OfflineData
            activeKey={activeKey}
            loading={loading}
            offlineData={offlineData}
            offlineChartData={offlineChartData}
            handleTabChange={this.handleTabChange}
          />
        </Suspense>
jim's avatar
jim committed
175
      </GridContent>
176 177
    );
  }
afc163's avatar
afc163 committed
178
}
ddcat1115's avatar
ddcat1115 committed
179

陈帅's avatar
陈帅 committed
180
export default Analysis;