Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { Map } from 'immutable';
  5. import { tableActions } from '~/redux/actions/table';
  6. import tableService from '~/services/tableService';
  7.  
  8. import { Steps } from './constants';
  9. import StepsBlock from './components/StepsBlock';
  10. import StatisticsDataForm from './components/StatisticsDataForm';
  11. import TableFilterGrid from './components/TableFilterGrid';
  12. import SelectedDataTable from './components/SelectedDataTable';
  13.  
  14. import './PortalPageContainer';
  15.  
  16. class PortalPageContainer extends Component {
  17.   state = {
  18.     step: Steps.StatisticsDataForm,
  19.     isChangingFilters: false,
  20.     filterBlocks: [],
  21.   }
  22.  
  23.   setStatisticsData = (statisticsData) => {
  24.     const { dispatch } = this.props;
  25.     dispatch(tableActions.setStatisticsData(statisticsData));
  26.   }
  27.  
  28.   setFilterValues = (filter) => {
  29.     const { dispatch } = this.props;
  30.     dispatch(tableActions.setFilterValues(filter));
  31.   }
  32.  
  33.   nextStep = () => {
  34.     const { step } = this.state;
  35.     switch (step) {
  36.       case Steps.STATISTICS_DATA: {
  37.         const { tableInfo } = this.props;
  38.         const statisticsData = tableInfo.get('statisticsData');
  39.         const sphereParameter = statisticsData.get('sphereParameter');
  40.         const subjects = statisticsData.get('subjects').toArray();
  41.  
  42.         tableService.getFilters({ subjects, sphereParameter }).then((filters) => {
  43.           const filterBlocks = [];
  44.           filters.forEach((filter) => {
  45.             filterBlocks.push({
  46.               id: filter.id,
  47.               name: filter.name,
  48.               values: filter.elements.map(element => ({ value: element.id, text: element.name })),
  49.             });
  50.           });
  51.  
  52.           this.setState({
  53.             step: step + 1,
  54.             filterBlocks,
  55.           });
  56.         });
  57.         break;
  58.       }
  59.  
  60.       case Steps.FILTERS: {
  61.         const { tableInfo } = this.props;
  62.         const { filterBlocks } = this.state;
  63.         const selectedFilters = tableInfo.get('selectedFilters');
  64.         const allFiltersSelected = filterBlocks.length === selectedFilters.size;
  65.         const eachFilterHasValues = allFiltersSelected
  66.           && selectedFilters.every(filter => filter.values.length !== 0);
  67.         if (!eachFilterHasValues) {
  68.           break;
  69.         }
  70.         const { dispatch } = this.props;
  71.         const statisticsData = tableInfo.get('statisticsData');
  72.         const tableOptions = {
  73.           statisticsData: {
  74.             statisticType: statisticsData.get('statisticType'),
  75.             subjects: statisticsData.get('subjects').toArray(),
  76.             sphere: statisticsData.get('sphere'),
  77.             sphereParameter: statisticsData.get('sphereParameter'),
  78.           },
  79.           selectedFilters: selectedFilters.toArray(),
  80.         };
  81.         dispatch(tableActions.clearFilterValues());
  82.         dispatch(tableActions.getTableData(tableOptions));
  83.  
  84.         this.setState({
  85.           step: step + 1,
  86.           isChangingFilters: true,
  87.         });
  88.         break;
  89.       }
  90.  
  91.       default: {
  92.         this.setState({
  93.           step: step + 1,
  94.         });
  95.       }
  96.     }
  97.   }
  98.  
  99.   previousStep = () => {
  100.     const { step } = this.state;
  101.     const { dispatch } = this.props;
  102.     switch (step) {
  103.       case Steps.FILTERS: {
  104.         this.setState({
  105.           step: step - 1,
  106.           isChangingFilters: false,
  107.         });
  108.         break;
  109.       }
  110.       case Steps.TABLE: {
  111.         dispatch(tableActions.clearGrid());
  112.         this.setState({
  113.           step: step - 1,
  114.           isChangingFilters: false,
  115.         });
  116.         break;
  117.       }
  118.  
  119.       default: {
  120.         this.setState({
  121.           step: step - 1,
  122.         });
  123.       }
  124.     }
  125.   }
  126.  
  127.   render() {
  128.     const { step, isChangingFilters, filterBlocks } = this.state;
  129.     const { tableInfo } = this.props;
  130.     const statisticsData = tableInfo.get('statisticsData');
  131.     const grid = tableInfo.get('grid');
  132.     const getTableDataSuccess = tableInfo.get('getTableDataSuccess');
  133.     const selectedFilters = tableInfo.get('selectedFilters');
  134.  
  135.     switch (step) {
  136.       case Steps.STATISTICS_DATA:
  137.         return (
  138.           <div className="PortalPageContainer">
  139.             <StepsBlock step={step} />
  140.             <StatisticsDataForm
  141.               statisticsData={statisticsData}
  142.               setStatisticsData={this.setStatisticsData}
  143.               nextStep={this.nextStep}
  144.             />
  145.           </div>
  146.         );
  147.       case Steps.FILTERS:
  148.         return (
  149.           <div className="PortalPageContainer">
  150.             <StepsBlock step={step} />
  151.             <TableFilterGrid
  152.               selectedFilters={selectedFilters.toArray()}
  153.               filterBlocks={filterBlocks}
  154.               isChangingFilters={isChangingFilters}
  155.               setFilterValues={this.setFilterValues}
  156.               previousStep={this.previousStep}
  157.               nextStep={this.nextStep}
  158.             />
  159.           </div>
  160.         );
  161.  
  162.       case Steps.TABLE:
  163.         return (
  164.           <div className="PortalPageContainer">
  165.             <StepsBlock step={step} getTableDataSuccess={getTableDataSuccess} />
  166.             <SelectedDataTable
  167.               tableData={grid}
  168.               previousStep={this.previousStep}
  169.             />
  170.           </div>
  171.         );
  172.         // skip default
  173.     }
  174.   }
  175. }
  176.  
  177. PortalPageContainer.propTypes = {
  178.   dispatch: PropTypes.func.isRequired,
  179.   tableInfo: PropTypes.instanceOf(Map).isRequired,
  180. };
  181.  
  182. function mapStateToProps(state) {
  183.   return {
  184.     tableInfo: state.tableInfo,
  185.   };
  186. }
  187.  
  188.  
  189. export default connect(mapStateToProps)(PortalPageContainer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement