|
1 | | -import { Component, OnInit } from '@angular/core'; |
2 | | -import { FormGroup, FormControl, Validators, ValidatorFn, AbstractControl } from '@angular/forms'; |
3 | | -import { Project } from './project'; |
| 1 | +import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; |
| 2 | +import { FormGroup, FormControl, Validators } from '@angular/forms'; |
4 | 3 | import { Observable } from 'rxjs'; |
5 | | -import {map, startWith} from 'rxjs/operators'; |
| 4 | +import {map, startWith, filter} from 'rxjs/operators'; |
| 5 | +import { FormCustomValidators } from './validators/FormCustomValidators'; |
| 6 | +import { UserService } from './service/user.service'; |
| 7 | +import { UserDTO } from './model/UserDTO'; |
| 8 | +import { AlbumDTO } from './model/AlbumDTO'; |
| 9 | +import { AlbumService } from './service/album.service'; |
| 10 | +import { MatAutocompleteTrigger } from '@angular/material'; |
| 11 | + |
6 | 12 |
|
7 | 13 | @Component({ |
8 | 14 | selector: 'app-root', |
9 | 15 | templateUrl: './app.component.html', |
10 | 16 | styleUrls: ['./app.component.css'] |
11 | 17 | }) |
12 | 18 | export class AppComponent implements OnInit { |
| 19 | + |
13 | 20 | form: FormGroup; |
14 | | - projects: Project[]; |
15 | | - filteredOptions: Observable<string[]>; |
16 | | - state: string; |
17 | | - states = [ |
18 | | - 'Alabama', |
19 | | - 'Alaska', |
20 | | - 'Arizona', |
21 | | - 'Arkansas', |
22 | | - 'California', |
23 | | - 'Colorado', |
24 | | - 'Connecticut', |
25 | | - 'Delaware', |
26 | | - 'Florida', |
27 | | - 'Georgia', |
28 | | - 'Hawaii', |
29 | | - 'Idaho', |
30 | | - 'Illinois', |
31 | | - 'Indiana', |
32 | | - 'Iowa', |
33 | | - 'Kansas', |
34 | | - 'Kentucky', |
35 | | - 'Louisiana', |
36 | | - 'Maine', |
37 | | - 'Maryland', |
38 | | - 'Massachusetts', |
39 | | - 'Michigan', |
40 | | - 'Minnesota', |
41 | | - 'Mississippi', |
42 | | - 'Missouri', |
43 | | - 'Montana', |
44 | | - 'Nebraska', |
45 | | - 'Nevada', |
46 | | - 'New Hampshire', |
47 | | - 'New Jersey', |
48 | | - 'New Mexico', |
49 | | - 'New York', |
50 | | - 'North Carolina', |
51 | | - 'North Dakota', |
52 | | - 'Ohio', |
53 | | - 'Oklahoma', |
54 | | - 'Oregon', |
55 | | - 'Pennsylvania', |
56 | | - 'Rhode Island', |
57 | | - 'South Carolina', |
58 | | - 'South Dakota', |
59 | | - 'Tennessee', |
60 | | - 'Texas', |
61 | | - 'Utah', |
62 | | - 'Vermont', |
63 | | - 'Virginia', |
64 | | - 'Washington', |
65 | | - 'West Virginia', |
66 | | - 'Wisconsin', |
67 | | - 'Wyoming', |
68 | | - ]; |
69 | | - |
70 | | - constructor() { |
| 21 | + |
| 22 | + @ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger; |
| 23 | + @ViewChild('focusMe') _focusMe: ElementRef; |
| 24 | + |
| 25 | + user: UserDTO; |
| 26 | + users: Array<UserDTO>; |
| 27 | + filteredUsersOptions: Observable<UserDTO[]>; |
| 28 | + |
| 29 | + album: AlbumDTO; |
| 30 | + albums: Array<AlbumDTO>; |
| 31 | + filteredAlbumsOptions: Observable<AlbumDTO[]>; |
| 32 | + |
| 33 | + constructor(private userService: UserService, |
| 34 | + private albumService: AlbumService) { |
| 35 | + |
| 36 | + this.users = new Array<UserDTO>(); |
| 37 | + this.albums = new Array<AlbumDTO>(); |
71 | 38 | } |
72 | 39 |
|
73 | 40 | ngOnInit() { |
| 41 | + this.getUsers(); |
| 42 | + this.initForm(); |
| 43 | + } |
| 44 | + |
| 45 | + private initForm(): void { |
74 | 46 | this.form = new FormGroup({ |
75 | | - project: new FormControl('', [Validators.required, FormCustomValidators.valueSelected(this.states)]), |
| 47 | + formUserControl: new FormControl('', [Validators.required]), |
| 48 | + formAlbumsControl: new FormControl({value: '', disabled: true}, [Validators.required]) |
76 | 49 | }); |
77 | | - this.filteredOptions = this.form.get('project').valueChanges.pipe( |
| 50 | + |
| 51 | + this.filteredUsersOptions = this.form.get('formUserControl').valueChanges.pipe( |
| 52 | + startWith(''), |
| 53 | + filter(value => typeof value === 'string'), |
| 54 | + map(value => this._filterUsers(value)) |
| 55 | + ); |
| 56 | + this.filteredAlbumsOptions = this.form.get('formAlbumsControl').valueChanges.pipe( |
78 | 57 | startWith(''), |
79 | | - map(value => this._filter(value)) |
| 58 | + filter(value => typeof value === 'string'), |
| 59 | + map(value => this._filterAlbums(value)) |
80 | 60 | ); |
81 | 61 | } |
82 | 62 |
|
83 | | - private _filter(value: string): string[] { |
| 63 | + private _filterUsers(value: string): UserDTO[] { |
84 | 64 | const filterValue = value.toLowerCase(); |
85 | | - return this.states.filter(option => option.toLowerCase().indexOf(filterValue) === 0); |
| 65 | + return this.users.filter(user => user.name.toLowerCase().indexOf(filterValue) === 0); |
86 | 66 | } |
87 | 67 |
|
88 | | -} |
| 68 | + private _filterAlbums(value: string): AlbumDTO[] { |
| 69 | + const filterValue = value.toLowerCase(); |
| 70 | + return this.albums.filter(album => album.title.toLowerCase().indexOf(filterValue) === 0); |
| 71 | + } |
89 | 72 |
|
90 | | -export class FormCustomValidators { |
91 | | - static valueSelected(myArray: any[]): ValidatorFn { |
| 73 | + private addValidUserSelected() { |
| 74 | + this.form.get('formUserControl').setValidators(FormCustomValidators.valueSelected(this.users)); |
| 75 | + } |
92 | 76 |
|
93 | | - return (c: AbstractControl): { [key: string]: boolean } | null => { |
94 | | - let selectboxValue = c.value; |
95 | | - let pickedOrNot = myArray.filter(alias => alias === selectboxValue); |
| 77 | + private addValidAlbumsSelected() { |
| 78 | + const albumsTitle = this.albums.map(album => album.title); |
| 79 | + this.form.get('formAlbumsControl').setValidators(FormCustomValidators.valueSelected(albumsTitle)); |
| 80 | + } |
96 | 81 |
|
97 | | - if (pickedOrNot.length > 0) { |
98 | | - // everything's fine. return no error. therefore it's null. |
99 | | - return null; |
| 82 | + private getUsers(): void { |
| 83 | + this.userService.getUsers().subscribe( res => { |
| 84 | + this.users = res; |
| 85 | + this.addValidUserSelected(); |
| 86 | + console.log('Usuários: ', this.users); |
| 87 | + }, error => |
| 88 | + console.log(error) |
| 89 | + ); |
| 90 | + } |
100 | 91 |
|
101 | | - } else { |
102 | | - //there's no matching selectboxvalue selected. so return match error. |
103 | | - return { 'incorrect': true }; |
104 | | - } |
105 | | - } |
| 92 | + private verificaSeDeveHabilitarFormAlbumsControl(): void { |
| 93 | + const formAlbumsControl = this.form.get('formAlbumsControl'); |
| 94 | + this.albums.length > 0 ? formAlbumsControl.enable() : formAlbumsControl.disable(); |
| 95 | + } |
| 96 | + |
| 97 | + getAlbumsByUser(userId: number) { |
| 98 | + console.log('User Id: ', userId); |
| 99 | + this.albumService.getAlbumsFromUser(userId).subscribe( resp => { |
| 100 | + this.albums = resp; |
| 101 | + this.addValidAlbumsSelected(); |
| 102 | + this.verificaSeDeveHabilitarFormAlbumsControl(); |
| 103 | + console.log('Albums By Users: ', this.albums); |
| 104 | + }, error => { |
| 105 | + console.log(error); |
| 106 | + }); |
106 | 107 | } |
| 108 | + |
| 109 | + getUser(): void { |
| 110 | + console.log("Usuario Selecionado: ", this.user); |
| 111 | + } |
| 112 | + |
| 113 | + displayFn(user: UserDTO) { |
| 114 | + if (user) { return user.name; } |
| 115 | + } |
| 116 | + |
107 | 117 | } |
108 | 118 |
|
109 | 119 |
|
| 120 | + |
0 commit comments