index.tsx 5.92 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';
陈帅's avatar
陈帅 committed
4
import { RangePickerValue } from 'antd/lib/date-picker/interface';
5 6 7
import { getTimeDistance } from './utils/utils';
import styles from './style.less';
import PageLoading from './components/PageLoading';
陈帅's avatar
陈帅 committed
8 9 10
import { Dispatch } from 'redux';
import { IAnalysisData } from './data.d';
import { RadioChangeEvent } from 'antd/lib/radio';
陈帅's avatar
陈帅 committed
11
import { GridContent } from '@ant-design/pro-layout';
12

13 14 15 16 17
const IntroduceRow = React.lazy(() => import('./components/IntroduceRow'));
const SalesCard = React.lazy(() => import('./components/SalesCard'));
const TopSearch = React.lazy(() => import('./components/TopSearch'));
const ProportionSales = React.lazy(() => import('./components/ProportionSales'));
const OfflineData = React.lazy(() => import('./components/OfflineData'));
18

陈帅's avatar
陈帅 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
interface BLOCK_NAME_CAMEL_CASEProps {
  BLOCK_NAME_CAMEL_CASE: IAnalysisData;
  dispatch: Dispatch;
  loading: boolean;
}

interface BLOCK_NAME_CAMEL_CASEState {
  salesType: 'all' | 'online' | 'stores';
  currentTabKey: string;
  rangePickerValue: RangePickerValue;
}

@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: any;
    loading: {
      effects: { [key: string]: boolean };
    };
  }) => ({
    BLOCK_NAME_CAMEL_CASE,
    loading: loading.effects['BLOCK_NAME_CAMEL_CASE/fetch'],
  })
)
class PAGE_NAME_UPPER_CAMEL_CASE extends Component<
  BLOCK_NAME_CAMEL_CASEProps,
  BLOCK_NAME_CAMEL_CASEState
> {
  state: BLOCK_NAME_CAMEL_CASEState = {
Anderson's avatar
Anderson committed
50 51 52 53
    salesType: 'all',
    currentTabKey: '',
    rangePickerValue: getTimeDistance('year'),
  };
陈帅's avatar
陈帅 committed
54 55
  reqRef!: number;
  timeoutId!: number;
56
  componentDidMount() {
57
    const { dispatch } = this.props;
58 59
    this.reqRef = requestAnimationFrame(() => {
      dispatch({
60
        type: 'BLOCK_NAME_CAMEL_CASE/fetch',
61
      });
62
    });
63 64 65 66 67
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
68
      type: 'BLOCK_NAME_CAMEL_CASE/clear',
69
    });
70
    cancelAnimationFrame(this.reqRef);
afc163's avatar
afc163 committed
71
    clearTimeout(this.timeoutId);
72 73
  }

陈帅's avatar
陈帅 committed
74
  handleChangeSalesType = (e: RadioChangeEvent) => {
75 76 77
    this.setState({
      salesType: e.target.value,
    });
niko's avatar
niko committed
78
  };
79

陈帅's avatar
陈帅 committed
80
  handleTabChange = (key: string) => {
81 82 83
    this.setState({
      currentTabKey: key,
    });
niko's avatar
niko committed
84
  };
85

陈帅's avatar
陈帅 committed
86
  handleRangePickerChange = (rangePickerValue: RangePickerValue) => {
87
    const { dispatch } = this.props;
88 89 90
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
91

92
    dispatch({
93
      type: 'BLOCK_NAME_CAMEL_CASE/fetchSalesData',
nikogu's avatar
nikogu committed
94
    });
niko's avatar
niko committed
95
  };
96

陈帅's avatar
陈帅 committed
97
  selectDate = (type: 'today' | 'week' | 'month' | 'year') => {
98
    const { dispatch } = this.props;
99 100 101 102
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

103
    dispatch({
104
      type: 'BLOCK_NAME_CAMEL_CASE/fetchSalesData',
105
    });
niko's avatar
niko committed
106
  };
107

陈帅's avatar
陈帅 committed
108
  isActive = (type: 'today' | 'week' | 'month' | 'year') => {
afc163's avatar
afc163 committed
109 110 111
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
愚道's avatar
愚道 committed
112
      return '';
afc163's avatar
afc163 committed
113
    }
niko's avatar
niko committed
114 115 116 117
    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
afc163's avatar
afc163 committed
118 119
      return styles.currentDate;
    }
愚道's avatar
愚道 committed
120
    return '';
陈帅's avatar
陈帅 committed
121
  };
afc163's avatar
afc163 committed
122

123
  render() {
陈帅's avatar
陈帅 committed
124
    const { rangePickerValue, salesType, currentTabKey } = this.state;
125
    const { BLOCK_NAME_CAMEL_CASE, loading } = this.props;
126 127
    const {
      visitData,
afc163's avatar
afc163 committed
128
      visitData2,
129 130 131 132 133 134 135
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
136
    } = BLOCK_NAME_CAMEL_CASE;
137 138 139 140 141 142
    let salesPieData;
    if (salesType === 'all') {
      salesPieData = salesTypeData;
    } else {
      salesPieData = salesType === 'online' ? salesTypeDataOnline : salesTypeDataOffline;
    }
afc163's avatar
afc163 committed
143 144 145 146 147 148 149
    const menu = (
      <Menu>
        <Menu.Item>操作一</Menu.Item>
        <Menu.Item>操作二</Menu.Item>
      </Menu>
    );

陈帅's avatar
陈帅 committed
150
    const dropdownGroup = (
151
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
152 153 154
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
155 156 157
      </span>
    );

nikogu's avatar
nikogu committed
158
    const activeKey = currentTabKey || (offlineData[0] && offlineData[0].name);
159
    return (
陈帅's avatar
陈帅 committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      <GridContent>
        <React.Fragment>
          <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>
          <Row gutter={24}>
            <Col xl={12} lg={24} md={24} sm={24} xs={24}>
              <Suspense fallback={null}>
                <TopSearch
                  loading={loading}
                  visitData2={visitData2}
                  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>
          <Suspense fallback={null}>
            <OfflineData
              activeKey={activeKey}
              loading={loading}
              offlineData={offlineData}
              offlineChartData={offlineChartData}
              handleTabChange={this.handleTabChange}
            />
          </Suspense>
        </React.Fragment>
      </GridContent>
209 210
    );
  }
afc163's avatar
afc163 committed
211
}
ddcat1115's avatar
ddcat1115 committed
212

213
export default PAGE_NAME_UPPER_CAMEL_CASE;