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