Added some filtering to participant component

This commit is contained in:
Erik Tiekstra
2021-03-29 12:51:49 +02:00
committed by Erik Tiekstra
parent 99db911f39
commit 8a9b0049d4
5 changed files with 78 additions and 37 deletions

View File

@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { Participant } from '@dafa-models/participant.model';
import { SortBy } from '@dafa-models/sort-by.model';
import { ParticipantsService } from '@dafa-services/api/participants.service';
@@ -12,10 +12,11 @@ import { ParticipantsService } from '@dafa-services/api/participants.service';
export class ParticipantsListComponent {
@Input() participants: Participant[];
@Input() sortBy: SortBy | null;
@Output() sorted = new EventEmitter<keyof Participant>();
constructor(private participantsService: ParticipantsService) {}
handleSort(key: keyof Participant): void {
this.participantsService.setSortKey(key);
this.sorted.emit(key);
}
}

View File

@@ -17,9 +17,17 @@
</form>
<dafa-participants-list
*ngIf="participants$ | async as participants; else loadingRef"
*ngIf="activeParticipants$ | async as participants; else loadingRef"
[participants]="participants"
[sortBy]="sortBy$ | async"
[sortBy]="activeParticipantsSortBy$ | async"
(sorted)="handleActiveParticipantsSort($event)"
></dafa-participants-list>
<dafa-participants-list
*ngIf="followUpParticipants$ | async as participants; else loadingRef"
[participants]="participants"
[sortBy]="followUpParticipantsSortBy$ | async"
(sorted)="handleFollowUpParticipantsSort($event)"
></dafa-participants-list>
<ng-template #loadingRef>

View File

@@ -12,8 +12,10 @@ import { BehaviorSubject, Observable } from 'rxjs';
})
export class ParticipantsComponent {
private _searchValue$ = new BehaviorSubject<string>('');
participants$: Observable<Participant[]> = this.participantsService.participants$;
sortBy$: Observable<SortBy | null> = this.participantsService.sortBy$;
activeParticipants$: Observable<Participant[]> = this.participantsService.activeParticipants$;
followUpParticipants$: Observable<Participant[]> = this.participantsService.followUpParticipants$;
activeParticipantsSortBy$: Observable<SortBy | null> = this.participantsService.activeParticipantsSortBy$;
followUpParticipantsSortBy$: Observable<SortBy | null> = this.participantsService.followUpParticipantsSortBy$;
constructor(private participantsService: ParticipantsService) {}
@@ -28,4 +30,12 @@ export class ParticipantsComponent {
handleSearchInput($event: CustomEvent): void {
this._searchValue$.next($event.detail.target.value);
}
handleActiveParticipantsSort(key: keyof Participant): void {
this.participantsService.setActiveParticipantsSortKey(key);
}
handleFollowUpParticipantsSort(key: keyof Participant): void {
this.participantsService.setFollowUpParticipantsSortKey(key);
}
}

View File

@@ -3,14 +3,24 @@ import { Injectable } from '@angular/core';
import { environment } from '@dafa-environment';
import { Participant } from '@dafa-models/participant.model';
import { SortBy } from '@dafa-models/sort-by.model';
import { sort } from '@dafa-utils/sort.util';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
function filterParticipants(participants: Participant[], searchFilter: string): Participant[] {
return participants.filter(participant => {
const searchValueExistsInName = participant.fullName.toLowerCase().includes(searchFilter.toLowerCase());
const searchValueExistsInErrandNumber = participant.errandNumber.toString().includes(searchFilter);
return searchValueExistsInName || searchValueExistsInErrandNumber;
});
}
@Injectable({
providedIn: 'root',
})
export class ParticipantsService {
private _participants$: Observable<Participant[]> = this.httpClient
private _allParticipants$: Observable<Participant[]> = this.httpClient
.get<Participant[]>(`${environment.apiBase}/participants`)
.pipe(
map(participants =>
@@ -20,34 +30,33 @@ export class ParticipantsService {
}))
)
);
private _sortBy$ = new BehaviorSubject<SortBy | null>({ key: 'handleBefore', reverse: false });
public sortBy$: Observable<SortBy> = this._sortBy$.asObservable();
private _activeParticipantsSortBy$ = new BehaviorSubject<SortBy | null>({ key: 'handleBefore', reverse: false });
public activeParticipantsSortBy$: Observable<SortBy> = this._activeParticipantsSortBy$.asObservable();
private _followUpParticipantsSortBy$ = new BehaviorSubject<SortBy | null>({ key: 'handleBefore', reverse: false });
public followUpParticipantsSortBy$: Observable<SortBy> = this._followUpParticipantsSortBy$.asObservable();
private _searchFilter$ = new BehaviorSubject<string>('');
public searchFilter$: Observable<string> = this._searchFilter$.asObservable();
public participants$: Observable<Participant[]> = combineLatest([
this._sortBy$,
this._participants$,
public filteredParticipants$: Observable<Participant[]> = combineLatest([
this._allParticipants$,
this._searchFilter$,
]).pipe(map(([participants, searchFilter]) => filterParticipants(participants, searchFilter)));
public activeParticipants$: Observable<Participant[]> = combineLatest([
this.filteredParticipants$,
this._activeParticipantsSortBy$,
]).pipe(
map(([sortBy, participants, searchFilter]) => {
const filteredParticipants = participants.filter(participant => {
const searchValueExistsInName = participant.fullName.toLowerCase().includes(searchFilter.toLowerCase());
const searchValueExistsInErrandNumber = participant.errandNumber.toString().includes(searchFilter);
map(([participants, sortBy]) => {
return sortBy ? sort(participants, sortBy) : participants;
})
);
return searchValueExistsInName || searchValueExistsInErrandNumber;
});
if (sortBy) {
const reverse = sortBy.reverse ? -1 : 1;
return [...filteredParticipants].sort((a, b) => {
const first = a[sortBy.key];
const second = b[sortBy.key];
return reverse * (+(first > second) - +(second > first));
});
}
return participants;
public followUpParticipants$: Observable<Participant[]> = combineLatest([
this.filteredParticipants$,
this._followUpParticipantsSortBy$,
]).pipe(
map(([participants, sortBy]) => {
return sortBy ? sort(participants, sortBy) : participants;
})
);
@@ -55,15 +64,16 @@ export class ParticipantsService {
this._searchFilter$.next(value);
}
public setSortKey(key: keyof Participant) {
const currentSortBy = this._sortBy$.getValue();
let reverse = false;
public setActiveParticipantsSortKey(key: keyof Participant) {
const currentSortBy = this._activeParticipantsSortBy$.getValue();
const reverse = currentSortBy?.key === key ? !currentSortBy.reverse : false;
this._activeParticipantsSortBy$.next({ key, reverse });
}
if (currentSortBy?.key === key) {
reverse = !currentSortBy.reverse;
}
this._sortBy$.next({ key, reverse });
public setFollowUpParticipantsSortKey(key: keyof Participant) {
const currentSortBy = this._followUpParticipantsSortBy$.getValue();
const reverse = currentSortBy?.key === key ? !currentSortBy.reverse : false;
this._followUpParticipantsSortBy$.next({ key, reverse });
}
constructor(private httpClient: HttpClient) {}

View File

@@ -0,0 +1,12 @@
import { SortBy } from '@dafa-models/sort-by.model';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function sort(data: any[], sortBy: SortBy): any[] {
const reverse = sortBy.reverse ? -1 : 1;
return [...data].sort((a, b) => {
const first = a[sortBy.key];
const second = b[sortBy.key];
return reverse * (+(first > second) - +(second > first));
});
}