From c5aa8b2f70e6913ee0862c343462b31d27b4fc73 Mon Sep 17 00:00:00 2001 From: "Kevin C. Coram" Date: Wed, 4 Sep 2019 22:38:01 -0400 Subject: [PATCH] Build form from contact model data --- .../global-form/src/app/app.component.spec.ts | 8 ++- apps/global-form/src/app/app.component.ts | 65 +++++++++++++++---- apps/global-form/src/app/app.module.ts | 4 +- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/apps/global-form/src/app/app.component.spec.ts b/apps/global-form/src/app/app.component.spec.ts index a04fd25..0693f7a 100644 --- a/apps/global-form/src/app/app.component.spec.ts +++ b/apps/global-form/src/app/app.component.spec.ts @@ -3,11 +3,16 @@ import { RouterTestingModule } from '@angular/router/testing'; import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { NameComponent } from './name/name.component'; +import { ContactModule } from '@nested-forms/contact'; describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [RouterTestingModule, ReactiveFormsModule], + imports: [ + RouterTestingModule, + ReactiveFormsModule, + ContactModule.forRoot() + ], declarations: [AppComponent, NameComponent] }).compileComponents(); })); @@ -17,5 +22,4 @@ describe('AppComponent', () => { const app = fixture.debugElement.componentInstance; expect(app).toBeTruthy(); }); - }); diff --git a/apps/global-form/src/app/app.component.ts b/apps/global-form/src/app/app.component.ts index 24c4c3c..0777b7e 100644 --- a/apps/global-form/src/app/app.component.ts +++ b/apps/global-form/src/app/app.component.ts @@ -1,23 +1,66 @@ -import { FormGroup, FormBuilder } from '@angular/forms'; -import { Component } from '@angular/core'; +import { FormGroup, FormBuilder, FormArray } from '@angular/forms'; +import { Component, OnInit, OnDestroy } from '@angular/core'; +import { Contact, ContactService } from '@nested-forms/contact'; +import { Subscription } from 'rxjs'; @Component({ selector: 'nested-forms-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) -export class AppComponent { +export class AppComponent implements OnInit, OnDestroy { + contact: Contact; form: FormGroup; - constructor(private fb: FormBuilder) { - this.form = this.fb.group({ + private subscription: Subscription; + + constructor(private fb: FormBuilder, private service: ContactService) {} + + public ngOnInit() { + this.subscription = this.service + .loadContact() + .subscribe((data: Contact) => { + this.contact = data; + this.form = this.createForm(); + }); + } + + public ngOnDestroy() { + if (this.subscription) { + this.subscription.unsubscribe(); + } + } + + private createForm(): FormGroup { + const name = this.contact.name; + + const addresses: FormArray = this.fb.array([]); + + const group = this.fb.group({ nameGroup: this.fb.group({ - firstName: [], // new FormControl(this.name ? this.name.firstName : ''), - lastName: [], // new FormControl(this.name ? this.name.lastName : ''), - middleName: [], // new FormControl(this.name ? this.name.middleName : ''), - prefix: [], // new FormControl(this.name ? this.name.prefix : ''), - suffix: [], // new FormControl(this.name ? this.name.suffix : ''), - }) + firstName: [name ? name.firstName : ''], + lastName: [name ? name.lastName : ''], + middleName: [name ? name.middleName : ''], + prefix: [name ? name.prefix : ''], + suffix: [name ? name.suffix : ''] + }), + addresses: addresses }); + + if (this.contact.addresses) { + this.contact.addresses.forEach(addr => { + addresses.push( + this.fb.group({ + line1: [addr ? addr.line_1 : ''], + line2: [addr ? addr.line_2 : ''], + city: [addr ? addr.city : ''], + state: [addr ? addr.state : ''], + postalCode: [addr ? addr.postalCode : ''] + }) + ); + }); + } + + return group; } } diff --git a/apps/global-form/src/app/app.module.ts b/apps/global-form/src/app/app.module.ts index f748a5f..6716cb7 100644 --- a/apps/global-form/src/app/app.module.ts +++ b/apps/global-form/src/app/app.module.ts @@ -5,13 +5,15 @@ import { AppComponent } from './app.component'; import { RouterModule } from '@angular/router'; import { ReactiveFormsModule } from '@angular/forms'; import { NameComponent } from './name/name.component'; +import { ContactModule } from '@nested-forms/contact'; @NgModule({ declarations: [AppComponent, NameComponent], imports: [ BrowserModule, RouterModule.forRoot([], { initialNavigation: 'enabled' }), - ReactiveFormsModule + ReactiveFormsModule, + ContactModule.forRoot() ], providers: [], bootstrap: [AppComponent]