From e79b25b9a8967c38311a85e881bf62196fa444c2 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Fri, 23 Aug 2024 15:03:03 +0200 Subject: [PATCH] better code --- .../ServerProperties/ServerProperties.jsx | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/pages/ServerProperties/ServerProperties.jsx b/src/pages/ServerProperties/ServerProperties.jsx index 2e8fd0b..c74da6f 100644 --- a/src/pages/ServerProperties/ServerProperties.jsx +++ b/src/pages/ServerProperties/ServerProperties.jsx @@ -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;