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';
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
  state = {
陈帅's avatar
陈帅 committed
21
    loading: true,
Anderson's avatar
Anderson committed
22 23 24 25 26
    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
    });
陈帅's avatar
陈帅 committed
34 35 36 37 38
    setTimeout(() => {
      this.setState({
        loading: false,
      });
    }, 2000);
39 40 41 42 43 44 45
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'chart/clear',
    });
46
    cancelAnimationFrame(this.reqRef);
47 48
  }

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

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

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

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

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

78
    dispatch({
79 80
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
81
  };
82

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

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

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

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

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

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