11import { Component , OnInit , ViewChild , ElementRef } from '@angular/core' ;
2- import { FormGroup , FormControl , Validators } from '@angular/forms' ;
2+ import { FormGroup , FormControl , Validators , FormBuilder } from '@angular/forms' ;
33import { Observable } from 'rxjs' ;
4- import { map , startWith , filter } from 'rxjs/operators' ;
4+ import { map , startWith } from 'rxjs/operators' ;
55import { FormCustomValidators } from './validators/FormCustomValidators' ;
66import { UserService } from './service/user.service' ;
77import { UserDTO } from './model/UserDTO' ;
@@ -22,16 +22,17 @@ export class AppComponent implements OnInit {
2222 @ViewChild ( MatAutocompleteTrigger ) trigger : MatAutocompleteTrigger ;
2323 @ViewChild ( 'focusMe' ) _focusMe : ElementRef ;
2424
25- user : UserDTO ;
25+ user : string ;
2626 users : Array < UserDTO > ;
2727 filteredUsersOptions : Observable < UserDTO [ ] > ;
2828
29- album : AlbumDTO ;
29+ album : string ;
3030 albums : Array < AlbumDTO > ;
3131 filteredAlbumsOptions : Observable < AlbumDTO [ ] > ;
3232
3333 constructor ( private userService : UserService ,
34- private albumService : AlbumService ) {
34+ private albumService : AlbumService ,
35+ private fb : FormBuilder ) {
3536
3637 this . users = new Array < UserDTO > ( ) ;
3738 this . albums = new Array < AlbumDTO > ( ) ;
@@ -43,19 +44,17 @@ export class AppComponent implements OnInit {
4344 }
4445
4546 private initForm ( ) : void {
46- this . form = new FormGroup ( {
47+ this . form = this . fb . group ( {
4748 formUserControl : new FormControl ( '' , [ Validators . required ] ) ,
4849 formAlbumsControl : new FormControl ( { value : '' , disabled : true } , [ Validators . required ] )
4950 } ) ;
5051
5152 this . filteredUsersOptions = this . form . get ( 'formUserControl' ) . valueChanges . pipe (
5253 startWith ( '' ) ,
53- filter ( value => typeof value === 'string' ) ,
5454 map ( value => this . _filterUsers ( value ) )
5555 ) ;
5656 this . filteredAlbumsOptions = this . form . get ( 'formAlbumsControl' ) . valueChanges . pipe (
5757 startWith ( '' ) ,
58- filter ( value => typeof value === 'string' ) ,
5958 map ( value => this . _filterAlbums ( value ) )
6059 ) ;
6160 }
@@ -71,7 +70,8 @@ export class AppComponent implements OnInit {
7170 }
7271
7372 private addValidUserSelected ( ) {
74- this . form . get ( 'formUserControl' ) . setValidators ( FormCustomValidators . valueSelected ( this . users ) ) ;
73+ const userNames = this . users . map ( user => user . name ) ;
74+ this . form . get ( 'formUserControl' ) . setValidators ( FormCustomValidators . valueSelected ( userNames ) ) ;
7575 }
7676
7777 private addValidAlbumsSelected ( ) {
@@ -89,13 +89,19 @@ export class AppComponent implements OnInit {
8989 ) ;
9090 }
9191
92+ private getUserIdFromUserName ( userName : string ) : number {
93+ return this . users . find ( user => user . name === userName ) . id ;
94+ }
95+
9296 private verificaSeDeveHabilitarFormAlbumsControl ( ) : void {
9397 const formAlbumsControl = this . form . get ( 'formAlbumsControl' ) ;
9498 this . albums . length > 0 ? formAlbumsControl . enable ( ) : formAlbumsControl . disable ( ) ;
9599 }
96100
97- getAlbumsByUser ( userId : number ) {
98- console . log ( 'User Id: ' , userId ) ;
101+ getAlbumsByUser ( userName : string ) {
102+ console . log ( 'User Name: ' , userName ) ;
103+ const userId = this . getUserIdFromUserName ( userName ) ;
104+ console . log ( 'User id: ' , userId ) ;
99105 this . albumService . getAlbumsFromUser ( userId ) . subscribe ( resp => {
100106 this . albums = resp ;
101107 this . addValidAlbumsSelected ( ) ;
@@ -106,8 +112,9 @@ export class AppComponent implements OnInit {
106112 } ) ;
107113 }
108114
109- getUser ( ) : void {
110- console . log ( "Usuario Selecionado: " , this . user ) ;
115+ save ( ) : void {
116+ console . log ( 'Usuario Selecionado: ' , this . user ) ;
117+ console . log ( 'Album Selecionado: ' , this . album ) ;
111118 }
112119
113120 displayFn ( user : UserDTO ) {
0 commit comments