Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: #255 clear data on new customer button is pressed
  • Loading branch information
enriquezrene committed May 21, 2020
commit 9bb63625c68489e723d12421056d8caa8e14422e
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Store, select } from '@ngrx/store';

import { Subscription } from 'rxjs';
import { getCustomerById } from './../../../../store/customer-management.selectors';
import { getCustomerUnderEdition } from './../../../../store/customer-management.selectors';
import { Customer } from 'src/app/modules/shared/models';
import {
CustomerState,
Expand All @@ -24,7 +24,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
@Input() areTabsActive: boolean;
@Output() changeValueAreTabsActives = new EventEmitter<boolean>();
@Output() closeCustomerComponent = new EventEmitter<boolean>();
@Output() sendActivityName = new EventEmitter<string>();
customerToEdit: Customer;
editSubscription: Subscription;

Expand All @@ -38,7 +37,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
ngOnInit() {
this.areTabsActive = true;
this.changeValueAreTabsActives.emit(this.areTabsActive);
const customers$ = this.store.pipe(select(getCustomerById));
const customers$ = this.store.pipe(select(getCustomerUnderEdition));
this.editSubscription = customers$.subscribe((customer) => {
this.customerToEdit = customer;
this.setDataToUpdate(this.customerToEdit);
Expand All @@ -60,7 +59,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
} else {
this.store.dispatch(new CreateCustomer(customerData));
}
this.sendActivityName.emit(customerData.name);
this.areTabsActive = true;
this.changeValueAreTabsActives.emit(this.areTabsActive);
}
Expand All @@ -70,11 +68,12 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
this.store.dispatch(new LoadProjectTypes(customerData.id));
this.store.dispatch(new LoadCustomerProjects(customerData.id));
this.changeValueAreTabsActives.emit(true);
this.sendActivityName.emit(customerData.name);
this.customerForm.setValue({
name: customerData.name,
description: customerData.description,
});
} else {
this.customerForm.reset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export class CustomerListComponent implements OnInit, OnDestroy {
loadCustomersSubscription: Subscription;
changeCustomerSubscription: Subscription;

constructor(private store: Store<Customer>, private actionsSubject$: ActionsSubject) {
}
constructor(private store: Store<Customer>, private actionsSubject$: ActionsSubject) { }

ngOnInit(): void {
this.dtOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ <h4 class="navbar-brand nav-title">{{customerName}}</h4>
[areTabsActive]="areTabsActive"
(changeValueAreTabsActives)="activeTabs($event)"
(closeCustomerComponent)="closeCustomer($event)"
(sendActivityName)="sendActivityName($event)"
></app-create-customer>
</div>
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Component, Output, EventEmitter } from '@angular/core';
import { getCustomerUnderEdition } from './../../store/customer-management.selectors';
import { Customer } from 'src/app/modules/shared/models';
import { Store, select } from '@ngrx/store';
import { Component, Output, EventEmitter, OnInit } from '@angular/core';

@Component({
selector: 'app-management-customer-projects',
templateUrl: './management-customer-projects.component.html',
styleUrls: ['./management-customer-projects.component.scss'],
})
export class ManagementCustomerProjectsComponent {
export class ManagementCustomerProjectsComponent implements OnInit {
@Output() closeCustemerForm = new EventEmitter<boolean>();
areTabsActive: boolean;
activeTab: string;
customerName: string;
constructor() {}

constructor(private store: Store<Customer>) { }

ngOnInit(): void {
const customers$ = this.store.pipe(select(getCustomerUnderEdition));
customers$.subscribe((customer) => {
if (customer) {
this.customerName = customer.name;
} else {
this.customerName = undefined;
}
});
}

activeTabs($areTabsActive: boolean) {
setTimeout(() => {
Expand All @@ -26,9 +41,5 @@ export class ManagementCustomerProjectsComponent {
showTab(activeTab: string) {
this.activeTab = activeTab;
}
sendActivityName(event) {
setTimeout(() => {
this.customerName = event;
}, 1);
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import { SetCustomerToEdit } from 'src/app/modules/customer-management/store';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { Customer } from 'src/app/modules/shared/models';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CustomerComponent } from './customer.component';

describe('CustomerComponent', () => {
let component: CustomerComponent;
let fixture: ComponentFixture<CustomerComponent>;
let store: MockStore<Customer>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [CustomerComponent],
providers: [provideMockStore({ initialState: {} })],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CustomerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
store = TestBed.inject(MockStore);
});

it('should be created', () => {
expect(component).toBeTruthy();
});

it('dispatches an action on activateCustomerForm', () => {
spyOn(store, 'dispatch');

component.activateCustomerForm();

expect(store.dispatch).toHaveBeenCalledWith(new SetCustomerToEdit(null));
});

it('should change te value of var when method is called', () => {
component.showCustomerForm = false;
component.activateCustomerForm();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Store } from '@ngrx/store';
import { Customer } from 'src/app/modules/shared/models';
import { SetCustomerToEdit } from 'src/app/modules/customer-management/store';
import { Component } from '@angular/core';

@Component({
Expand All @@ -9,7 +12,10 @@ export class CustomerComponent {
showCustomerForm = false;
activityName: string;

constructor(private store: Store<Customer>) { }

activateCustomerForm() {
this.store.dispatch(new SetCustomerToEdit(null));
this.showCustomerForm = true;
}
closeCustomerForm(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getCustomerId = createSelector(getCustomerState, (state: CustomerSt
}
});

export const getCustomerById = createSelector(allCustomers, customerIdtoEdit, (customers, customerIdToEdit) => {
export const getCustomerUnderEdition = createSelector(allCustomers, customerIdtoEdit, (customers, customerIdToEdit) => {
if (customers) {
return customers.find((customer) => {
return customer.id === customerIdToEdit;
Expand Down