update api call properties

This commit is contained in:
AntoninoP 2024-08-22 23:53:48 +02:00
parent bc8ad3f096
commit 771cef3e92

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,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;