Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ 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.hasChangedEvent.emit(this.hasChange = false);
this.customerForm.setValue({
name: customerData.name,
description: customerData.description,
Expand All @@ -81,6 +80,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
this.markTabsAsInactive();
this.customerForm.reset();
}
this.hasChangedEvent.emit((this.hasChange = false));
}

markTabsAsInactive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CustomerState,
DeleteCustomer,
LoadCustomers,
ResetCustomerToEdit,
SetCustomerToEdit,
} from 'src/app/modules/customer-management/store';
import { DataTablesModule } from 'angular-datatables';
Expand Down Expand Up @@ -103,6 +104,17 @@ describe('CustomerTableListComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new SetCustomerToEdit('1'));
});

it('when you click close modal, if the idToEdit is equal currentCustomerIdToEdit should dispatch ResetCustomerToEdit', () => {

spyOn(store, 'dispatch');

component.idToEdit = '1';
component.currentCustomerIdToEdit = '1';
component.closeModal();

expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
});

it('onClick delete, dispatch DeleteCustomer', () => {
spyOn(store, 'dispatch');
component.idToDelete = '1';
Expand All @@ -111,6 +123,17 @@ describe('CustomerTableListComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new DeleteCustomer('1'));
});

it('onClick delete, if idToDelete is equal to currentCustomerIdToEdit should dispatch ResetCustomerToEdit', () => {

spyOn(store, 'dispatch');

component.idToDelete = '1';
component.currentCustomerIdToEdit = '1';
component.deleteCustomer();

expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
});

const params = [
{ actionName: 'delete', actionType: CustomerManagementActionTypes.DELETE_CUSTOMER_SUCCESS },
{ actionName: 'update', actionType: CustomerManagementActionTypes.UPDATE_CUSTOMER_SUCCESS },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { ActionsSubject, select, Store } from '@ngrx/store';
import { DataTableDirective } from 'angular-datatables';
import { Observable, Subject, Subscription } from 'rxjs';
import { delay, filter } from 'rxjs/operators';
import { getIsLoading } from 'src/app/modules/customer-management/store/customer-management.selectors';
import {
customerIdtoEdit,
getIsLoading
} from 'src/app/modules/customer-management/store/customer-management.selectors';
import { Customer } from './../../../../../shared/models/customer.model';
import {
CustomerManagementActionTypes,
DeleteCustomer,
LoadCustomers,
ResetCustomerToEdit,
SetCustomerToEdit,
} from './../../../../store/customer-management.actions';
import { ResetProjectToEdit, SetProjectToEdit } from '../../../projects/components/store/project.actions';
Expand All @@ -31,9 +35,11 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
dtElement: DataTableDirective;
loadCustomersSubscription: Subscription;
changeCustomerSubscription: Subscription;
customerIdToEditSubscription: Subscription;
showModal = false;
idToDelete: string;
idToEdit: string;
currentCustomerIdToEdit: string;
message: string;
isLoading$: Observable<boolean>;

Expand All @@ -47,6 +53,12 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
paging: false,
responsive: true,
};

const customerIdToEdit$ = this.store.pipe(select(customerIdtoEdit));
this.customerIdToEditSubscription = customerIdToEdit$.subscribe((customerId: string) => {
this.currentCustomerIdToEdit = customerId;
});

this.loadCustomersSubscription = this.actionsSubject$
.pipe(filter((action: any) => action.type === CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS))
.subscribe((action) => {
Expand Down Expand Up @@ -76,6 +88,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
ngOnDestroy() {
this.loadCustomersSubscription.unsubscribe();
this.changeCustomerSubscription.unsubscribe();
this.customerIdToEditSubscription.unsubscribe();
this.dtTrigger.unsubscribe();
}

Expand All @@ -98,6 +111,9 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
this.showModal = false;
this.changeValueShowCustomerForm.emit(this.showCustomerForm);
this.resetProjectFieldsToEdit();
if (this.currentCustomerIdToEdit === this.idToEdit) {
this.store.dispatch(new ResetCustomerToEdit());
}
this.store.dispatch(new SetCustomerToEdit(this.idToEdit));
}

Expand All @@ -109,6 +125,10 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
}

deleteCustomer() {
if (this.idToDelete === this.currentCustomerIdToEdit) {
this.store.dispatch(new ResetCustomerToEdit());
this.resetProjectFieldsToEdit();
}
this.store.dispatch(new DeleteCustomer(this.idToDelete));
this.showModal = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a
...state,
data: [...state.data, action.payload],
customerId: action.payload.id,
customerIdToEdit: action.payload.id,
isLoading: false,
message: 'Customer created successfully!',
};
Expand Down