mirror of
https://github.com/hubHarmony/servii-frontend.git
synced 2024-11-18 05:40:31 +00:00
[+] Fixed UpdateProperties front optimization
Co-authored-by: Antoninop <antoninopiraino70@gmail.com>
This commit is contained in:
commit
8672c4432d
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable react/prop-types */
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useParams, useNavigate } from 'react-router-dom';
|
import { useParams, useNavigate } from 'react-router-dom';
|
||||||
import styles from './ServerProperties.module.scss';
|
import styles from './ServerProperties.module.scss';
|
||||||
@ -5,10 +6,11 @@ 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 = ({ user }) => {
|
const ServerProprieties = () => {
|
||||||
const { serverName } = useParams();
|
const { serverName } = useParams();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [server, setServer] = useState(null);
|
const [server, setServer] = useState(null);
|
||||||
|
const [initialServer, setInitialServer] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
@ -21,6 +23,7 @@ const ServerProprieties = ({ user }) => {
|
|||||||
|
|
||||||
if (foundServer) {
|
if (foundServer) {
|
||||||
setServer(foundServer);
|
setServer(foundServer);
|
||||||
|
setInitialServer(foundServer);
|
||||||
} else {
|
} else {
|
||||||
setError('Server not found');
|
setError('Server not found');
|
||||||
}
|
}
|
||||||
@ -35,30 +38,41 @@ const ServerProprieties = ({ user }) => {
|
|||||||
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 = [];
|
||||||
['max-players', server.maxPlayers.toString()],
|
|
||||||
['motd', server.motd],
|
const serverProperties = [
|
||||||
['difficulty', server.difficulty],
|
{ key: 'maxPlayers', type: 'number', name: 'max-players' },
|
||||||
['enable-command-block', server.enableCommandBlock.toString()],
|
{ key: 'motd', type: 'string', name: 'motd' },
|
||||||
['gamemode', server.gamemode.toString()],
|
{ key: 'difficulty', type: 'string', name: 'difficulty' },
|
||||||
['hardcore', server.hardcore.toString()],
|
{ key: 'enableCommandBlock', type: 'boolean', name: 'enable-command-block' },
|
||||||
['online-mode', server.onlineMode.toString()],
|
{ key: 'gamemode', type: 'string', name: 'gamemode' },
|
||||||
['pvp', server.pvp.toString()]
|
{ 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);
|
await serviiApi.updateProperties(server.name, props);
|
||||||
|
setInitialServer({ ...server });
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating server:', error);
|
console.error('Error updating server:', error);
|
||||||
alert('Error updating server');
|
alert('Error updating server');
|
||||||
@ -161,7 +175,7 @@ ServerProprieties.propTypes = {
|
|||||||
uid: PropTypes.string.isRequired,
|
uid: PropTypes.string.isRequired,
|
||||||
displayName: PropTypes.string,
|
displayName: PropTypes.string,
|
||||||
email: PropTypes.string,
|
email: PropTypes.string,
|
||||||
}),
|
}).isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ServerProprieties;
|
export default ServerProprieties;
|
||||||
|
Loading…
Reference in New Issue
Block a user