containers/Settings/LeaguesSettings.js

import React, { Component } from 'react';
import { connect } from 'react-redux';

import { getLeaguesSettings } from '../../selectors/settings';
import EditableSettingsTable from '../../components/MenuItems/Settings/EditableSettingsTable';
import { MENU_ITEMS, CNT_PATH, PERMISSIONS, CONFIRM_MODAL_CONTENT, MODALS } from '../../constants';
import { checkPermisson, getCnt, getActiveModal, getNavigatedLink } from '../../selectors/common';
import ConfirmationModal from '../../components/Modals/ConfirmationModal';
import { aSetNavigationInfo, aSetOpenModal } from '../../reducers/common';
import { aGetLeaguesSettings } from '../../reducers/settings';
import { aSaveLeaguesSettings } from '../../reducers/actions';
import { withRouterHooks } from '../../utils/router';

const mapToProps = (state) => ({
  leaguesSettings: getLeaguesSettings(state),
  checkPermisson: (payload) => checkPermisson(state, payload),
  cnt: getCnt(state),
  activeModal: getActiveModal(state),
  nextNavigationLink: getNavigatedLink(state),
});

const actionsToProps = (dispatch) => ({
  getLeaguesSettings: (payload) => dispatch(aGetLeaguesSettings(payload)),
  saveLeaguesSettings: (payload) => dispatch(aSaveLeaguesSettings(payload)),
  setOpenModal: (payload) => dispatch(aSetOpenModal(payload)),
  setNavigationInfo: (payload) => dispatch(aSetNavigationInfo(payload)),
});

/**
 * @class
 * @property {object} props
 * @property {Array} props.leaguesSettings All leagues settings data
 * @property {boolean} props.checkPermisson Check if user have
 * permission for specific action or view
 * @property {string} props.cnt Indicate CNT path
 * @property {Function} props.getLeaguesSettings Call API for leagues settings
 * @property {Function} props.saveLeaguesSettings Call API to save changes
 * @property {Function} props.setOpenModal Open modal
 * @property {string} props.activeModal Active modal
 * @property {object} props.nextNavigationLink link for navigation
 * @property {Function} props.setNavigationInfo set navigation info for unsaved data
 *
 */
class LeaguesSettings extends Component {
  constructor(props) {
    super(props);

    /**
     * @member {Array}
     */
    this.fields = [
      {
        label: '',
        field: 'emblem',
        getValue: (item) => `${CNT_PATH(this.props.cnt)}/${item.emblem}`,
        icon: true,
      },
      {
        label: 'ID',
        field: 'leagueID',
        getValue: (item, field) => item[field],
      },
      {
        label: 'Name',
        field: 'name',
        getValue: (item, field) => item[field],
      },
      {
        label: 'Status',
        field: 'status',
        getValue: (item, field) => item[field] === 1,
        checkbox: true,
      },
    ];

    /**
     * @member {boolean}
     */
    this.editPermission = props.checkPermisson(PERMISSIONS.SETTINGS_MARKET_EDIT);
  }

  /**
   * Get initial data
   *
   * @returns {void}
   */
  componentDidMount() {
    this.props.getLeaguesSettings();
  }

  /**
   * Call API to save data
   *
   * @function
   * @returns {void}
   */
  save = () => {
    this.props.saveLeaguesSettings({ leaguesSettings: this.state.table });
    this.editableSettingsTable.clearChanges();
  };

  /**
   * close modal and navigate
   *
   * @returns {void}
   */
  unsavedNavigation = () => {
    this.props.setOpenModal({ modal: '' });
    this.props.navigate(this.props.nextNavigationLink);
  };

  /**
   * set table info
   *
   * @param {Array} data
   * @returns {void}
   */
  setTableInfo = (data) => {
    this.setState({ table: data });
  };

  /**
   * Render
   *
   * @returns {view}
   */
  render() {
    return (
      <>
        <div className="user">
          {this.props.leaguesSettings && (
            <EditableSettingsTable
              key={MENU_ITEMS.SETTINGS.SETTINGS_LEAGUES_VIEW.path}
              tableRows={this.props.leaguesSettings}
              fields={this.fields}
              save={this.save}
              editPermission={this.editPermission}
              setNavigationInfo={this.props.setNavigationInfo}
              nextNavigationLink={this.props.nextNavigationLink}
              setTableInfo={this.setTableInfo}
              ref={(ref) => {
                this.editableSettingsTable = ref;
              }}
            />
          )}
        </div>
        {this.props.activeModal === MODALS.SAVE_BEFORE_NAVIGATION ? (
          <ConfirmationModal
            modalData={CONFIRM_MODAL_CONTENT.SAVE_CHANGES}
            cancel={this.unsavedNavigation}
            confirmation={this.save}
            cancelBtnText="Cancel"
            confirmBtnText="Save"
            logo
          />
        ) : null}
      </>
    );
  }
}

export default connect(mapToProps, actionsToProps)(withRouterHooks(LeaguesSettings));