199. Unsubscribe using SubSink library (#29)
This commit is contained in:
@ -10,3 +10,11 @@
|
|||||||
- Generate AppRoutingModule
|
- Generate AppRoutingModule
|
||||||
- `ng generate module app-routing --flat --module=app`
|
- `ng generate module app-routing --flat --module=app`
|
||||||
- Modify `app-routing.module.ts`
|
- Modify `app-routing.module.ts`
|
||||||
|
|
||||||
|
#### Section 29: Security Management - Front End
|
||||||
|
|
||||||
|
##### 199. Unsubscribe using Subsink library
|
||||||
|
|
||||||
|
1. Install [SubSink](https://www.npmjs.com/package/subsink)
|
||||||
|
- `npm install subsink --save`
|
||||||
|
2. Use it
|
||||||
5
support-portal-frontend/package-lock.json
generated
5
support-portal-frontend/package-lock.json
generated
@ -10769,6 +10769,11 @@
|
|||||||
"normalize-path": "^3.0.0"
|
"normalize-path": "^3.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"subsink": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/subsink/-/subsink-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-QFL2oKaA6jVai82dcF0/SIKHNrKJO/wAiHBw9CG576+eBzeg+lZmpG63Tvajx3yg05Hf9ZIZ+zroJutj3hvLNQ=="
|
||||||
|
},
|
||||||
"supports-color": {
|
"supports-color": {
|
||||||
"version": "5.5.0",
|
"version": "5.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
"@auth0/angular-jwt": "^5.0.2",
|
"@auth0/angular-jwt": "^5.0.2",
|
||||||
"angular-notifier": "^9.1.0",
|
"angular-notifier": "^9.1.0",
|
||||||
"rxjs": "~6.6.0",
|
"rxjs": "~6.6.0",
|
||||||
|
"subsink": "^1.0.2",
|
||||||
"tslib": "^2.3.0",
|
"tslib": "^2.3.0",
|
||||||
"zone.js": "~0.11.4"
|
"zone.js": "~0.11.4"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import {Component, OnDestroy, OnInit} from '@angular/core';
|
import {Component, OnDestroy, OnInit} from '@angular/core';
|
||||||
import {BehaviorSubject, Subscription} from "rxjs";
|
import {BehaviorSubject} from "rxjs";
|
||||||
import {User} from "../../model/user";
|
import {User} from "../../model/user";
|
||||||
import {UserService} from "../../service/user.service";
|
import {UserService} from "../../service/user.service";
|
||||||
import {NotificationService} from "../../service/notification.service";
|
import {NotificationService} from "../../service/notification.service";
|
||||||
@ -11,6 +11,7 @@ import {AuthenticationService} from "../../service/authentication.service";
|
|||||||
import {Router} from "@angular/router";
|
import {Router} from "@angular/router";
|
||||||
import {FileUploadStatus} from "../../model/file-upload.status";
|
import {FileUploadStatus} from "../../model/file-upload.status";
|
||||||
import {Role} from "../../enum/role.enum";
|
import {Role} from "../../enum/role.enum";
|
||||||
|
import {SubSink} from "subsink";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-user',
|
selector: 'app-user',
|
||||||
@ -27,7 +28,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
public loggedInUser: User;
|
public loggedInUser: User;
|
||||||
|
|
||||||
public refreshing: boolean;
|
public refreshing: boolean;
|
||||||
private subscriptions: Subscription[] = [];
|
private subs = new SubSink();
|
||||||
public selectedUser: User;
|
public selectedUser: User;
|
||||||
public profileImageFileName: string | null;
|
public profileImageFileName: string | null;
|
||||||
public profileImage: File | null;
|
public profileImage: File | null;
|
||||||
@ -48,7 +49,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.subscriptions.forEach(sub => sub.unsubscribe());
|
this.subs.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
public changeTitle(title: string): void {
|
public changeTitle(title: string): void {
|
||||||
@ -58,7 +59,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
public getUsers(showNotification: boolean) {
|
public getUsers(showNotification: boolean) {
|
||||||
this.refreshing = true;
|
this.refreshing = true;
|
||||||
|
|
||||||
let subscription = this.userService.getAllUsers()
|
this.subs.sink = this.userService.getAllUsers()
|
||||||
.subscribe(
|
.subscribe(
|
||||||
usersPage => {
|
usersPage => {
|
||||||
this.users = usersPage.content;
|
this.users = usersPage.content;
|
||||||
@ -73,7 +74,6 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.refreshing = false;
|
this.refreshing = false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private sendErrorNotification(message: string) {
|
private sendErrorNotification(message: string) {
|
||||||
this, this.sendNotification(NotificationType.ERROR, message);
|
this.sendNotification(NotificationType.ERROR, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private sendNotification(type: NotificationType, message: string) {
|
private sendNotification(type: NotificationType, message: string) {
|
||||||
@ -97,7 +97,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
public onAddNewUser(userForm: NgForm): void {
|
public onAddNewUser(userForm: NgForm): void {
|
||||||
let formData = this.userService.createUserFormData(null, userForm.value, this.profileImage);
|
let formData = this.userService.createUserFormData(null, userForm.value, this.profileImage);
|
||||||
let subscription = this.userService.addUser(formData)
|
this.subs.sink = this.userService.addUser(formData)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(user: User) => {
|
(user: User) => {
|
||||||
this.clickButton('new-user-close');
|
this.clickButton('new-user-close');
|
||||||
@ -110,7 +110,6 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.sendErrorNotification(errorResponse.error.message);
|
this.sendErrorNotification(errorResponse.error.message);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private invalidateVariables(): void {
|
private invalidateVariables(): void {
|
||||||
@ -156,7 +155,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
public onUpdateUser(): void {
|
public onUpdateUser(): void {
|
||||||
const formData = this.userService.createUserFormData(this.currentUsername, this.editUser, this.profileImage);
|
const formData = this.userService.createUserFormData(this.currentUsername, this.editUser, this.profileImage);
|
||||||
let subscription = this.userService.updateUser(formData)
|
this.subs.sink = this.userService.updateUser(formData)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(user: User) => {
|
(user: User) => {
|
||||||
this.clickButton('closeEditUserButton');
|
this.clickButton('closeEditUserButton');
|
||||||
@ -168,11 +167,10 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.sendErrorNotification(errorResponse.error.message);
|
this.sendErrorNotification(errorResponse.error.message);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onDeleteUser(user: User) {
|
onDeleteUser(user: User) {
|
||||||
const subscription = this.userService.deleteUser(user.userId)
|
this.subs.sink = this.userService.deleteUser(user.userId)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(response: CustomHttpResponse) => {
|
(response: CustomHttpResponse) => {
|
||||||
this.getUsers(false);
|
this.getUsers(false);
|
||||||
@ -183,13 +181,12 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.sendErrorNotification(errorResponse.error.message);
|
this.sendErrorNotification(errorResponse.error.message);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onResetPassword(emailForm: NgForm): void {
|
public onResetPassword(emailForm: NgForm): void {
|
||||||
this.refreshing = true;
|
this.refreshing = true;
|
||||||
let email = emailForm.value['reset-password-email'];
|
let email = emailForm.value['reset-password-email'];
|
||||||
let subscription = this.userService.resetPassword(email)
|
this.subs.sink = this.userService.resetPassword(email)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(response: CustomHttpResponse) => {
|
(response: CustomHttpResponse) => {
|
||||||
this.notificationService.notify(NotificationType.SUCCESS, response.message);
|
this.notificationService.notify(NotificationType.SUCCESS, response.message);
|
||||||
@ -203,14 +200,13 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
emailForm.reset();
|
emailForm.reset();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdateCurrentUser(user: User) {
|
onUpdateCurrentUser(user: User) {
|
||||||
this.currentUsername = this.authenticationService.getUserFromLocalStorage().username;
|
this.currentUsername = this.authenticationService.getUserFromLocalStorage().username;
|
||||||
this.refreshing = true;
|
this.refreshing = true;
|
||||||
const formData = this.userService.createUserFormData(this.currentUsername, user, this.profileImage);
|
const formData = this.userService.createUserFormData(this.currentUsername, user, this.profileImage);
|
||||||
let subscription = this.userService.updateUser(formData)
|
this.subs.sink = this.userService.updateUser(formData)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(user: User) => {
|
(user: User) => {
|
||||||
this.authenticationService.addUserToLocalStorage(user);
|
this.authenticationService.addUserToLocalStorage(user);
|
||||||
@ -224,7 +220,6 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.refreshing = false;
|
this.refreshing = false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onLogOut() {
|
onLogOut() {
|
||||||
@ -243,7 +238,7 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("profileImage", this.profileImage);
|
formData.append("profileImage", this.profileImage);
|
||||||
let user = this.authenticationService.getUserFromLocalStorage();
|
let user = this.authenticationService.getUserFromLocalStorage();
|
||||||
let subscription = this.userService.updateProfileImage(user.username, formData)
|
this.subs.sink = this.userService.updateProfileImage(user.username, formData)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(event: HttpEvent<any>) => {
|
(event: HttpEvent<any>) => {
|
||||||
this.reportUploadProgress(event);
|
this.reportUploadProgress(event);
|
||||||
@ -258,7 +253,6 @@ export class UserComponent implements OnInit, OnDestroy {
|
|||||||
this.getUsers(false);
|
this.getUsers(false);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.subscriptions.push(subscription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private reportUploadProgress(event: HttpEvent<any>): void {
|
private reportUploadProgress(event: HttpEvent<any>): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user