Untitled
unknown
javascript
a year ago
6.4 kB
5
Indexable
function generateHomeContent(data) {
let html = `
<h2>Benvenuto nella home page</h2>
<div id="message" class="hidden"></div>
<br>
<a href="#" onclick="loadCreateGroup()">Crea un nuovo gruppo</a>
<br><br>
<h3>Ecco i gruppi creati da te:</h3>
<br>
<table border="1">
<tr>
<th>Title</th>
<th>Creation Date</th>
<th>Numero minimo di partecipanti raggiunto</th>
</tr>`;
if (data.groups && data.groups.length > 0) {
data.groups.forEach(group => {
html += `
<tr>
<td><a href="#" onclick="loadDetailsPage(${group.groupId})">${group.title}</a></td>
<td>${group.creationDate}</td>
<td>${group.status === "active" ? "sì" : "no"}</td>
</tr>`;
});
} else {
html += `
<tr>
<td colspan="3">Nessun gruppo trovato.</td>
</tr>`;
}
html += `
</table>
<br><br>
<h3>Ecco i tuoi inviti:</h3>
<table border="1">
<tr>
<th>Group ID</th>
<th>Invitation ID</th>
<th>Invitation Status</th>
<th>Accetta</th>
<th>Rifiuta</th>
</tr>`;
if (data.invitations && data.invitations.length > 0) {
data.invitations.forEach(invitation => {
const acceptFormId = `accept_form_${invitation.invitationId}`;
const refuseFormId = `refuse_form_${invitation.invitationId}`;
const acceptInputId = `accept_input_${invitation.invitationId}`;
const groupInputId = `group_input_${invitation.invitationId}`;
const refuseInputId = `refuse_input_${invitation.invitationId}`;
html += `
<tr>
<td>${invitation.groupId}</td>
<td>${invitation.invitationId}</td>
<td>${invitation.invitationStatus}</td>
<td>${invitation.invitationStatus === "invited" ? `
<form id="${acceptFormId}">
<input type="hidden" id="${acceptInputId}" name="acceptInvitationId" value="${invitation.invitationId}"/>
<input type="hidden" id="${groupInputId}" name="acceptGroupId" value="${invitation.groupId}"/>
<button type="submit" onclick="acceptInvitation(event, '${acceptInputId}', '${groupInputId}'); return false;">Accetta</button>
</form>
` : ''}</td>
<td>${invitation.invitationStatus === "invited" ? `
<form id="${refuseFormId}">
<input type="hidden" id="${refuseInputId}" name="refuseInvitationId" value="${invitation.invitationId}"/>
<button type="submit" onclick="refuseInvitation(event, '${refuseInputId}'); return false;">Rifiuta</button>
</form>
` : ''}</td>
</tr>`;
});
} else {
html += `
<tr>
<td colspan="5">Nessun invito trovato.</td>
</tr>`;
}
html += `
</table>
<br><br>
<a href="logout">Logout</a>`;
return html;
}
function acceptInvitation(event, acceptInputId, groupInputId) {
event.preventDefault();
const formData = {
acceptInvitationId: document.getElementById(acceptInputId).value,
acceptGroupId: document.getElementById(groupInputId).value,
};
const xhr = new XMLHttpRequest();
xhr.open("POST", "acceptInvitation", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status !== 200) {
console.error("Server error", xhr.status);
displayMessage("Errore nell'accettare l'invito", "error");
} else {
try {
const result = JSON.parse(xhr.responseText);
if(result.status === "error") {
displayMessage("Errore nell'accettare l'invito", "error");
} else {
loadHomePage();
}
} catch (error) {
console.error("Parsing error", error);
displayMessage("Error processing response", "error");
}
}
}
};
xhr.onerror = function() {
console.error("Request error", xhr.statusText);
displayMessage("Network error nell'accettare l'invito", "error");
};
try {
xhr.send(JSON.stringify(formData));
} catch (error) {
console.error("Send error", error);
displayMessage("Error sending request", "error");
}
}
function refuseInvitation(event, refuseInputId) {
event.preventDefault();
const formData = {
refuseInvitationId: document.getElementById(refuseInputId).value,
};
const xhr = new XMLHttpRequest();
xhr.open("POST", "refuseInvitation", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status !== 200) {
console.error("Server error", xhr.status);
displayMessage("Errore nel rifiutare l'invito", "error");
} else {
try {
const result = JSON.parse(xhr.responseText);
if(result.status === "error") {
displayMessage("Errore nel rifiutare l'invito", "error");
} else {
loadHomePage();
}
} catch (error) {
console.error("Parsing error", error);
displayMessage("Error processing response", "error");
}
}
}
};
xhr.onerror = function() {
console.error("Request error", xhr.statusText);
displayMessage("Network error nel rifiutare l'invito", "error");
};
try {
xhr.send(JSON.stringify(formData));
} catch (error) {
console.error("Send error", error);
displayMessage("Error sending request", "error");
}
}Editor is loading...
Leave a Comment