153. Register component (#20)
This commit is contained in:
@ -1,23 +1,60 @@
|
|||||||
import {Component, OnInit} from '@angular/core';
|
import {Component, OnDestroy, OnInit} from '@angular/core';
|
||||||
import {User} from "../../model/user";
|
import {User} from "../../model/user";
|
||||||
|
import {Router} from "@angular/router";
|
||||||
|
import {AuthenticationService} from "../../service/authentication.service";
|
||||||
|
import {NotificationService} from "../../service/notification.service";
|
||||||
|
import {Subscription} from "rxjs";
|
||||||
|
import {HttpErrorResponse} from "@angular/common/http";
|
||||||
|
import {NotificationType} from "../../notification/notification-type";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-register',
|
selector: 'app-register',
|
||||||
templateUrl: './register.component.html',
|
templateUrl: './register.component.html',
|
||||||
styleUrls: ['./register.component.css']
|
styleUrls: ['./register.component.css']
|
||||||
})
|
})
|
||||||
export class RegisterComponent implements OnInit {
|
export class RegisterComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
public showLoading: boolean;
|
public showLoading: boolean;
|
||||||
|
private subscriptions: Subscription[] = [];
|
||||||
|
|
||||||
constructor() {
|
constructor(private router: Router,
|
||||||
|
private authenticationService: AuthenticationService,
|
||||||
|
private notificationService: NotificationService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
if (this.authenticationService.isUserLoggedIn()) {
|
||||||
|
this.router.navigateByUrl("/user/management");
|
||||||
|
this.notificationService.notify(NotificationType.INFO, "You are already logged in");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public onRegister(user: User) {
|
public onRegister(user: User) {
|
||||||
|
this.showLoading = true;
|
||||||
|
|
||||||
|
let subscription = this.authenticationService
|
||||||
|
.register(user)
|
||||||
|
.subscribe(user => {
|
||||||
|
this.notificationService.notify(NotificationType.SUCCESS, `A new account was created for ${user.firstName}.
|
||||||
|
Please check your email for password to log in`);
|
||||||
|
this.router.navigateByUrl('/login');
|
||||||
|
this.showLoading = false;
|
||||||
|
},
|
||||||
|
(errorResponse: HttpErrorResponse) => {
|
||||||
|
console.log(errorResponse);
|
||||||
|
this.sendErrorNotification(errorResponse.error.message);
|
||||||
|
this.showLoading = false;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.subscriptions.push(subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.subscriptions.forEach(sub => sub.unsubscribe());
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendErrorNotification(message: string) {
|
||||||
|
this.notificationService.notify(NotificationType.ERROR, message ? message : 'An error occurred. Please try again')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user