159. Fetching users (#21)
This commit is contained in:
@ -1,24 +1,64 @@
|
|||||||
import {Component, OnInit} from '@angular/core';
|
import {Component, OnDestroy, OnInit} from '@angular/core';
|
||||||
import {BehaviorSubject} from "rxjs";
|
import {BehaviorSubject, Subscription} from "rxjs";
|
||||||
|
import {User} from "../../model/user";
|
||||||
|
import {UserService} from "../../service/user.service";
|
||||||
|
import {NotificationService} from "../../service/notification.service";
|
||||||
|
import {NotificationType} from "../../notification/notification-type";
|
||||||
|
import {HttpErrorResponse} from "@angular/common/http";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-user',
|
selector: 'app-user',
|
||||||
templateUrl: './user.component.html',
|
templateUrl: './user.component.html',
|
||||||
styleUrls: ['./user.component.css']
|
styleUrls: ['./user.component.css']
|
||||||
})
|
})
|
||||||
export class UserComponent implements OnInit {
|
export class UserComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
private titleSubject = new BehaviorSubject<string>('Users');
|
private titleSubject = new BehaviorSubject<string>('Users');
|
||||||
public titleAction$ = this.titleSubject.asObservable();
|
public titleAction$ = this.titleSubject.asObservable();
|
||||||
|
|
||||||
constructor() {
|
public users: User[] = [];
|
||||||
|
public refreshing: boolean;
|
||||||
|
private subscriptions: Subscription[] = [];
|
||||||
|
|
||||||
|
constructor(private userService: UserService,
|
||||||
|
private notificationService: NotificationService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
this.getUsers(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.subscriptions.forEach(sub => sub.unsubscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
public changeTitle(title: string): void {
|
public changeTitle(title: string): void {
|
||||||
this.titleSubject.next(title);
|
this.titleSubject.next(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getUsers(showNotification: boolean) {
|
||||||
|
this.refreshing = true;
|
||||||
|
|
||||||
|
let subscription = this.userService.getAllUsers()
|
||||||
|
.subscribe(
|
||||||
|
usersPage => {
|
||||||
|
this.users = usersPage.content;
|
||||||
|
this.userService.addUsersToLocalStorage(this.users);
|
||||||
|
if (showNotification)
|
||||||
|
this.notificationService.notify(NotificationType.SUCCESS, `${this.users.length} users loaded successfully`)
|
||||||
|
},
|
||||||
|
(errorResponse: HttpErrorResponse) => {
|
||||||
|
this.sendErrorNotification(errorResponse.error.message);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
this.refreshing = false;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
this.subscriptions.push(subscription);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendErrorNotification(message: string) {
|
||||||
|
this.notificationService.notify(NotificationType.ERROR, message ? message : 'An error occurred. Please try again')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ export class UserService {
|
|||||||
constructor(private httpClient: HttpClient) {
|
constructor(private httpClient: HttpClient) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public getAllUsers(): Observable<UserPage | HttpErrorResponse> {
|
public getAllUsers(): Observable<UserPage> {
|
||||||
return this.httpClient
|
return this.httpClient
|
||||||
.get<UserPage>(`${this.host}/user`);
|
.get<UserPage>(`${this.host}/user`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user