From 771cef3e9257af51d1d03592aba75909f9bab39d Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Thu, 22 Aug 2024 23:53:48 +0200 Subject: [PATCH 1/2] update api call properties --- .../ServerProperties/ServerProperties.jsx | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/pages/ServerProperties/ServerProperties.jsx b/src/pages/ServerProperties/ServerProperties.jsx index 19a7a75..2e8fd0b 100644 --- a/src/pages/ServerProperties/ServerProperties.jsx +++ b/src/pages/ServerProperties/ServerProperties.jsx @@ -1,3 +1,4 @@ +/* eslint-disable react/prop-types */ import { useEffect, useState } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import styles from './ServerProperties.module.scss'; @@ -5,10 +6,12 @@ import serviiApi from "../../service/api.tsx"; import Loading from '../Loading/loading'; import PropTypes from "prop-types"; -const ServerProprieties = ({ user }) => { + +const ServerProprieties = () => { const { serverName } = useParams(); const navigate = useNavigate(); const [server, setServer] = useState(null); + const [initialServer, setInitialServer] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); @@ -21,6 +24,7 @@ const ServerProprieties = ({ user }) => { if (foundServer) { setServer(foundServer); + setInitialServer(foundServer); } else { setError('Server not found'); } @@ -47,18 +51,37 @@ const ServerProprieties = ({ user }) => { const handleSave = async () => { try { - const props = [ - ['max-players', server.maxPlayers.toString()], - ['motd', server.motd], - ['difficulty', server.difficulty], - ['enable-command-block', server.enableCommandBlock.toString()], - ['gamemode', server.gamemode.toString()], - ['hardcore', server.hardcore.toString()], - ['online-mode', server.onlineMode.toString()], - ['pvp', server.pvp.toString()] - ]; + const props = []; - await serviiApi.updateProperties(server.name, 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()]); + } + + if (props.length > 0) { + await serviiApi.updateProperties(server.name, props); + setInitialServer({ ...server }); + } } catch (error) { console.error('Error updating server:', error); alert('Error updating server'); @@ -156,12 +179,14 @@ const ServerProprieties = ({ user }) => { ); }; + ServerProprieties.propTypes = { user: PropTypes.shape({ uid: PropTypes.string.isRequired, displayName: PropTypes.string, email: PropTypes.string, - }), + }).isRequired, }; + export default ServerProprieties; From e79b25b9a8967c38311a85e881bf62196fa444c2 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Fri, 23 Aug 2024 15:03:03 +0200 Subject: [PATCH 2/2] 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;