index.tsx 5.73 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';
11

12 13 14 15 16
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'));
17

陈帅's avatar
陈帅 committed
18 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
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
49 50 51 52
    salesType: 'all',
    currentTabKey: '',
    rangePickerValue: getTimeDistance('year'),
  };
陈帅's avatar
陈帅 committed
53 54
  reqRef!: number;
  timeoutId!: number;
55
  componentDidMount() {
56
    const { dispatch } = this.props;
57 58
    this.reqRef = requestAnimationFrame(() => {
      dispatch({
59
        type: 'BLOCK_NAME_CAMEL_CASE/fetch',
60
      });
61
    });
62 63 64 65 66
  }

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

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

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

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

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

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

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

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

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

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

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

210
export default PAGE_NAME_UPPER_CAMEL_CASE;