Forked from
TDT4140-Group 58 / Spring2021-SellPoint
104 commits behind the upstream repository.
-
Jonny Ngo Luong authoredJonny Ngo Luong authored
user-registration-form.component.ts 1.72 KiB
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { User } from 'src/app/models/user.model';
import { UserService } from '../user.service';
@Component({
selector: 'app-user-registration-form',
templateUrl: './user-registration-form.component.html',
styleUrls: ['./user-registration-form.component.scss']
})
export class UserRegistrationFormComponent implements OnInit {
username: string = "";
email: string = "";
password: string = "";
statusMessage: string = "";
constructor(private userService: UserService, private router: Router) { }
ngOnInit(): void {
}
/**
* Validates form.
*/
checkForm(): boolean {
if (this.username == "") {
this.setStatusMessage("Brukernavn kan ikke være tom");
return false;
}
else if (this.email == "") {
this.setStatusMessage("Eposten kan ikke være tom");
return false;
}
else if (this.password == "") {
this.setStatusMessage("Passordet kan ikke være tom");
return false;
}
this.setStatusMessage("");
return true;
}
/**
* Publishes user if it is valid.
*/
registerUser() {
if (this.checkForm()) {
const newUser = new User({
username: this.username,
email: this.email,
password: this.password,
});
// Adds user to database and changes page afterwards
this.userService.addUser(newUser).then(status => {
console.log("User was added: " + status);
this.router.navigateByUrl("/");
}).catch(error => {
console.log("Error adding user: " + error);
});
}
}
/**
* Sets a status message.
*/
setStatusMessage(message: string) {
this.statusMessage = message;
}
}