import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getMarketsSettings } from '../../selectors/settings';
import EditableSettingsTable from '../../components/MenuItems/Settings/EditableSettingsTable';
import { CONFIRM_MODAL_CONTENT, MENU_ITEMS, MODALS, PERMISSIONS } from '../../constants';
import { checkPermisson, getActiveModal, getNavigatedLink } from '../../selectors/common';
import ConfirmationModal from '../../components/Modals/ConfirmationModal';
import { aSetNavigationInfo, aSetOpenModal } from '../../reducers/common';
import { aGetMarketsSettings } from '../../reducers/settings';
import { aSaveMarketsSettings } from '../../reducers/actions';
import { withRouterHooks } from '../../utils/router';
const mapToProps = (state) => ({
marketSettings: getMarketsSettings(state),
checkPermisson: (payload) => checkPermisson(state, payload),
activeModal: getActiveModal(state),
nextNavigationLink: getNavigatedLink(state),
});
const actionsToProps = (dispatch) => ({
getMarketsSettings: (payload) => dispatch(aGetMarketsSettings(payload)),
saveMarketsSettings: (payload) => dispatch(aSaveMarketsSettings(payload)),
setOpenModal: (payload) => dispatch(aSetOpenModal(payload)),
setNavigationInfo: (payload) => dispatch(aSetNavigationInfo(payload)),
});
/**
* @class
* @property {object} props
* @property {Array} props.marketSettings All markets settings
* @property {boolean} props.checkPermisson Check if user have
* permission for specific action or view
* @property {Function} props.getMarketsSettings Call API to get market settings
* @property {Function} props.saveMarketsSettings Call API to change market settings
* @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 MarketsSettings extends Component {
constructor(props) {
super(props);
/**
* @member {Array}
*/
this.fields = [
{
label: 'ID',
field: 'id',
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.getMarketsSettings();
}
/**
* Call API to save data
*
* @function
* @returns {void}
*/
save = () => {
this.props.saveMarketsSettings({ marketsSettings: 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.marketSettings && (
<EditableSettingsTable
key={MENU_ITEMS.SETTINGS.SETTINGS_MARKET_VIEW.path}
tableRows={this.props.marketSettings}
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.bind(this, this.state.tableRows)}
cancelBtnText="Cancel"
confirmBtnText="Save"
logo
/>
) : null}
</>
);
}
}
export default connect(mapToProps, actionsToProps)(withRouterHooks(MarketsSettings));