feat: make sorting by date work

This commit is contained in:
2025-05-18 20:39:47 +02:00
parent 8ffb7f4eff
commit e0e6fa0573
4 changed files with 81 additions and 34 deletions

View File

@@ -19,12 +19,28 @@ namespace WebApp.Endpoints
// GET /events // GET /events
group.MapGet("/", group.MapGet("/",
async (ApplicationDbContext dbContext, HttpContext httpContext) => async (ApplicationDbContext dbContext, HttpContext httpContext) =>
await dbContext.Events {
.Include(Eve => Eve.Organisation)
.OrderByDescending(Eve => Eve.EventId) var sort = httpContext.Request.Query["sort"].ToString();
.Select(Eve => Eve.ToEventSummaryDto()) //EventSummaryDto IOrderedQueryable<Event> res;
.AsNoTracking() var r = dbContext.Events
.ToListAsync()); .Include(Eve => Eve.Organisation);
if (sort is not null && sort.ToUpper() == "ASC")
{
res = r.OrderBy(Eve => Eve.EventId);
}
else
{
res = r.OrderByDescending(Eve => Eve.EventId);
}
return await res
.Select(Eve => Eve.ToEventSummaryDto()) //EventSummaryDto
.AsNoTracking()
.ToListAsync();
});
// GET /events/1 // GET /events/1
group.MapGet("/{id}", group.MapGet("/{id}",

View File

@@ -1,9 +1,16 @@
document.addEventListener("DOMContentLoaded", async () => { var isAscending: boolean = false;
function toggleListSortOrder() {
isAscending = !isAscending;
loadEvents();
}
async function loadEvents() {
const container = document.getElementById("eventList"); const container = document.getElementById("eventList");
if (!container) return; if (!container) return;
try { try {
const res = await fetch("/api/events"); const res = await fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
if (!res.ok) throw new Error("Błąd pobierania wydarzeń"); if (!res.ok) throw new Error("Błąd pobierania wydarzeń");
const events = await res.json(); const events = await res.json();
@@ -31,4 +38,13 @@
container.innerHTML = `<p class="text-danger">Błąd ładowania danych.</p>`; container.innerHTML = `<p class="text-danger">Błąd ładowania danych.</p>`;
console.error(err); console.error(err);
} }
}
document.addEventListener("DOMContentLoaded", async () => {
loadEvents();
// listen for clicks
const listSortToggleButton = document.getElementById("list-sort-btn");
if (listSortToggleButton) {
listSortToggleButton.addEventListener("click", toggleListSortOrder);
}
}); });

View File

@@ -71,7 +71,7 @@
<button class="btn btn-link" onclick=""> <button class="btn btn-link" onclick="">
<svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#2898BD"><path d="M440-120v-240h80v80h320v80H520v80h-80Zm-320-80v-80h240v80H120Zm160-160v-80H120v-80h160v-80h80v240h-80Zm160-80v-80h400v80H440Zm160-160v-240h80v80h160v80H680v80h-80Zm-480-80v-80h400v80H120Z" /></svg> <svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#2898BD"><path d="M440-120v-240h80v80h320v80H520v80h-80Zm-320-80v-80h240v80H120Zm160-160v-80H120v-80h160v-80h80v240h-80Zm160-80v-80h400v80H440Zm160-160v-240h80v80h160v80H680v80h-80Zm-480-80v-80h400v80H120Z" /></svg>
</button> </button>
<button class="btn btn-link" onclick=""><svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#2898BD"><path d="M320-440v-287L217-624l-57-56 200-200 200 200-57 56-103-103v287h-80ZM600-80 400-280l57-56 103 103v-287h80v287l103-103 57 56L600-80Z" /></svg></button> <button class="btn btn-link" id="list-sort-btn" onclick=""><svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#2898BD"><path d="M320-440v-287L217-624l-57-56 200-200 200 200-57 56-103-103v287h-80ZM600-80 400-280l57-56 103 103v-287h80v287l103-103 57 56L600-80Z" /></svg></button>
</span> </span>
</div> </div>

View File

@@ -8,35 +8,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next()); step((generator = generator.apply(thisArg, _arguments || [])).next());
}); });
}; };
document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, void 0, function* () { var isAscending = false;
const container = document.getElementById("eventList"); function toggleListSortOrder() {
if (!container) isAscending = !isAscending;
return; loadEvents();
try { }
const res = yield fetch("/api/events"); function loadEvents() {
if (!res.ok) return __awaiter(this, void 0, void 0, function* () {
throw new Error("Błąd pobierania wydarzeń"); const container = document.getElementById("eventList");
const events = yield res.json(); if (!container)
if (events.length === 0) {
container.innerHTML = "<p class='text-muted'>Brak wydarzeń do wyświetlenia.</p>";
return; return;
} try {
// Wyczyść kontener przed dodaniem nowych const res = yield fetch("/api/events" + (isAscending ? "?sort=asc" : ""));
container.innerHTML = ''; if (!res.ok)
for (const ev of events) { throw new Error("Błąd pobierania wydarzeń");
const card = document.createElement("div"); const events = yield res.json();
card.className = "event-card filled"; if (events.length === 0) {
card.innerHTML = ` container.innerHTML = "<p class='text-muted'>Brak wydarzeń do wyświetlenia.</p>";
return;
}
// Wyczyść kontener przed dodaniem nowych
container.innerHTML = '';
for (const ev of events) {
const card = document.createElement("div");
card.className = "event-card filled";
card.innerHTML = `
<span>${ev.title}</span> <span>${ev.title}</span>
<button class="remove-btn delete-btn" data-id="${ev.eventId}"> <button class="remove-btn delete-btn" data-id="${ev.eventId}">
<svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#FFFFFF"><path d="M280-440h400v-80H280v80ZM480-80q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" height="30px" viewBox="0 -960 960 960" width="30px" fill="#FFFFFF"><path d="M280-440h400v-80H280v80ZM480-80q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z"/></svg>
</button> </button>
`; `;
container.appendChild(card); container.appendChild(card);
}
} }
} catch (err) {
catch (err) { container.innerHTML = `<p class="text-danger">Błąd ładowania danych.</p>`;
container.innerHTML = `<p class="text-danger">Błąd ładowania danych.</p>`; console.error(err);
console.error(err); }
});
}
document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, void 0, function* () {
loadEvents();
// listen for clicks
const listSortToggleButton = document.getElementById("list-sort-btn");
if (listSortToggleButton) {
listSortToggleButton.addEventListener("click", toggleListSortOrder);
} }
})); }));