Last active
October 14, 2019 15:53
-
-
Save troelslenda/92b42bbe166ee0bd038c0507312d9740 to your computer and use it in GitHub Desktop.
MitDGI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import { environment } from '../environments/environment'; | |
import { of, fromEvent, merge, iif } from 'rxjs'; | |
import { filter, flatMap, switchMap, catchError } from "rxjs/operators"; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class PersonService { | |
constructor(private http: HttpClient) { } | |
public get person() { | |
// Let Observable fire once and then fromEvent. | |
return merge( | |
of(true), | |
fromEvent(document.querySelector('dgi-person-selector'), 'personSelected') | |
).pipe( | |
// Replace of, FromEvent Observable with a Person Observable | |
switchMap(_ => this.getPerson()) | |
); | |
} | |
getPerson = () => iif( | |
// If selectedPerson is persisted in localstorage, load data and | |
// filter getPersons endpoint. | |
// Otherwise - load data from getCurrentPerson endpoint. | |
() => !!localStorage.getItem('selectedPerson'), | |
this.getPersons().pipe( | |
flatMap(x => Object.values(x)), | |
filter(item => item.MimerId == localStorage.getItem('selectedPerson')), | |
), | |
this.http.get(`${environment.umbracoApi}/Person/GetCurrentPerson`) | |
.pipe( | |
catchError(_ => of({ error: 'You are probably not authorized' })))); | |
getPersons = () => this.http.get(`${environment.umbracoApi}/Person/GetPersons`) | |
.pipe( | |
catchError(_ => of({ error: 'You are probably not authorized' }))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment