Analysis.js 5.05 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';
陈帅's avatar
陈帅 committed
8
import { AsyncLoadBizCharts } from '@/components/Charts/AsyncLoadBizCharts';
9

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

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

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

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

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

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

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

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

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

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

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

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

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

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

131
    return (
jim's avatar
jim committed
132
      <GridContent>
陈帅's avatar
陈帅 committed
133 134 135 136 137 138 139 140 141 142 143 144 145
        <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>
146 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
        <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
172 173 174 175 176 177 178 179 180
        <Suspense fallback={null}>
          <OfflineData
            activeKey={activeKey}
            loading={loading}
            offlineData={offlineData}
            offlineChartData={offlineChartData}
            handleTabChange={this.handleTabChange}
          />
        </Suspense>
jim's avatar
jim committed
181
      </GridContent>
182 183
    );
  }
afc163's avatar
afc163 committed
184
}
ddcat1115's avatar
ddcat1115 committed
185

陈帅's avatar
陈帅 committed
186 187 188 189 190
export default props => (
  <AsyncLoadBizCharts>
    <Analysis {...props} />
  </AsyncLoadBizCharts>
);