Skip to content

Commit

Permalink
fix filter
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixTJDietrich committed Oct 3, 2024
1 parent 305b670 commit c4b9271
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
24 changes: 13 additions & 11 deletions webapp/src/app/home/leaderboard/filter/filter.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
<lucide-angular [img]="ListFilter" class="size-4" />
<h3 class="text-base font-semibold tracking-wide">Filter</h3>
</span>
<hlm-select [placeholder]="placeholder()">
<label hlmLabel>Select week</label>
<hlm-select-trigger class="w-56">
<hlm-select-value />
</hlm-select-trigger>
<hlm-select-content class="w-56">
@for (option of options(); track option.id) {
<hlm-option [value]="option.value" (selectChange)="onSelectChange($event)">{{ option.label }}</hlm-option>
}
</hlm-select-content>
</hlm-select>
<form ngForm>
<hlm-select [placeholder]="placeholder()" [(ngModel)]="value" name="value">
<label hlmLabel>Select week</label>
<hlm-select-trigger class="w-56">
<hlm-select-value />
</hlm-select-trigger>
<hlm-select-content class="w-56">
@for (option of options(); track option.id) {
<hlm-option [value]="option.value">{{ option.label }}</hlm-option>
}
</hlm-select-content>
</hlm-select>
</form>
</div>
54 changes: 29 additions & 25 deletions webapp/src/app/home/leaderboard/filter/filter.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,29 +33,30 @@ 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 {
protected ListFilter = ListFilter;
after = input<string>();
before = input<string>();

options = signal<SelectOption[]>([]);
placeholder = signal<string>('Select a timeframe');
value = signal<string>(`${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<SelectOption>();
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');
Expand All @@ -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]
}
});
});
}
}
8 changes: 4 additions & 4 deletions webapp/src/app/home/leaderboard/leaderboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
</thead>
<tbody appTableBody>
@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) {
<tr appTableRow id="skeleton">
<td appTableCell class="flex justify-center">
<hlm-skeleton class="h-5 w-7" [style.width.px]="20 + 1 * idx" />
</td>
<td appTableCell class="py-2">
<span class="flex items-center gap-2">
<hlm-skeleton class="w-10 h-10 rounded-full" />
<hlm-skeleton class="h-5" [style.width.px]="100 + Math.random() * 200" />
<hlm-skeleton class="h-5" [style.width.px]="100 + (idx % 3) * 75" />
</span>
</td>
<td appTableCell class="flex justify-center">
<hlm-skeleton class="h-5" [style.width.px]="20 + (10 - idx) + Math.random() * 10" />
<hlm-skeleton class="h-5" [style.width.px]="20 + (10 - idx) + (idx % 3) * 4" />
</td>
<td appTableCell class="py-2">
<hlm-skeleton class="h-5" [style.width.px]="30 + (Math.random() * 70) / idx" />
<hlm-skeleton class="h-5" [style.width.px]="30 + ((idx % 4) * 20) / idx" />
</td>
</tr>
}
Expand Down

0 comments on commit c4b9271

Please sign in to comment.