All files / src/components DatatableInitializer.js

92.31% Statements 24/26
83.33% Branches 5/6
88.89% Functions 8/9
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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                                                                                                        5x   5x               5x               5x   5x 5x   5x                                     2x 2x 2x 2x 2x   2x 1x                   2x                   2x       5x                           2x 3x 3x 3x   3x       2x                                            
import React, { Component } from "react";
import { connect } from "react-redux";
import { throttle } from "lodash";
import equal from "fast-deep-equal";
import elementResizeEvent from "element-resize-event";
import { MuiThemeProvider } from "@material-ui/core/styles";
import { MuiPickersUtilsProvider } from "material-ui-pickers";
import MomentUtils from "@date-io/moment";
import { moment, locale } from "../moment.config";
import { mainTheme } from "./MuiTheme";
import {
  initializeOptionsPropType,
  initializeCustomComponentsPropType,
  updateComponentSizePropType,
  optionsPropType,
  forceRerenderPropType,
  actionsPropType,
  strippedPropType,
  refreshRowsPropType,
  CustomTableBodyRowPropType,
  CustomTableBodyCellPropType,
  CustomTableHeaderRowPropType,
  CustomTableHeaderCellPropType,
  customDataTypesPropType,
  customPropsPropType,
  dtKeyPropType
} from "../proptypes";
import DatatableContainer from "./DatatableContainer";
import {
  initializeOptions as initializeOptionsAction,
  updateComponentSize as updateComponentSizeAction
} from "../redux/actions/datatableActions";
import initializeCustomComponentsAction from "../redux/actions/customComponentsActions";
 
export class DatatableInitializer extends Component {
  componentDidMount() {
    const {
      optionsInit,
      dtKey,
      forceRerender,
      actions,
      refreshRows,
      stripped,
      customProps,
      CustomTableBodyCell,
      CustomTableBodyRow,
      CustomTableHeaderCell,
      CustomTableHeaderRow,
      customDataTypes,
      updateComponentSize,
      initializeOptions,
      initializeCustomComponents
    } = this.props;
 
    initializeOptions({
      optionsInit,
      dtKey,
      forceRerender,
      actions,
      refreshRows,
      stripped
    });
    initializeCustomComponents({
      customProps,
      CustomTableBodyCell,
      CustomTableBodyRow,
      CustomTableHeaderCell,
      CustomTableHeaderRow,
      customDataTypes
    });
    updateComponentSize();
 
    const callBack = () => throttle(() => updateComponentSize(), 100);
    const element = document.getElementById("o2xp").parentElement;
 
    elementResizeEvent(element, callBack());
  }
 
  shouldComponentUpdate(nextProps) {
    const {
      customProps,
      CustomTableBodyCell,
      CustomTableBodyRow,
      CustomTableHeaderCell,
      CustomTableHeaderRow,
      customDataTypes,
      optionsInit,
      dtKey,
      forceRerender,
      actions,
      refreshRows,
      stripped,
      initializeOptions,
      initializeCustomComponents
    } = nextProps;
    const { optionsInit: optionsInitProps } = this.props;
    const update = equal(optionsInit, optionsInitProps);
    const { customProps: customPropsProps } = this.props;
    const updateCustomProps = equal(customProps, customPropsProps);
 
    if (!update) {
      initializeOptions({
        optionsInit,
        dtKey,
        forceRerender,
        actions,
        refreshRows,
        stripped
      });
    }
 
    Iif (!updateCustomProps) {
      initializeCustomComponents({
        customProps,
        CustomTableBodyCell,
        CustomTableBodyRow,
        CustomTableHeaderCell,
        CustomTableHeaderRow,
        customDataTypes
      });
    }
    return !update || !updateCustomProps;
  }
 
  render() {
    return (
      <MuiThemeProvider theme={mainTheme}>
        <MuiPickersUtilsProvider
          utils={MomentUtils}
          locale={locale}
          moment={moment}
        >
          <DatatableContainer />
        </MuiPickersUtilsProvider>
      </MuiThemeProvider>
    );
  }
}
 
const mapDispatchToProps = dispatch => {
  return {
    initializeOptions: state => dispatch(initializeOptionsAction(state)),
    updateComponentSize: () => dispatch(updateComponentSizeAction()),
    initializeCustomComponents: state =>
      dispatch(initializeCustomComponentsAction(state))
  };
};
 
DatatableInitializer.propTypes = {
  customProps: customPropsPropType,
  initializeOptions: initializeOptionsPropType,
  dtKey: dtKeyPropType,
  initializeCustomComponents: initializeCustomComponentsPropType,
  updateComponentSize: updateComponentSizePropType,
  optionsInit: optionsPropType.isRequired,
  forceRerender: forceRerenderPropType,
  actions: actionsPropType,
  stripped: strippedPropType,
  refreshRows: refreshRowsPropType,
  CustomTableBodyCell: CustomTableBodyCellPropType,
  CustomTableBodyRow: CustomTableBodyRowPropType,
  CustomTableHeaderCell: CustomTableHeaderCellPropType,
  CustomTableHeaderRow: CustomTableHeaderRowPropType,
  customDataTypes: customDataTypesPropType
};
 
export default connect(
  null,
  mapDispatchToProps
)(DatatableInitializer);