Added date and category filtering
This commit is contained in:
1 parent
606558a5eb
commit
ed637b5ba7
6 files changed
+118
-36
No files matched your search
@@ -1,13 +1,22 @@
|
||||
import { fetchEvents } from "./api/ticketmaster.js";
|
||||
import { createEventCard } from "./ui/eventCard.js";
|
||||
import { renderEventList } from "./ui/eventList.js";
|
||||
import { getFilters } from "./ui/filters.js";
|
||||
|
||||
const button = document.querySelector("#load-events");
|
||||
const container = document.querySelector("#event-list");
|
||||
const cityInput = document.querySelector("#city-input");
|
||||
|
||||
button.addEventListener("click", async () => {
|
||||
button.addEventListener("click", handleSearch);
|
||||
|
||||
const city = cityInput.value.trim();
|
||||
cityInput.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter") {
|
||||
handleSearch();
|
||||
}
|
||||
});
|
||||
|
||||
async function handleSearch() {
|
||||
|
||||
const { city, dateFrom, dateTo, category } = getFilters();
|
||||
|
||||
if (!city) {
|
||||
container.innerHTML = "Please enter a city.";
|
||||
@@ -18,22 +27,28 @@ button.addEventListener("click", async () => {
|
||||
|
||||
const events = await fetchEvents(city);
|
||||
|
||||
container.innerHTML = "";
|
||||
const filteredEvents = applyFilters(events, dateFrom, dateTo, category);
|
||||
|
||||
if (events.length === 0) {
|
||||
container.innerHTML = "No events found.";
|
||||
return;
|
||||
}
|
||||
renderEventList(filteredEvents, container);
|
||||
}
|
||||
|
||||
events.forEach(event => {
|
||||
const card = createEventCard(event);
|
||||
container.appendChild(card);
|
||||
function applyFilters(events, dateFrom, dateTo, category) {
|
||||
|
||||
return events.filter(event => {
|
||||
|
||||
const eventDate = event.dates.start.localDate;
|
||||
|
||||
const eventCategory =
|
||||
event.classifications?.[0]?.segment?.name?.toLowerCase();
|
||||
|
||||
const matchDateFrom = dateFrom ? eventDate >= dateFrom : true;
|
||||
const matchDateTo = dateTo ? eventDate <= dateTo : true;
|
||||
|
||||
const matchCategory = category
|
||||
? eventCategory === category
|
||||
: true;
|
||||
|
||||
return matchDateFrom && matchDateTo && matchCategory;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
cityInput.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter") {
|
||||
button.click();
|
||||
}
|
||||
});
|
||||
}
|
||||
+36
-15
@@ -1,17 +1,38 @@
|
||||
export function createEventCard(event) {
|
||||
|
||||
const article = document.createElement("article");
|
||||
article.className = "event-card";
|
||||
|
||||
const name = event.name;
|
||||
const date = event.dates.start.localDate;
|
||||
const venue = event._embedded?.venues[0]?.name;
|
||||
|
||||
article.innerHTML = `
|
||||
<h3 class="event-card__title">${name}</h3>
|
||||
<p class="event-card__date">${date}</p>
|
||||
<p class="event-card__venue">${venue}</p>
|
||||
`;
|
||||
|
||||
return article;
|
||||
}
|
||||
const article = document.createElement("article");
|
||||
article.className = "event-card";
|
||||
|
||||
const name = event.name;
|
||||
|
||||
// DATE
|
||||
const rawDate = event.dates?.start?.localDate;
|
||||
const formattedDate = rawDate
|
||||
? new Date(rawDate).toLocaleDateString("de-CH", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric"
|
||||
})
|
||||
: "Date not available";
|
||||
|
||||
// TIME
|
||||
const rawTime = event.dates?.start?.localTime;
|
||||
const formattedTime = rawTime
|
||||
? rawTime.slice(0, 5)
|
||||
: "";
|
||||
|
||||
// Combine date + time
|
||||
const dateTime = formattedTime
|
||||
? `${formattedDate}, ${formattedTime}`
|
||||
: formattedDate;
|
||||
|
||||
const venue = event._embedded?.venues[0]?.name || "Unknown venue";
|
||||
|
||||
article.innerHTML = `
|
||||
<h3 class="event-card__title">${name}</h3>
|
||||
<p class="event-card__date">${dateTime}</p>
|
||||
<p class="event-card__venue">${venue}</p>
|
||||
`;
|
||||
|
||||
return article;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { createEventCard } from "./eventCard.js";
|
||||
|
||||
export function renderEventList(events, container) {
|
||||
|
||||
container.innerHTML = "";
|
||||
|
||||
if (events.length === 0) {
|
||||
container.innerHTML = "No events found.";
|
||||
return;
|
||||
}
|
||||
|
||||
events.forEach(event => {
|
||||
const card = createEventCard(event);
|
||||
container.appendChild(card);
|
||||
});
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export function getFilters() {
|
||||
const city = document.querySelector("#city-input").value.trim();
|
||||
const dateFrom = document.querySelector("#date-from").value;
|
||||
const dateTo = document.querySelector("#date-to").value;
|
||||
const category = document.querySelector("#category-input").value;
|
||||
|
||||
return { city, dateFrom, dateTo, category };
|
||||
}
|
||||
Reference in new issue
Block a user