Refactor form building logic into its own service
parent
34098189c8
commit
bfda0985eb
|
@ -6,6 +6,7 @@ import { NameComponent } from './name/name.component';
|
||||||
import { AddressListComponent } from './address-list/address-list.component';
|
import { AddressListComponent } from './address-list/address-list.component';
|
||||||
import { AddressComponent } from './address/address.component';
|
import { AddressComponent } from './address/address.component';
|
||||||
import { ContactModule } from '@nested-forms/contact';
|
import { ContactModule } from '@nested-forms/contact';
|
||||||
|
import { ContactFormService } from './services/contact-form.service';
|
||||||
|
|
||||||
describe('AppComponent', () => {
|
describe('AppComponent', () => {
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
|
@ -15,7 +16,8 @@ describe('AppComponent', () => {
|
||||||
ReactiveFormsModule,
|
ReactiveFormsModule,
|
||||||
ContactModule.forRoot()
|
ContactModule.forRoot()
|
||||||
],
|
],
|
||||||
declarations: [AppComponent, NameComponent, AddressComponent, AddressListComponent]
|
declarations: [AppComponent, NameComponent, AddressComponent, AddressListComponent],
|
||||||
|
providers: [ ContactFormService ],
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import { FormGroup, FormBuilder, FormArray } from '@angular/forms';
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
import { FormGroup } from '@angular/forms';
|
||||||
import { Contact, ContactService } from '@nested-forms/contact';
|
import { Contact, ContactService } from '@nested-forms/contact';
|
||||||
import { Subscription } from 'rxjs';
|
import { Subscription } from 'rxjs';
|
||||||
|
import { ContactFormService } from './services/contact-form.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'nested-forms-root',
|
selector: 'nested-forms-root',
|
||||||
templateUrl: './app.component.html',
|
templateUrl: './app.component.html',
|
||||||
styleUrls: ['./app.component.css']
|
styleUrls: ['./app.component.css'],
|
||||||
})
|
})
|
||||||
export class AppComponent implements OnInit, OnDestroy {
|
export class AppComponent implements OnInit, OnDestroy {
|
||||||
contact: Contact;
|
contact: Contact;
|
||||||
|
@ -14,14 +15,17 @@ export class AppComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
private subscription: Subscription;
|
private subscription: Subscription;
|
||||||
|
|
||||||
constructor(private fb: FormBuilder, private service: ContactService) {}
|
constructor(
|
||||||
|
private service: ContactService,
|
||||||
|
private formService: ContactFormService,
|
||||||
|
) {}
|
||||||
|
|
||||||
public ngOnInit() {
|
public ngOnInit() {
|
||||||
this.subscription = this.service
|
this.subscription = this.service
|
||||||
.loadContact()
|
.loadContact()
|
||||||
.subscribe((data: Contact) => {
|
.subscribe((data: Contact) => {
|
||||||
this.contact = data;
|
this.contact = data;
|
||||||
this.form = this.createForm();
|
this.form = this.formService.createForm(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,37 +34,4 @@ export class AppComponent implements OnInit, OnDestroy {
|
||||||
this.subscription.unsubscribe();
|
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: [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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { ReactiveFormsModule } from '@angular/forms';
|
||||||
|
|
||||||
|
import { ContactFormService } from './contact-form.service';
|
||||||
|
|
||||||
|
describe('ContactFormService', () => {
|
||||||
|
beforeEach(() => TestBed.configureTestingModule({
|
||||||
|
imports: [ReactiveFormsModule],
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
const service: ContactFormService = TestBed.get(ContactFormService);
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
||||||
|
import { Contact } from '@nested-forms/contact';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class ContactFormService {
|
||||||
|
constructor(private fb: FormBuilder) {}
|
||||||
|
|
||||||
|
public createForm(model: Contact): FormGroup {
|
||||||
|
const name = model.name;
|
||||||
|
|
||||||
|
const addresses: FormArray = this.fb.array([]);
|
||||||
|
|
||||||
|
const group = this.fb.group({
|
||||||
|
nameGroup: this.fb.group({
|
||||||
|
firstName: [name ? name.firstName : ''],
|
||||||
|
lastName: [name ? name.lastName : ''],
|
||||||
|
middleName: [name ? name.middleName : ''],
|
||||||
|
prefix: [name ? name.prefix : ''],
|
||||||
|
suffix: [name ? name.suffix : ''],
|
||||||
|
}),
|
||||||
|
addresses: addresses,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (model.addresses) {
|
||||||
|
model.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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue