import React from 'react';
import Command from './Command.jsx';
import Modal from 'react-responsive-modal';
import RichMultiSelect from '../common/RichMultiSelect.jsx';
import { saveCommandGroupSettings, updateCommandGroup } from './service/CommandService';
export default class CommandGroup extends React.Component {
state = {
channels: [],
roles: [],
allowedChannels: [],
ignoredChannels: [],
allowedRoles: [],
ignoredRoles: [],
settingsOpen: false,
enableOpen: false,
disableOpen: false,
};
componentDidMount() {
this.updateState(this.props);
}
componentWillReceiveProps(props) {
this.updateState(props);
}
updateState(props) {
const state = {
channels: props.channels || [],
roles: props.roles || [],
isEnabled: props.defaultValue,
};
if (props.command) {
state.allowedChannels = (state.allowedChannels && state.allowedChannels.length) ? state.allowedChannels : props.command.allowedChannels || [];
state.ignoredChannels = (state.ignoredChannels && state.ignoredChannels.length) ? state.ignoredChannels : props.command.ignoredChannels || [];
state.allowedRoles = (state.allowedRoles && state.allowedRoles.length) ? state.allowedRoles : props.command.allowedRoles || [];
state.ignoredRoles = (state.ignoredRoles && state.ignoredRoles.length) ? state.ignoredRoles : props.command.ignoredRoles || [];
}
this.setState(state);
}
handleAllowedChannels = (props, selectedOptions) => {
const allowedChannels = selectedOptions.map(o => ({ id: o.value, name: o.label }));
this.setState({ allowedChannels });
}
handleIgnoredChannels = (props, selectedOptions) => {
const ignoredChannels = selectedOptions.map(o => ({ id: o.value, name: o.label }));
this.setState({ ignoredChannels });
}
handleAllowedRoles = (props, selectedOptions) => {
const allowedRoles = selectedOptions.map(o => ({ id: o.value, name: o.label }));
this.setState({ allowedRoles });
}
handleIgnoredRoles = (props, selectedOptions) => {
const ignoredRoles = selectedOptions.map(o => ({ id: o.value, name: o.label }));
this.setState({ ignoredRoles });
}
openSettings = () => {
this.setState({ settingsOpen: true });
}
closeSettings = () => {
this.setState({ settingsOpen: false });
}
async saveSettings(group) {
const settings = {
allowedChannels: this.state.allowedChannels.map(c => c.id || c.value).filter(c => c),
ignoredChannels: this.state.ignoredChannels.map(c => c.id || c.value).filter(c => c),
allowedRoles: this.state.allowedRoles.map(c => c.id || c.value).filter(c => c),
ignoredRoles: this.state.ignoredRoles.map(c => c.id || c.value).filter(c => c),
};
try {
await saveCommandGroupSettings(group, settings);
await this.props.getCommands();
this.closeSettings();
} catch (err) {
// pass
}
}
async updateAll(group, enabled) {
try {
await updateCommandGroup(group, enabled);
await this.props.getCommands();
this.closeEnable();
this.closeDisable();
} catch (err) {
// pass
}
}
openEnable = () => {
this.setState({ enableOpen: true });
}
closeEnable = () => {
this.setState({ enableOpen: false });
}
openDisable = () => {
this.setState({ disableOpen: true });
}
closeDisable = () => {
this.setState({ disableOpen: false });
}
render() {
let group = this.props.group;
let className = 'subtab-content is-active';
let commands = group.commands.map(c => {
const result = [];
result.push(
Note: This will overwrite all existing {group.name} command permissions.
Are you sure you want to enable all commands in this group?
Are you sure you want to disable all commands in this group?