public getUsers(): void {
this.isLoading = true;
this.sub.add(
this.adminService.getAdministrators().subscribe((administrators) => {
if (administrators) {
this.administrators = administrators;
this.dataSource.data = administrators.map(user => {
return new Administrator(user);
});
this.isLoading = false
}
})
);
}
public administrators: BehaviorSubject<any> = new BehaviorSubject<any>([]);
public administrators$: Observable<any> = this.administrators.asObservable();
constructor(private http: HttpClient) {}
public getAdministrators(): Observable<IAdministrator[]> {
if (!this.administrators.value.length) {
this.http.get(`${environment.baseUrl}/api/v1/admin/users`).pipe(
tap((response) => {
this.administrators.next(response);
})
).subscribe();
}
return this.administrators.asObservable();
}
export const handler = <T>() => pipe(
map((value: T) => ({ loading: false, value })),
startWith({ loading: true }),
catchError(error => of({ loading: false, error })),
shareReplay({ refCount: true, bufferSize: 1 }),
)
getAdministrators() {
return this.http.get<IAdministrator[]>(`api/v1/admin/users`).pipe(handler())
}