Refactor form building logic into its own service

master
Kevin C. Coram 2019-09-04 23:47:09 -04:00
parent 34098189c8
commit bfda0985eb
Signed by: kevin
GPG Key ID: 0342351B3D61AD35
4 changed files with 70 additions and 39 deletions

View File

@ -6,6 +6,7 @@ import { NameComponent } from './name/name.component';
import { AddressListComponent } from './address-list/address-list.component';
import { AddressComponent } from './address/address.component';
import { ContactModule } from '@nested-forms/contact';
import { ContactFormService } from './services/contact-form.service';
describe('AppComponent', () => {
beforeEach(async(() => {
@ -15,7 +16,8 @@ describe('AppComponent', () => {
ReactiveFormsModule,
ContactModule.forRoot()
],
declarations: [AppComponent, NameComponent, AddressComponent, AddressListComponent]
declarations: [AppComponent, NameComponent, AddressComponent, AddressListComponent],
providers: [ ContactFormService ],
}).compileComponents();
}));

View File

@ -1,12 +1,13 @@
import { FormGroup, FormBuilder, FormArray } from '@angular/forms';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Contact, ContactService } from '@nested-forms/contact';
import { Subscription } from 'rxjs';
import { ContactFormService } from './services/contact-form.service';
@Component({
selector: 'nested-forms-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit, OnDestroy {
contact: Contact;
@ -14,14 +15,17 @@ export class AppComponent implements OnInit, OnDestroy {
private subscription: Subscription;
constructor(private fb: FormBuilder, private service: ContactService) {}
constructor(
private service: ContactService,
private formService: ContactFormService,
) {}
public ngOnInit() {
this.subscription = this.service
.loadContact()
.subscribe((data: Contact) => {
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();
}
}
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;
}
}

View File

@ -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();
});
});

View File

@ -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;
}
}