feat(ui): added complete js routing for create game

added complete js routing for each button. Removed every form and replaced buttons with divs
This commit is contained in:
LQ63
2025-11-11 16:46:06 +01:00
parent b508d2f428
commit 6d958cdd9e
7 changed files with 252 additions and 45 deletions

View File

@@ -103,21 +103,161 @@ function sendGameCreationRequest(dataObject) {
body: JSON.stringify(dataObject)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
return response.json().then(data => {
if (!response.ok) {
return Promise.reject(data);
}
return data;
});
})
.then(data => {
if (data.status === 'success') {
// Redirect the user
window.location.href = data.redirectUrl;
} else {
console.error("Game creation failed:", data.message);
}
})
.catch(error => {
console.error('Fetch error:', error);
alert('Could not create game. Please try again.');
if (error && error.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
});
}
}
function startGame(gameId) {
sendGameStartRequest(gameId)
}
function sendGameStartRequest(gameId) {
const route = jsRoutes.controllers.IngameController.startGame(gameId);
fetch(route.url, {
method: route.type,
})
.then(response => {
return response.json().then(data => {
if (!response.ok) {
return Promise.reject(data);
}
return data;
});
})
.then(data => {
// SUCCESS BLOCK: data is the { status: 'success', ... } object
if (data.status === 'success') {
window.location.href = data.redirectUrl;
}
})
.catch(error => {
if (error && error.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
});
}
function removePlayer(gameid, playersessionId) {
sendRemovePlayerRequest(gameid, playersessionId)
}
function sendRemovePlayerRequest(gameId, playersessionId) {
const route = jsRoutes.controllers.IngameController.kickPlayer(gameId, playersessionId);
fetch(route.url, {
method: route.type,
headers: {
'Content-Type': 'application/json',
}
})
.then(response => {
return response.json().then(data => {
if (!response.ok) {
return Promise.reject(data);
}
return data;
});
})
.then(data => {
// SUCCESS BLOCK: data is the { status: 'success', ... } object
if (data.status === 'success') {
window.location.href = data.redirectUrl;
}
})
.catch(error => {
if (error && error.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
});
}
function leaveGame(gameId) {
sendLeavePlayerRequest(gameId)
}
function sendLeavePlayerRequest(gameId) {
const route = jsRoutes.controllers.IngameController.leaveGame(gameId);
fetch(route.url, {
method: route.type,
})
.then(response => {
return response.json().then(data => {
if (!response.ok) {
return Promise.reject(data);
}
return data;
});
})
.then(data => {
// SUCCESS BLOCK: data is the { status: 'success', ... } object
if (data.status === 'success') {
window.location.href = data.redirectUrl;
}
})
.catch(error => {
if (error && error.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
});
}
function handlePlayCard(cardobject, gameId) {
const cardId = cardobject.dataset.cardId;
const jsonObj = {
cardID: cardId
}
sendPlayCardRequest(jsonObj, gameId)
}
function sendPlayCardRequest(jsonObj, gameId) {
const route = jsRoutes.controllers.IngameController.playCard(gameId);
fetch(route.url, {
method: route.type,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(jsonObj)
})
.then(response => {
return response.json().then(data => {
if (!response.ok) {
return Promise.reject(data);
}
return data;
});
})
.then(data => {
if (data.status === 'success') {
window.location.href = data.redirectUrl;
}
})
.catch(error => {
if (error && error.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
});
}