Skip to content

Commit

Permalink
edit(turnout/opening-status): change styling of turnout and opening-s…
Browse files Browse the repository at this point in the history
…tatus pages.
  • Loading branch information
niccolozanotti committed Dec 5, 2024
1 parent 0b813a8 commit e389a30
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 114 deletions.
88 changes: 80 additions & 8 deletions _includes/opening-status.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,68 @@
<div id="status-message"></div>
<div id="status-container">
<div id="status-message">
<h3 class="loading">Loading status...</h3>
</div>
</div>

<style>
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f5f5f5;
}

#status-container {
text-align: center;
padding: 20px;
background: white;
border-radius: 16px;
box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
width: 90%; /* Use 90% of the screen width */
max-width: 600px; /* Limit width on larger screens */
box-sizing: border-box;
}

#status-message h3 {
font-size: 24px;
margin: 0;
line-height: 1.5;
}

#status-message h3.open {
color: #28a745; /* Green for open */
}

#status-message h3.closed {
color: #dc3545; /* Red for closed */
}

#status-message p {
font-size: 14px;
color: #666;
margin: 8px 0 0;
}

.loading {
color: #007bff;
animation: pulse 1.5s infinite;
}

@keyframes pulse {
0%, 100% {
opacity: 0.5;
}
50% {
opacity: 1;
}
}
</style>



<script>
document.addEventListener("DOMContentLoaded", function () {
Expand All @@ -7,9 +71,9 @@
function formatTime(timeStr) {
const timeParts = timeStr.split(':');
if (timeParts.length === 3) {
return `${timeParts[0]}:${timeParts[1]}:${timeParts[2].split('.')[0]}`; // Truncate milliseconds
return `${timeParts[0]}:${timeParts[1]}:${timeParts[2].split('.')[0]}`;
}
return timeStr; // Fallback to original if format is unexpected
return timeStr;
}

function fetchStatus() {
Expand All @@ -20,20 +84,28 @@
const lastUpdatedTime = data.last_update ? formatTime(data.last_update) : '';

if (data.status.includes("open")) {
statusMessageElement.innerHTML = `<h3>Siamo aperti!${lastUpdatedTime ? ' (Last updated at ' + lastUpdatedTime + ')' : ''} </h3>`;
statusMessageElement.innerHTML = `
<h3 class="open">We are open!</h3>
${lastUpdatedTime ? `<p>Last updated at ${lastUpdatedTime}</p>` : ''}
`;
} else if (data.status.includes("closed")) {
statusMessageElement.innerHTML = `<h3>Siamo chiusi!${lastUpdatedTime ? ' (Last updated at ' + lastUpdatedTime + ')' : ''} </h3>`;
statusMessageElement.innerHTML = `
<h3 class="closed">We are closed!</h3>
${lastUpdatedTime ? `<p>Last updated at ${lastUpdatedTime}</p>` : ''}
`;
}
})
.catch(error => {
console.error('Error fetching the status:', error);
const statusMessageElement = document.getElementById('status-message');
statusMessageElement.innerHTML = `
<h3 class="closed">Error fetching status</h3>
<p>Please try again later.</p>
`;
});
}

// Initial fetch when the page loads
fetchStatus();

// Update status every 60 seconds (60000 milliseconds)
setInterval(fetchStatus, 60000);
});
</script>
120 changes: 60 additions & 60 deletions _includes/scripts/turnout.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
const registerForm = document.getElementById('registerForm');
const nameInput = document.getElementById('nameInput');
const occupantsList = document.getElementById('occupantsList');

function updateOccupantsList() {
fetch('https://d1s96qvov1.execute-api.eu-north-1.amazonaws.com/dev/people/status')
.then(response => response.json())
.then(data => {
occupantsList.innerHTML = ''; // Clear the list first
data.occupants.forEach(name => {
const listItem = document.createElement('li');
listItem.textContent = name;

const unregisterButton = document.createElement('button');
unregisterButton.textContent = 'Unregister';
unregisterButton.classList.add('unregister-button');
unregisterButton.onclick = () => unregister(name);

listItem.appendChild(unregisterButton);
occupantsList.appendChild(listItem);
});
});
}

registerForm.onsubmit = function(event) {
event.preventDefault();
const name = nameInput.value;

fetch('https://d1s96qvov1.execute-api.eu-north-1.amazonaws.com/dev/people/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: name }),
})
.then(response => response.json())
.then(data => {
if (data.status === "registered") {
nameInput.value = ''; // Clear the input field
updateOccupantsList(); // Refresh the occupants list
document.addEventListener("DOMContentLoaded", function () {
const apiUrl = "https://d1s96qvov1.execute-api.eu-north-1.amazonaws.com/dev/people";
const registerForm = document.getElementById("registerForm");
const nameInput = document.getElementById("nameInput");
const occupantsList = document.getElementById("occupantsList");

// Fetch and display the current occupants
function fetchOccupants() {
fetch(`${apiUrl}/status`)
.then(response => response.json())
.then(data => {
occupantsList.innerHTML = ""; // Clear the list
if (data.occupants && data.occupants.length > 0) {
data.occupants.forEach(person => {
const listItem = document.createElement("li");
listItem.textContent = person;
const unregisterButton = document.createElement("button");
unregisterButton.textContent = "Rimuovi";
unregisterButton.className = "unregister-button";
unregisterButton.addEventListener("click", () => unregisterPerson(person));
listItem.appendChild(unregisterButton);
occupantsList.appendChild(listItem);
});
} else {
occupantsList.innerHTML = "<li>Nessuno è in aula in questo momento.</li>";
}
});
};
})
.catch(error => console.error("Error fetching occupants:", error));
}

function unregister(name) {
fetch('https://d1s96qvov1.execute-api.eu-north-1.amazonaws.com/dev/people/unregister', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: name }),
// Register a new person
registerForm.addEventListener("submit", function (event) {
event.preventDefault();
const name = nameInput.value.trim();
if (name) {
fetch(`${apiUrl}/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name })
})
.then(() => {
nameInput.value = "";
fetchOccupants();
})
.catch(error => console.error("Error registering person:", error));
}
});

// Unregister a person
function unregisterPerson(name) {
fetch(`${apiUrl}/unregister`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name })
})
.then(response => response.json())
.then(data => {
if (data.status === "unregistered") {
updateOccupantsList(); // Refresh the occupants list
}
});
}

updateOccupantsList();
setInterval(updateOccupantsList, 20000); // Update every 20 seconds

.then(() => fetchOccupants())
.catch(error => console.error("Error unregistering person:", error));
}

// Fetch occupants initially and at intervals
fetchOccupants();
setInterval(fetchOccupants, 30000); // Refresh every 30 seconds
});
5 changes: 3 additions & 2 deletions _includes/turnout-form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<h3>Sei in aula o te ne stai andando? 👀</h3>
<form id="registerForm">
<input type="text" id="nameInput" placeholder="Inserisci il tuo nome" required>
<button type="submit">Registrati</button>
<input type="text" id="nameInput" placeholder="Inserisci il tuo nome" required>
<button type="submit">Registrati</button>
</form>

<h3>Presenti in aula real-time:</h3>
Expand Down
Loading

0 comments on commit e389a30

Please sign in to comment.