Added some functionality and some new data to mock-api
This commit is contained in:
committed by
Erik Tiekstra
parent
cfe1907842
commit
c03b66c6bc
@@ -10,26 +10,51 @@ import { map } from 'rxjs/operators';
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ParticipantsService {
|
||||
private _participants$: Observable<Participant[]> = this.httpClient.get<Participant[]>(
|
||||
`${environment.apiBase}/participants`
|
||||
);
|
||||
private _sortBy$ = new BehaviorSubject<SortBy | null>({ key: 'service', reverse: false });
|
||||
private _participants$: Observable<Participant[]> = this.httpClient
|
||||
.get<Participant[]>(`${environment.apiBase}/participants`)
|
||||
.pipe(
|
||||
map(participants =>
|
||||
participants.map(participant => ({
|
||||
...participant,
|
||||
fullName: `${participant.firstName} ${participant.lastName}`,
|
||||
}))
|
||||
)
|
||||
);
|
||||
private _sortBy$ = new BehaviorSubject<SortBy | null>({ key: 'handleBefore', reverse: false });
|
||||
public sortBy$: Observable<SortBy> = this._sortBy$.asObservable();
|
||||
private _searchFilter$ = new BehaviorSubject<string>('');
|
||||
public searchFilter$: Observable<string> = this._searchFilter$.asObservable();
|
||||
|
||||
public participants$: Observable<Participant[]> = combineLatest([
|
||||
this._sortBy$,
|
||||
this._participants$,
|
||||
this._searchFilter$,
|
||||
]).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);
|
||||
|
||||
return searchValueExistsInName || searchValueExistsInErrandNumber;
|
||||
});
|
||||
|
||||
public participants$: Observable<Participant[]> = combineLatest([this._sortBy$, this._participants$]).pipe(
|
||||
map(([sortBy, participants]) => {
|
||||
if (sortBy) {
|
||||
const reverse = sortBy.reverse ? -1 : 1;
|
||||
return [...participants].sort((a, b) => {
|
||||
return [...filteredParticipants].sort((a, b) => {
|
||||
const first = a[sortBy.key];
|
||||
const second = b[sortBy.key];
|
||||
return reverse * +(first > second) - +(second > first);
|
||||
|
||||
return reverse * (+(first > second) - +(second > first));
|
||||
});
|
||||
}
|
||||
return participants;
|
||||
})
|
||||
);
|
||||
|
||||
public setSearchFilter(value: string) {
|
||||
this._searchFilter$.next(value);
|
||||
}
|
||||
|
||||
public setSortKey(key: keyof Participant) {
|
||||
const currentSortBy = this._sortBy$.getValue();
|
||||
let reverse = false;
|
||||
|
||||
Reference in New Issue
Block a user