import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import {Role, User} from 'src/app/models/models'; import { UserService } from 'src/app/_services/user-service/user.service'; @Component({ selector: 'app-update-user', templateUrl: './update-user.component.html', styleUrls: ['./update-user.component.scss'] }) export class UpdateUserComponent implements OnInit { id: number; roleRequest:string; user: User; roles = [ { name: 'ROLE_USER', value: 'ROLE_USER', checked: false, }, { name: 'ROLE_ADMIN', value: 'ROLE_ADMIN', checked: false, }, ]; constructor(private userService: UserService, private router: Router, private route: ActivatedRoute) { } ngOnInit(): void { this.id = this.route.snapshot.params['id']; this.roleRequest = this.route.snapshot.params['role']; console.log("id, ",this.id,"role",this.roleRequest); this.userService.getUserById(this.id).subscribe( data => { this.user = data; }, error => console.log(error)); } onSubmit() { this.userService.updateUser(this.id, this.user).subscribe(data => { this.goToUserList(); } , error => console.log(error)); } goToUserList() { if(this.isAdminRequest()){ this.router.navigate(['/admin']); }else { this.router.navigate(['/user']); } } get selectedOptions() { return this.roles.filter((opt) => opt.checked).map((opt) => opt.value); } updateRole(role: string) { console.log(role); this.user.roles = new Array(new Role(role)); console.log(this.user.roles); } isAdminRequest() { return this.roleRequest === 'ROLE_ADMIN'; } }