Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"chat.tools.terminal.autoApprove": {
"pytest": true
}
}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fastapi
uvicorn
httpx
watchfiles
watchfiles
pytest
91 changes: 73 additions & 18 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,60 @@

# In-memory activity database
activities = {
"Chess Club": {
"description": "Learn strategies and compete in chess tournaments",
"schedule": "Fridays, 3:30 PM - 5:00 PM",
"max_participants": 12,
"participants": ["michael@mergington.edu", "daniel@mergington.edu"]
},
"Programming Class": {
"description": "Learn programming fundamentals and build software projects",
"schedule": "Tuesdays and Thursdays, 3:30 PM - 4:30 PM",
"max_participants": 20,
"participants": ["emma@mergington.edu", "sophia@mergington.edu"]
},
"Gym Class": {
"description": "Physical education and sports activities",
"schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM",
"max_participants": 30,
"participants": ["john@mergington.edu", "olivia@mergington.edu"]
}
"Chess Club": {
"description": "Learn strategies and compete in chess tournaments",
"schedule": "Fridays, 3:30 PM - 5:00 PM",
"max_participants": 12,
"participants": ["michael@mergington.edu", "daniel@mergington.edu"]
},
"Programming Class": {
"description": "Learn programming fundamentals and build software projects",
"schedule": "Tuesdays and Thursdays, 3:30 PM - 4:30 PM",
"max_participants": 20,
"participants": ["emma@mergington.edu", "sophia@mergington.edu"]
},
"Gym Class": {
"description": "Physical education and sports activities",
"schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM",
"max_participants": 30,
"participants": ["john@mergington.edu", "olivia@mergington.edu"]
},
"Basketball Team": {
"description": "Competitive basketball league and training",
"schedule": "Mondays and Thursdays, 4:00 PM - 5:30 PM",
"max_participants": 15,
"participants": ["alex@mergington.edu"]
},
"Tennis Club": {
"description": "Learn and compete in tennis matches",
"schedule": "Wednesdays and Saturdays, 3:00 PM - 4:30 PM",
"max_participants": 10,
"participants": ["jordan@mergington.edu", "taylor@mergington.edu"]
},
"Drama Club": {
"description": "Perform in theatrical productions and develop acting skills",
"schedule": "Tuesdays and Thursdays, 5:00 PM - 6:30 PM",
"max_participants": 25,
"participants": ["casey@mergington.edu"]
},
"Art Studio": {
"description": "Explore painting, drawing, and sculpture techniques",
"schedule": "Mondays, 4:00 PM - 5:30 PM",
"max_participants": 18,
"participants": ["morgan@mergington.edu", "ryan@mergington.edu"]
},
"Debate Team": {
"description": "Develop argumentation and public speaking skills",
"schedule": "Wednesdays, 4:00 PM - 5:30 PM",
"max_participants": 16,
"participants": ["avery@mergington.edu"]
},
"Robotics Club": {
"description": "Design and build robots for competitions",
"schedule": "Fridays, 4:00 PM - 6:00 PM",
"max_participants": 14,
"participants": ["chris@mergington.edu", "blake@mergington.edu"]
}
}


Expand All @@ -62,6 +98,25 @@ def signup_for_activity(activity_name: str, email: str):
# Get the specific activity
activity = activities[activity_name]

# Validate student is not already signed up
if email in activity["participants"]:
raise HTTPException(status_code=400, detail="Student already signed up")

# Add student
activity["participants"].append(email)
return {"message": f"Signed up {email} for {activity_name}"}


@app.post("/activities/{activity_name}/unregister")
def unregister_from_activity(activity_name: str, email: str):
"""Unregister a student from an activity"""
if activity_name not in activities:
raise HTTPException(status_code=404, detail="Activity not found")

activity = activities[activity_name]

if email not in activity["participants"]:
raise HTTPException(status_code=400, detail="Student is not signed up for this activity")

