From c4b9271243dd7fb2aa987c63fe6d3857f44e7174 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 4 Oct 2024 00:25:01 +0200 Subject: [PATCH] fix filter --- .../leaderboard/filter/filter.component.html | 24 +++++---- .../leaderboard/filter/filter.component.ts | 54 ++++++++++--------- .../leaderboard/leaderboard.component.html | 8 +-- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/webapp/src/app/home/leaderboard/filter/filter.component.html b/webapp/src/app/home/leaderboard/filter/filter.component.html index c449c817..e0acb284 100644 --- a/webapp/src/app/home/leaderboard/filter/filter.component.html +++ b/webapp/src/app/home/leaderboard/filter/filter.component.html @@ -3,15 +3,17 @@

Filter

- - - - - - - @for (option of options(); track option.id) { - {{ option.label }} - } - - +
+ + + + + + + @for (option of options(); track option.id) { + {{ option.label }} + } + + +
diff --git a/webapp/src/app/home/leaderboard/filter/filter.component.ts b/webapp/src/app/home/leaderboard/filter/filter.component.ts index 6c8b5d48..255205eb 100644 --- a/webapp/src/app/home/leaderboard/filter/filter.component.ts +++ b/webapp/src/app/home/leaderboard/filter/filter.component.ts @@ -1,4 +1,5 @@ -import { Component, input, signal } from '@angular/core'; +import { Component, computed, effect, input, signal } from '@angular/core'; +import { FormsModule } from '@angular/forms'; import { Router, RouterLink } from '@angular/router'; import dayjs from 'dayjs'; import weekOfYear from 'dayjs/plugin/weekOfYear'; @@ -32,7 +33,7 @@ function formatLabel(startDate: dayjs.Dayjs, endDate: dayjs.Dayjs | undefined) { @Component({ selector: 'app-leaderboard-filter', standalone: true, - imports: [RouterLink, LucideAngularModule, BrnSelectModule, HlmSelectModule, HlmLabelModule], + imports: [RouterLink, LucideAngularModule, BrnSelectModule, HlmSelectModule, HlmLabelModule, FormsModule], templateUrl: './filter.component.html' }) export class LeaderboardFilterComponent { @@ -40,21 +41,22 @@ export class LeaderboardFilterComponent { after = input(); before = input(); - options = signal([]); - placeholder = signal('Select a timeframe'); + value = signal(`${this.after() ?? dayjs().day(1).format('YYYY-MM-DD')}.${this.before() ?? dayjs().format('YYYY-MM-DD')}`); - constructor(private router: Router) { - // get monday - sunday of last 4 weeks - const options = new Array(); + placeholder = computed(() => { + return formatLabel(dayjs(this.after()) ?? dayjs().day(1), this.before() === undefined ? undefined : dayjs(this.before())); + }); + + options = computed(() => { const now = dayjs(); let currentDate = dayjs().day(1); - options.push({ - id: now.unix(), - value: `${currentDate.format('YYYY-MM-DD')}.${now.format('YYYY-MM-DD')}`, - label: formatLabel(currentDate, undefined) - }); - - this.placeholder.set(formatLabel(currentDate, undefined)); + const options: SelectOption[] = [ + { + id: now.unix(), + value: `${currentDate.format('YYYY-MM-DD')}.${now.format('YYYY-MM-DD')}`, + label: formatLabel(currentDate, undefined) + } + ]; for (let i = 0; i < 4; i++) { const startDate = currentDate.subtract(7, 'day'); @@ -66,18 +68,20 @@ export class LeaderboardFilterComponent { }); currentDate = startDate; } - this.options.set(options); - } - onSelectChange(event: Event) { - const value = (event.target as HTMLSelectElement).value; - const dates = value.split('.'); - // change query params - this.router.navigate([], { - queryParams: { - after: dates[0], - before: dates[1] - } + return options; + }); + + constructor(private router: Router) { + effect(() => { + const dates = this.value().split('.'); + // change query params + this.router.navigate([], { + queryParams: { + after: dates[0], + before: dates[1] + } + }); }); } } diff --git a/webapp/src/app/home/leaderboard/leaderboard.component.html b/webapp/src/app/home/leaderboard/leaderboard.component.html index 98e8b9de..3318e303 100644 --- a/webapp/src/app/home/leaderboard/leaderboard.component.html +++ b/webapp/src/app/home/leaderboard/leaderboard.component.html @@ -9,7 +9,7 @@ @if (isLoading()) { - @for (entry of Array(10); track entry; let idx = $index) { + @for (entry of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; track entry; let idx = $index) { @@ -17,14 +17,14 @@ - + - + - + }