From d5b177c009f0199ac50e4aaaec862b87b611fe91 Mon Sep 17 00:00:00 2001 From: PaulRC-ioet Date: Tue, 10 Nov 2020 12:05:27 -0500 Subject: [PATCH] fix: #563 fix bug cannot creat project --- .../customer-list.component.spec.ts | 13 ++++- .../customer-list/customer-list.component.ts | 52 +++++++++++-------- .../create-project-type.component.spec.ts | 13 +++-- .../create-project-type.component.ts | 4 +- .../create-project.component.spec.ts | 13 +++-- .../create-project.component.ts | 1 + 6 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts index 7f073ad7b..88851ae6b 100644 --- a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts +++ b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts @@ -12,6 +12,8 @@ import { } from 'src/app/modules/customer-management/store'; import { DataTablesModule } from 'angular-datatables'; import { ActionsSubject } from '@ngrx/store'; +import { ResetProjectToEdit } from '../../../projects/components/store/project.actions'; +import { ResetProjectTypeToEdit } from '../../../projects-type/store'; describe('CustomerTableListComponent', () => { let component: CustomerListComponent; @@ -65,6 +67,15 @@ describe('CustomerTableListComponent', () => { expect(component.showCustomerForm).toBeTruthy(); }); + it('onClick edit, dispatch clean Forms in project and project type', () => { + spyOn(store, 'dispatch'); + + component.editCustomer('1'); + + expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectToEdit()); + expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectTypeToEdit()); + }); + it('onClick delete, dispatch DeleteCustomer', () => { spyOn(store, 'dispatch'); component.idToDelete = '1'; @@ -121,7 +132,7 @@ describe('CustomerTableListComponent', () => { const actionSubject = TestBed.inject(ActionsSubject); const action = { type: CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS, - payload: state.data + payload: state.data, }; spyOn(component.dtElement.dtInstance, 'then'); diff --git a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts index 1430da445..ed46b8b94 100644 --- a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts +++ b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts @@ -9,22 +9,23 @@ import { CustomerManagementActionTypes, DeleteCustomer, LoadCustomers, - SetCustomerToEdit + SetCustomerToEdit, } from './../../../../store/customer-management.actions'; +import { ResetProjectToEdit } from '../../../projects/components/store/project.actions'; +import { ResetProjectTypeToEdit } from '../../../projects-type/store'; @Component({ selector: 'app-customer-list', templateUrl: './customer-list.component.html', styleUrls: ['./customer-list.component.scss'], }) export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit { - @Input() showCustomerForm: boolean; @Output() changeValueShowCustomerForm = new EventEmitter(); @Input() customers: Customer[] = []; dtOptions: any = {}; dtTrigger: Subject = new Subject(); - @ViewChild(DataTableDirective, {static: false}) + @ViewChild(DataTableDirective, { static: false }) dtElement: DataTableDirective; loadCustomersSubscription: Subscription; changeCustomerSubscription: Subscription; @@ -40,33 +41,32 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit { ngOnInit(): void { this.dtOptions = { scrollY: '290px', - paging: false + paging: false, }; - this.loadCustomersSubscription = this.actionsSubject$.pipe( - filter((action: any) => ( - action.type === CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS - ) - ) - ).subscribe((action) => { - this.customers = action.payload; - this.rerenderDataTable(); - }); + this.loadCustomersSubscription = this.actionsSubject$ + .pipe(filter((action: any) => action.type === CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS)) + .subscribe((action) => { + this.customers = action.payload; + this.rerenderDataTable(); + }); - this.changeCustomerSubscription = this.actionsSubject$.pipe( - filter((action: any) => ( - action.type === CustomerManagementActionTypes.DELETE_CUSTOMER_SUCCESS || - action.type === CustomerManagementActionTypes.UPDATE_CUSTOMER_SUCCESS || - action.type === CustomerManagementActionTypes.CREATE_CUSTOMER_SUCCESS + this.changeCustomerSubscription = this.actionsSubject$ + .pipe( + filter( + (action: any) => + action.type === CustomerManagementActionTypes.DELETE_CUSTOMER_SUCCESS || + action.type === CustomerManagementActionTypes.UPDATE_CUSTOMER_SUCCESS || + action.type === CustomerManagementActionTypes.CREATE_CUSTOMER_SUCCESS ) ) - ).subscribe((action) => { - this.store.dispatch(new LoadCustomers()); - this.showCustomerForm = false; - }); + .subscribe((action) => { + this.store.dispatch(new LoadCustomers()); + this.showCustomerForm = false; + }); this.store.dispatch(new LoadCustomers()); } - ngAfterViewInit(): void { + ngAfterViewInit(): void { this.rerenderDataTable(); } @@ -80,6 +80,12 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit { this.showCustomerForm = true; this.changeValueShowCustomerForm.emit(this.showCustomerForm); this.store.dispatch(new SetCustomerToEdit(customerId)); + this.resetProjectFieldsToEdit(); + } + + private resetProjectFieldsToEdit() { + this.store.dispatch(new ResetProjectToEdit()); + this.store.dispatch(new ResetProjectTypeToEdit()); } deleteCustomer() { diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts index b85058f16..d504ee667 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts @@ -41,10 +41,7 @@ describe('InputProjectTypeComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [CreateProjectTypeComponent], - providers: [ - FormBuilder, - provideMockStore({ initialState: state }) - ], + providers: [FormBuilder, provideMockStore({ initialState: state })], }).compileComponents(); })); @@ -153,6 +150,14 @@ describe('InputProjectTypeComponent', () => { expect(component.projectTypeForm.setValue).toHaveBeenCalledWith(projectTypeDataForm); }); + it('shoud reset Project Type Form before set the data to Edit', () => { + spyOn(component.projectTypeForm, 'reset'); + + component.setDataToUpdate(projectType); + + expect(component.projectTypeForm.reset).toHaveBeenCalled(); + }); + it('should dispatch a ResetProjectTypeToEdit action', () => { spyOn(store, 'dispatch'); diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts index b5e3221c8..21b6b18c2 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts @@ -19,8 +19,7 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy { customerId: string; getCustomerIdSubscription: Subscription; - constructor(private formBuilder: FormBuilder, - private store: Store) { + constructor(private formBuilder: FormBuilder, private store: Store) { this.projectTypeForm = this.formBuilder.group({ name: ['', Validators.required], description: [''], @@ -47,6 +46,7 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy { } setDataToUpdate(projectTypeData: ProjectType) { + this.projectTypeForm.reset(); if (projectTypeData) { this.projectTypeForm.setValue({ name: projectTypeData.name, diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts index 6e8880c4c..fa7325496 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts @@ -38,10 +38,7 @@ describe('InputProjectComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [CreateProjectComponent], - providers: [ - FormBuilder, - provideMockStore({ initialState: state }) - ], + providers: [FormBuilder, provideMockStore({ initialState: state })], }).compileComponents(); })); @@ -233,6 +230,14 @@ describe('InputProjectComponent', () => { expect(component.projectForm.setValue).toHaveBeenCalledWith(projectForm); }); + it('shoud reset Project Form before set the data to Edit', () => { + spyOn(component.projectForm, 'reset'); + + component.setDataToUpdate(project); + + expect(component.projectForm.reset).toHaveBeenCalled(); + }); + it('should dispatch a ResetActivityToEdit action', () => { spyOn(store, 'dispatch'); diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts index fa303e86d..57a07574b 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts @@ -86,6 +86,7 @@ export class CreateProjectComponent implements OnInit, OnDestroy { } setDataToUpdate(projectData: Project) { + this.projectForm.reset(); if (projectData) { this.projectForm.setValue({ name: projectData.name,