better code

This commit is contained in:
AntoninoP 2024-08-23 15:03:03 +02:00
parent 771cef3e92
commit e79b25b9a8

View File

@ -6,7 +6,6 @@ import serviiApi from "../../service/api.tsx";
import Loading from '../Loading/loading';
import PropTypes from "prop-types";
const ServerProprieties = () => {
const { serverName } = useParams();
const navigate = useNavigate();
@ -39,44 +38,36 @@ const ServerProprieties = () => {
fetchServer();
}, [serverName]);
const validateInput = (input) => {
return input.replace(/[^a-zA-Z]/g, '');
};
const handleChange = (e) => {
const { name, value } = e.target;
const validatedValue = name === 'motd' ? validateInput(value) : value;
setServer({ ...server, [name]: validatedValue });
setServer({ ...server, [name]: value });
};
const handleSave = async () => {
try {
const props = [];
if (server.maxPlayers !== initialServer.maxPlayers) {
props.push(['max-players', server.maxPlayers.toString()]);
}
if (server.motd !== initialServer.motd) {
props.push(['motd', server.motd]);
}
if (server.difficulty !== initialServer.difficulty) {
props.push(['difficulty', server.difficulty]);
}
if (server.enableCommandBlock.toString() !== initialServer.enableCommandBlock.toString()) {
props.push(['enable-command-block', server.enableCommandBlock.toString()]);
}
if (server.gamemode !== initialServer.gamemode) {
props.push(['gamemode', server.gamemode.toString()]);
}
if (server.hardcore.toString() !== initialServer.hardcore.toString()) {
props.push(['hardcore', server.hardcore.toString()]);
}
if (server.onlineMode.toString() !== initialServer.onlineMode.toString()) {
props.push(['online-mode', server.onlineMode.toString()]);
}
if (server.pvp.toString() !== initialServer.pvp.toString()) {
props.push(['pvp', server.pvp.toString()]);
const serverProperties = [
{ key: 'maxPlayers', type: 'number', name: 'max-players' },
{ key: 'motd', type: 'string', name: 'motd' },
{ key: 'difficulty', type: 'string', name: 'difficulty' },
{ key: 'enableCommandBlock', type: 'boolean', name: 'enable-command-block' },
{ key: 'gamemode', type: 'string', name: 'gamemode' },
{ key: 'hardcore', type: 'boolean', name: 'hardcore' },
{ key: 'onlineMode', type: 'boolean', name: 'online-mode' },
{ key: 'pvp', type: 'boolean', name: 'pvp' }
];
serverProperties.forEach(({ key, type, name }) => {
const currentValue = server[key];
const initialValue = initialServer[key];
const currentString = type === 'boolean' ? currentValue.toString() : currentValue;
const initialString = type === 'boolean' ? initialValue.toString() : initialValue;
if (currentString !== initialString) {
props.push([name, currentString]);
}
});
if (props.length > 0) {
await serviiApi.updateProperties(server.name, props);
@ -179,7 +170,6 @@ const ServerProprieties = () => {
);
};
ServerProprieties.propTypes = {
user: PropTypes.shape({
uid: PropTypes.string.isRequired,
@ -188,5 +178,4 @@ ServerProprieties.propTypes = {
}).isRequired,
};
export default ServerProprieties;