Skip to content

Commit 083ad7d

Browse files
committed
fix: TT-29 Clean the form if the customer is removed
1 parent cfa91aa commit 083ad7d

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
7070
this.store.dispatch(new LoadProjectTypes(customerData.id));
7171
this.store.dispatch(new LoadCustomerProjects(customerData.id));
7272
this.changeValueAreTabsActives.emit(true);
73-
this.hasChangedEvent.emit(this.hasChange = false);
7473
this.customerForm.setValue({
7574
name: customerData.name,
7675
description: customerData.description,
@@ -81,6 +80,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
8180
this.markTabsAsInactive();
8281
this.customerForm.reset();
8382
}
83+
this.hasChangedEvent.emit((this.hasChange = false));
8484
}
8585

8686
markTabsAsInactive() {

src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CustomerState,
99
DeleteCustomer,
1010
LoadCustomers,
11+
ResetCustomerToEdit,
1112
SetCustomerToEdit,
1213
} from 'src/app/modules/customer-management/store';
1314
import { DataTablesModule } from 'angular-datatables';
@@ -111,6 +112,17 @@ describe('CustomerTableListComponent', () => {
111112
expect(store.dispatch).toHaveBeenCalledWith(new DeleteCustomer('1'));
112113
});
113114

115+
it('onClick delete, if idToDelete is equal to idToEdit should dispatch ResetCustomerToEdit', () => {
116+
117+
spyOn(store, 'dispatch');
118+
119+
component.idToDelete = '1';
120+
component.idToEdit = '1';
121+
component.deleteCustomer();
122+
123+
expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
124+
});
125+
114126
const params = [
115127
{ actionName: 'delete', actionType: CustomerManagementActionTypes.DELETE_CUSTOMER_SUCCESS },
116128
{ actionName: 'update', actionType: CustomerManagementActionTypes.UPDATE_CUSTOMER_SUCCESS },

src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { ActionsSubject, select, Store } from '@ngrx/store';
33
import { DataTableDirective } from 'angular-datatables';
44
import { Observable, Subject, Subscription } from 'rxjs';
55
import { delay, filter } from 'rxjs/operators';
6-
import { getIsLoading } from 'src/app/modules/customer-management/store/customer-management.selectors';
6+
import {
7+
customerIdtoEdit,
8+
getIsLoading
9+
} from 'src/app/modules/customer-management/store/customer-management.selectors';
710
import { Customer } from './../../../../../shared/models/customer.model';
811
import {
912
CustomerManagementActionTypes,
1013
DeleteCustomer,
1114
LoadCustomers,
15+
ResetCustomerToEdit,
1216
SetCustomerToEdit,
1317
} from './../../../../store/customer-management.actions';
1418
import { ResetProjectToEdit, SetProjectToEdit } from '../../../projects/components/store/project.actions';
@@ -31,6 +35,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
3135
dtElement: DataTableDirective;
3236
loadCustomersSubscription: Subscription;
3337
changeCustomerSubscription: Subscription;
38+
customerIdToEditSubscription: Subscription;
3439
showModal = false;
3540
idToDelete: string;
3641
idToEdit: string;
@@ -47,6 +52,12 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
4752
paging: false,
4853
responsive: true,
4954
};
55+
56+
const customerIdToEdit$ = this.store.pipe(select(customerIdtoEdit));
57+
this.customerIdToEditSubscription = customerIdToEdit$.subscribe((customerId: string) => {
58+
this.idToEdit = customerId;
59+
});
60+
5061
this.loadCustomersSubscription = this.actionsSubject$
5162
.pipe(filter((action: any) => action.type === CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS))
5263
.subscribe((action) => {
@@ -76,6 +87,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
7687
ngOnDestroy() {
7788
this.loadCustomersSubscription.unsubscribe();
7889
this.changeCustomerSubscription.unsubscribe();
90+
this.customerIdToEditSubscription.unsubscribe();
7991
this.dtTrigger.unsubscribe();
8092
}
8193

@@ -109,6 +121,10 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
109121
}
110122

111123
deleteCustomer() {
124+
if (this.idToDelete === this.idToEdit) {
125+
this.store.dispatch(new ResetCustomerToEdit());
126+
this.resetProjectFieldsToEdit();
127+
}
112128
this.store.dispatch(new DeleteCustomer(this.idToDelete));
113129
this.showModal = false;
114130
}

src/app/modules/customer-management/store/customer-management.reducers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a
5353
...state,
5454
data: [...state.data, action.payload],
5555
customerId: action.payload.id,
56+
customerIdToEdit: action.payload.id,
5657
isLoading: false,
5758
message: 'Customer created successfully!',
5859
};

0 commit comments

Comments
 (0)