Skip to content

Commit 1d140a2

Browse files
committed
move form to reactive form; modify model user to string
1 parent 004b887 commit 1d140a2

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/app/app.component.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
matInput formControlName="formUserControl"
1313
[matAutocomplete]="projectAutoComplete"
1414
[(ngModel)]="user"
15-
(blur)="getAlbumsByUser(user?.id)">
16-
<mat-autocomplete #projectAutoComplete="matAutocomplete" [displayWith]="displayFn">
17-
<mat-option *ngFor="let user of filteredUsersOptions | async" [value]="user">
15+
(blur)="getAlbumsByUser(user)">
16+
<mat-autocomplete #projectAutoComplete="matAutocomplete">
17+
<mat-option *ngFor="let user of filteredUsersOptions | async" [value]="user.name">
1818
{{ user.name }}
1919
</mat-option>
2020
</mat-autocomplete>
@@ -31,6 +31,7 @@
3131
aria-label="Number"
3232
matInput formControlName="formAlbumsControl"
3333
[matAutocomplete]="AlbumsAutoComplete"
34+
[(ngModel)] = "album"
3435
required>
3536
<mat-autocomplete #AlbumsAutoComplete="matAutocomplete">
3637
<mat-option *ngFor="let album of (albums.length === 0 ? [] : (filteredAlbumsOptions | async))" [value]="album.title">
@@ -48,7 +49,7 @@
4849
<div class="form-element">
4950
<button mat-raised-button color="primary"
5051
class="button"
51-
(click)="getUser()"
52+
(click)="save()"
5253
[disabled]="!form.valid">
5354
Send...
5455
</button>

src/app/app.component.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
2-
import { FormGroup, FormControl, Validators } from '@angular/forms';
2+
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
33
import { Observable } from 'rxjs';
4-
import {map, startWith, filter} from 'rxjs/operators';
4+
import { map, startWith } from 'rxjs/operators';
55
import { FormCustomValidators } from './validators/FormCustomValidators';
66
import { UserService } from './service/user.service';
77
import { 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

Comments
 (0)