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(); return result; }); const module = this.props.data.module; const roles = this.state.roles; const channels = this.state.channels.filter(c => (c.type === 0 || c.type === 4)); const channelOptions = channels.map(c => ({ value: c.id, label: c.name })); const roleOptions = roles.map(r => ({ value: r.id, label: r.name })); const allowedChannels = channels.filter(c => this.state.allowedChannels.find(i => (i && (i.id || i)) === c.id)); const ignoredChannels = channels.filter(c => this.state.ignoredChannels.find(i => (i && (i.id || i)) === c.id)); const allowedRoles = roles.filter(r => this.state.allowedRoles.find(i => (i && (i.id || i)) === r.id)); const ignoredRoles = roles.filter(r => this.state.ignoredRoles.find(i => (i && (i.id || i)) === r.id)); const settingsClasses = { modal: 'command-settings-modal', }; const confirmClasses = { modal: 'help-modal', }; return (

{group.name}

{commands}

Additional Permissions ({group.name})

Note: This will overwrite all existing {group.name} command permissions.

Enable All {group.name}

Are you sure you want to enable all commands in this group?

Disable All {group.name}

Are you sure you want to disable all commands in this group?

); } }