activity["participants"].remove(email)
Comment on lines +110 to +121
return {"message": f"Unregistered {email} from {activity_name}"}
111 changes: 97 additions & 14 deletions src/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,84 @@ document.addEventListener("DOMContentLoaded", () => {
const signupForm = document.getElementById("signup-form");
const messageDiv = document.getElementById("message");

function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#39;");
}

function showMessage(text, type) {
messageDiv.textContent = text;
messageDiv.className = type;
messageDiv.classList.remove("hidden");

setTimeout(() => {
messageDiv.classList.add("hidden");
}, 5000);
}

// Function to fetch activities from API
async function fetchActivities() {
activitiesList.innerHTML = `
<div class="activities-loading" role="status" aria-live="polite">
<div class="loading-spinner"></div>
<h4>Loading activities...</h4>
<p>Fetching the latest participant updates</p>
</div>
`;

try {
const response = await fetch("/activities");
const response = await fetch(`/activities?t=${Date.now()}`, {
cache: "no-store",
});
const activities = await response.json();

// Clear loading message
activitiesList.innerHTML = "";
activitySelect.innerHTML = '<option value="">-- Select an activity --</option>';

// 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;
const participantsList = details.participants.length
? details.participants
.map(
(participant) => `
<li class="participant-item">
<span class="participant-email">${escapeHtml(participant)}</span>
<button
type="button"
class="participant-remove-btn"
data-activity="${encodeURIComponent(name)}"
data-email="${encodeURIComponent(participant)}"
aria-label="Remove ${escapeHtml(participant)} from ${escapeHtml(name)}"
title="Unregister participant"
>
Comment on lines +52 to +65
🗑️
</button>
</li>
`
)
.join("")
: "<li class=\"participants-empty\">No participants yet</li>";

activityCard.innerHTML = `
<h4>${name}</h4>
<p>${details.description}</p>
<p><strong>Schedule:</strong> ${details.schedule}</p>
<p><strong>Availability:</strong> ${spotsLeft} spots left</p>
<div class="participants-section">
<p class="participants-heading"><strong>Participants (${details.participants.length}):</strong></p>
<ul class="participants-list">
${participantsList}
</ul>
</div>
`;

activitiesList.appendChild(activityCard);
Expand Down Expand Up @@ -59,25 +116,51 @@ document.addEventListener("DOMContentLoaded", () => {
const result = await response.json();

if (response.ok) {
messageDiv.textContent = result.message;
messageDiv.className = "success";
showMessage(result.message, "success");
signupForm.reset();
await fetchActivities();
} else {
messageDiv.textContent = result.detail || "An error occurred";
messageDiv.className = "error";
showMessage(result.detail || "An error occurred", "error");
}
} catch (error) {
showMessage("Failed to sign up. Please try again.", "error");
console.error("Error signing up:", error);
}
});

activitiesList.addEventListener("click", async (event) => {
const removeButton = event.target.closest(".participant-remove-btn");
if (!removeButton) {
return;
}

const activity = decodeURIComponent(removeButton.dataset.activity || "");
const email = decodeURIComponent(removeButton.dataset.email || "");

messageDiv.classList.remove("hidden");
if (!activity || !email) {
showMessage("Unable to remove participant. Please try again.", "error");
return;
}

try {
const response = await fetch(
`/activities/${encodeURIComponent(activity)}/unregister?email=${encodeURIComponent(email)}`,
{
method: "POST",
}
);

const result = await response.json();

// Hide message after 5 seconds
setTimeout(() => {
messageDiv.classList.add("hidden");
}, 5000);
if (response.ok) {
showMessage(result.message, "success");
await fetchActivities();
} else {
showMessage(result.detail || "Failed to remove participant.", "error");
}
} catch (error) {
messageDiv.textContent = "Failed to sign up. Please try again.";
messageDiv.className = "error";
messageDiv.classList.remove("hidden");
console.error("Error signing up:", error);
showMessage("Failed to remove participant. Please try again.", "error");
console.error("Error unregistering participant:", error);
}
});

Expand Down
106 changes: 106 additions & 0 deletions src/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ section h3 {
color: #1a237e;
}

.activities-loading {
min-height: 280px;
border: 2px dashed #c7d2fe;
border-radius: 10px;
background: linear-gradient(135deg, #eef2ff, #f8fafc);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 24px;
margin-bottom: 15px;
}

.activities-loading h4 {
font-size: 1.4rem;
color: #1a237e;
margin-bottom: 6px;
}

.activities-loading p {
font-size: 1rem;
color: #475569;
}

.loading-spinner {
width: 54px;
height: 54px;
border: 5px solid #c7d2fe;
border-top-color: #3949ab;
border-radius: 50%;
margin-bottom: 14px;
animation: spin 0.9s linear infinite;
}

@keyframes spin {
to {
transform: rotate(360deg);
}
}

.activity-card {
margin-bottom: 15px;
padding: 15px;
Expand All @@ -74,6 +115,71 @@ section h3 {
margin-bottom: 8px;
}

.participants-section {
margin-top: 12px;
padding: 10px 12px;
background-color: #ffffff;
border: 1px solid #e3e6ef;
border-radius: 6px;
}

.participants-heading {
margin-bottom: 8px;
color: #1a237e;
}

.participants-list {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 8px;
}

.participant-item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 8px 10px;
background-color: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 6px;
}

.participant-email {
color: #334155;
overflow-wrap: anywhere;
}

.participants-empty {
list-style: none;
margin-left: 0;
color: #64748b;
font-style: italic;
}

.participant-remove-btn {
border: none;
background-color: #fee2e2;
color: #b91c1c;
width: 30px;
height: 30px;
border-radius: 999px;
cursor: pointer;
font-size: 14px;
line-height: 1;
display: inline-flex;
align-items: center;
justify-content: center;
transition: background-color 0.2s ease, transform 0.2s ease;
}

.participant-remove-btn:hover {
background-color: #fecaca;
transform: scale(1.05);
}

.form-group {
margin-bottom: 15px;
}
Expand Down
Loading
Loading