diff --git a/support-portal-frontend/src/app/component/user/user.component.ts b/support-portal-frontend/src/app/component/user/user.component.ts index ba3bd1e..340ba3f 100644 --- a/support-portal-frontend/src/app/component/user/user.component.ts +++ b/support-portal-frontend/src/app/component/user/user.component.ts @@ -1,24 +1,64 @@ -import {Component, OnInit} from '@angular/core'; -import {BehaviorSubject} from "rxjs"; +import {Component, OnDestroy, OnInit} from '@angular/core'; +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({ selector: 'app-user', templateUrl: './user.component.html', styleUrls: ['./user.component.css'] }) -export class UserComponent implements OnInit { +export class UserComponent implements OnInit, OnDestroy { private titleSubject = new BehaviorSubject('Users'); public titleAction$ = this.titleSubject.asObservable(); - constructor() { + public users: User[] = []; + public refreshing: boolean; + private subscriptions: Subscription[] = []; + + constructor(private userService: UserService, + private notificationService: NotificationService) { } ngOnInit(): void { + this.getUsers(true); + } + + ngOnDestroy(): void { + this.subscriptions.forEach(sub => sub.unsubscribe()); } public changeTitle(title: string): void { 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') + } } diff --git a/support-portal-frontend/src/app/service/user.service.ts b/support-portal-frontend/src/app/service/user.service.ts index 46ebaa8..52dfb9b 100644 --- a/support-portal-frontend/src/app/service/user.service.ts +++ b/support-portal-frontend/src/app/service/user.service.ts @@ -16,7 +16,7 @@ export class UserService { constructor(private httpClient: HttpClient) { } - public getAllUsers(): Observable { + public getAllUsers(): Observable { return this.httpClient .get(`${this.host}/user`); }