-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (91 loc) · 3.42 KB
/
app.js
File metadata and controls
107 lines (91 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
document.addEventListener("DOMContentLoaded", () => {
const activitiesList = document.getElementById("activities-list");
const activitySelect = document.getElementById("activity");
const signupForm = document.getElementById("signup-form");
const messageDiv = document.getElementById("message");
// Function to fetch activities from API
async function fetchActivities() {
try {
const response = await fetch("/activities");
const activities = await response.json();
// Clear loading message
activitiesList.innerHTML = "";
// Populate activities list
Object.entries(activities).forEach(([name, details]) => {
const activityCard = document.createElement("div");
activityCard.className = "activity-card";
const spotsLeft = details.max_participants - details.participants.length;
// Build participants list HTML
let participantsHTML = "";
if (details.participants && details.participants.length > 0) {
participantsHTML = `
<div class="participants-section">
<strong>Participants:</strong>
<ul class="participants-list">
${details.participants.map(email => `<li>${email}</li>`).join("")}
</ul>
</div>
`;
} else {
participantsHTML = `
<div class="participants-section">
<strong>Participants:</strong>
<span class="no-participants">No participants yet</span>
</div>
`;
}
activityCard.innerHTML = `
<h4>${name}</h4>
<p>${details.description}</p>
<p><strong>Schedule:</strong> ${details.schedule}</p>
<p><strong>Availability:</strong> ${spotsLeft} spots left</p>
${participantsHTML}
`;
activitiesList.appendChild(activityCard);
// Add option to select dropdown
const option = document.createElement("option");
option.value = name;
option.textContent = name;
activitySelect.appendChild(option);
});
} catch (error) {
activitiesList.innerHTML = "<p>Failed to load activities. Please try again later.</p>";
console.error("Error fetching activities:", error);
}
}
// Handle form submission
signupForm.addEventListener("submit", async (event) => {
event.preventDefault();
const email = document.getElementById("email").value;
const activity = document.getElementById("activity").value;
try {
const response = await fetch(
`/activities/${encodeURIComponent(activity)}/signup?email=${encodeURIComponent(email)}`,
{
method: "POST",
}
);
const result = await response.json();
if (response.ok) {
messageDiv.textContent = result.message;
messageDiv.className = "success";
signupForm.reset();
} else {
messageDiv.textContent = result.detail || "An error occurred";
messageDiv.className = "error";
}
messageDiv.classList.remove("hidden");
// Hide message after 5 seconds
setTimeout(() => {
messageDiv.classList.add("hidden");
}, 5000);
} catch (error) {
messageDiv.textContent = "Failed to sign up. Please try again.";
messageDiv.className = "error";
messageDiv.classList.remove("hidden");
console.error("Error signing up:", error);
}
});
// Initialize app
fetchActivities();
});