import axios from 'axios';
import React from 'react';
import ReactTooltip from 'react-tooltip';
import Loader from '../common/Loader.jsx';
export default class CommandList extends React.Component {
state = {
customcommands: {},
prefix: '?',
isLoading: true,
}
async componentWillMount() {
try {
let response = await axios.get(`/api/modules/${this.props.match.params.id}/customcommands`);
this.setState({
customcommands: response.data.customcommands || {},
prefix: response.data.prefix || '?',
isLoading: false,
});
} catch (e) {
this.setState({ error: 'Failed to get data, try again later' });
}
}
componentDidUpdate() {
this.commandList = new List('commandList', {
page: 8,
pagination: {
innerWindow: 1,
outerWindow: 1,
left: 0,
right: 0,
paginationClass: 'pagination-list',
},
valueNames: ['cmd-name', 'cmd-response'],
});
}
deleteCommand(command) {
if (command.key) {
command.command = command.key;
}
const url = `/api/server/${this.props.match.params.id}/customCommand/delete`;
const commandName = command.command;
const data = { command: commandName };
let htmlValue = `${this.state.prefix}${commandName}`;
// escape the value to match list.js internal values
htmlValue = htmlValue.replace(/&/g, '&').replace(//g, '>');
axios.post(url, data)
.then((res) => {
if (res.status === 200) {
this.commandList.remove('cmd-name', htmlValue);
_showSuccess('Command removed.');
} else {
_showError('An error occurred.');
}
})
.catch((err) => {
if (err) {
_showError('An error occurred.');
}
});
}
editCommand (command) {
}
pick(o, ...props) {
return Object.assign({}, ...Object.keys(o).filter(k => [...props].includes(k))
.map(prop => ({ [prop]: o[prop] })));
}
ucfirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
render() {
if (this.state.isLoading) {
return