[+] Fixed UpdateProperties front optimization

Co-authored-by: Antoninop <antoninopiraino70@gmail.com>
This commit is contained in:
charleslemaux 2024-08-23 15:50:53 +02:00 committed by GitHub
commit 8672c4432d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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,11 @@ 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 +23,7 @@ const ServerProprieties = ({ user }) => {
if (foundServer) {
setServer(foundServer);
setInitialServer(foundServer);
} else {
setError('Server not found');
}
@ -35,30 +38,41 @@ const ServerProprieties = ({ user }) => {
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 = [
['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 = [];
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' }
];
await serviiApi.updateProperties(server.name, props);
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);
setInitialServer({ ...server });
}
} catch (error) {
console.error('Error updating server:', error);
alert('Error updating server');
@ -161,7 +175,7 @@ ServerProprieties.propTypes = {
uid: PropTypes.string.isRequired,
displayName: PropTypes.string,
email: PropTypes.string,
}),
}).isRequired,
};
export default ServerProprieties;