Files
mina-sidor-fa-web/apps/dafa-web/src/app/services/api/participants.service.ts
Erik Tiekstra f7081d84b5 feat(participant-page): Added routing and page for single participants. (TV-267)
Squashed commit of the following:

commit e59f80b44e8169fb1a02c505b261b38bef2f0913
Merge: ecf97a3 88c68e1
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Fri Jun 18 12:10:45 2021 +0200

    Merged develop and resolved conflicts

commit ecf97a3fe6ad78b6250b46cdaec5169ee4106df8
Merge: 10bc25e ba34b20
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Jun 15 15:44:41 2021 +0200

    Merge branch 'develop' into feature/TV-267-participant-page

commit 10bc25e77822724c469e8002fb00653eab7bf938
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Jun 15 15:42:18 2021 +0200

    Added back-link

commit a45228d0adfa20eb4ee5407c0bc6fea9ea07b8f1
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Jun 15 14:53:54 2021 +0200

    Added page to routing and fetched some data
2021-06-18 12:11:42 +02:00

110 lines
4.3 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ParticipantStatus } from '@dafa-enums/participant-status.enum';
import { SortOrder } from '@dafa-enums/sort-order.enum';
import { environment } from '@dafa-environment';
import {
mapParticipantApiResponseToParticipant,
Participant,
ParticipantApiResponse,
ParticipantsApiResponse,
} from '@dafa-models/participant.model';
import { Sort } from '@dafa-models/sort.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;
});
}
const API_HEADERS = { headers: environment.api.headers };
@Injectable({
providedIn: 'root',
})
export class ParticipantsService {
private _apiUrl = `${environment.api.url}/participants`;
private _allParticipants$: Observable<Participant[]> = this.httpClient
.get<ParticipantsApiResponse>(this._apiUrl)
.pipe(map(response => response.data.map(participant => mapParticipantApiResponseToParticipant(participant))));
private _activeParticipantsSortBy$ = new BehaviorSubject<Sort<keyof Participant> | null>({
key: 'handleBefore',
order: SortOrder.ASC,
});
public activeParticipantsSortBy$: Observable<
Sort<keyof Participant>
> = this._activeParticipantsSortBy$.asObservable();
private _followUpParticipantsSortBy$ = new BehaviorSubject<Sort<keyof Participant> | null>({
key: 'handleBefore',
order: SortOrder.ASC,
});
public followUpParticipantsSortBy$: Observable<
Sort<keyof Participant>
> = this._followUpParticipantsSortBy$.asObservable();
private _searchFilter$ = new BehaviorSubject<string>('');
public searchFilter$: Observable<string> = this._searchFilter$.asObservable();
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(([participants, sortBy]) => {
const activeParticipants = participants.filter(participant => participant.status === ParticipantStatus.ACTIVE);
return sortBy ? sort(activeParticipants, sortBy) : activeParticipants;
})
);
public followUpParticipants$: Observable<Participant[]> = combineLatest([
this.filteredParticipants$,
this._followUpParticipantsSortBy$,
]).pipe(
map(([participants, sortBy]) => {
const followUpParticipants = participants.filter(
participant => participant.status === ParticipantStatus.FOLLOW_UP
);
return sortBy ? sort(followUpParticipants, sortBy) : followUpParticipants;
})
);
public fetchDetailedParticipantData$(id: string): Observable<Participant> {
return this.httpClient
.get<ParticipantApiResponse>(`${this._apiUrl}/${id}`, { ...API_HEADERS })
.pipe(map(result => mapParticipantApiResponseToParticipant(result.data)));
}
public setSearchFilter(value: string) {
this._searchFilter$.next(value);
}
public setActiveParticipantsSortKey(key: keyof Participant) {
const currentSortBy = this._activeParticipantsSortBy$.getValue();
let order = currentSortBy.order;
if (currentSortBy?.key === key) {
order = order === SortOrder.ASC ? SortOrder.DESC : SortOrder.ASC;
}
this._activeParticipantsSortBy$.next({ key, order });
}
public setFollowUpParticipantsSortKey(key: keyof Participant) {
const currentSortBy = this._followUpParticipantsSortBy$.getValue();
let order = currentSortBy.order;
if (currentSortBy?.key === key) {
order = order === SortOrder.ASC ? SortOrder.DESC : SortOrder.ASC;
}
this._followUpParticipantsSortBy$.next({ key, order });
}
constructor(private httpClient: HttpClient) {}
}