Analysis.js 4.9 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Suspense } from 'react';
2
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
3 4
import { Row, Col, Icon, Menu, Dropdown } from 'antd';

5
import GridContent from '@/components/PageHeaderWrapper/GridContent';
6
import { getTimeDistance } from '@/utils/utils';
7 8

import styles from './Analysis.less';
陈帅's avatar
陈帅 committed
9
import PageLoading from '@/components/PageLoading';
10

陈帅's avatar
陈帅 committed
11 12 13 14 15
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'));
16

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

28
  componentDidMount() {
29
    const { dispatch } = this.props;
30 31 32 33
    this.reqRef = requestAnimationFrame(() => {
      dispatch({
        type: 'chart/fetch',
      });
34
    });
35 36 37 38 39 40 41
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'chart/clear',
    });
42
    cancelAnimationFrame(this.reqRef);
afc163's avatar
afc163 committed
43
    clearTimeout(this.timeoutId);
44 45
  }

jim's avatar
jim committed
46
  handleChangeSalesType = e => {
47 48 49
    this.setState({
      salesType: e.target.value,
    });
niko's avatar
niko committed
50
  };
51

jim's avatar
jim committed
52
  handleTabChange = key => {
53 54 55
    this.setState({
      currentTabKey: key,
    });
niko's avatar
niko committed
56
  };
57

jim's avatar
jim committed
58
  handleRangePickerChange = rangePickerValue => {
59
    const { dispatch } = this.props;
60 61 62
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
63

64
    dispatch({
nikogu's avatar
nikogu committed
65 66
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
67
  };
68

jim's avatar
jim committed
69
  selectDate = type => {
70
    const { dispatch } = this.props;
71 72 73 74
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

75
    dispatch({
76 77
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
78
  };
79

陈帅's avatar
陈帅 committed
80
  isActive = type => {
afc163's avatar
afc163 committed
81 82 83
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
愚道's avatar
愚道 committed
84
      return '';
afc163's avatar
afc163 committed
85
    }
niko's avatar
niko committed
86 87 88 89
    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
afc163's avatar
afc163 committed
90 91
      return styles.currentDate;
    }
愚道's avatar
愚道 committed
92
    return '';
陈帅's avatar
陈帅 committed
93
  };
afc163's avatar
afc163 committed
94

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

陈帅's avatar
陈帅 committed
122
    const dropdownGroup = (
123
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
124 125 126
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
127 128 129
      </span>
    );

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

132
    return (
jim's avatar
jim committed
133
      <GridContent>
陈帅's avatar
陈帅 committed
134 135 136 137 138 139 140 141 142 143 144 145 146
        <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>
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        <div className={styles.twoColLayout}>
          <Row gutter={24}>
            <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
173 174 175 176 177 178 179 180 181
        <Suspense fallback={null}>
          <OfflineData
            activeKey={activeKey}
            loading={loading}
            offlineData={offlineData}
            offlineChartData={offlineChartData}
            handleTabChange={this.handleTabChange}
          />
        </Suspense>
jim's avatar
jim committed
182
      </GridContent>
183 184
    );
  }
afc163's avatar
afc163 committed
185
}
ddcat1115's avatar
ddcat1115 committed
186

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