Last active
January 22, 2019 07:46
-
-
Save nikitalarionov/3fb1a46d9a0a5d077389b00554d52107 to your computer and use it in GitHub Desktop.
Authors code elegance example for review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthorsComponent implements OnInit, OnDestroy { | |
authorsForm: FormGroup; | |
authorsFormArray: FormArray; | |
getAuthorFormField(person?: EntryAuthorRoleViewModel, mainAuthorId?: number) { | |
return this.fb.group({ | |
name: new FormControl(person.author || '', Validators.required), | |
role: new FormControl(person.authorRole || '', Validators.required), | |
isMain: new FormControl(mainAuthorId === person.authorId || false, Validators.required) | |
}); | |
} | |
ngOnInit() { | |
// Initialise form structure | |
this.authorsForm = this.fb.group({ | |
authors: this.fb.array([]) | |
}); | |
// Save authors form array reference to variable | |
this.authorsFormArray = this.authorsForm.get('authors') as FormArray; | |
// Add new subscribe | |
this.subscriptions.add( | |
// Subscribe on loading authors data | |
this.entryLoader.loadAuthorsData(this.entryId).subscribe( | |
({ entryAuthorRoleList, mainAuthorId }: EntryAuthorRoleListViewModel) => { | |
// Iterate on authors to fill empty form | |
entryAuthorRoleList.map( person => { | |
// Add author field to empty form | |
this.authorsFormArray.push(this.getAuthorFormField(person, mainAuthorId)); | |
}); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment