Skip to content

Commit c1c3eca

Browse files
authored
fix: TT-29 Clean the form if the customer is removed (#662)
* fix: TT-29 Clean the form if the customer is removed * fix: TT-29 * fix: TT-29 refactor function
1 parent f0c2c11 commit c1c3eca

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-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: 23 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';
@@ -103,6 +104,17 @@ describe('CustomerTableListComponent', () => {
103104
expect(store.dispatch).toHaveBeenCalledWith(new SetCustomerToEdit('1'));
104105
});
105106

107+
it('when you click close modal, if the idToEdit is equal currentCustomerIdToEdit should dispatch ResetCustomerToEdit', () => {
108+
109+
spyOn(store, 'dispatch');
110+
111+
component.idToEdit = '1';
112+
component.currentCustomerIdToEdit = '1';
113+
component.closeModal();
114+
115+
expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
116+
});
117+
106118
it('onClick delete, dispatch DeleteCustomer', () => {
107119
spyOn(store, 'dispatch');
108120
component.idToDelete = '1';
@@ -111,6 +123,17 @@ describe('CustomerTableListComponent', () => {
111123
expect(store.dispatch).toHaveBeenCalledWith(new DeleteCustomer('1'));
112124
});
113125

126+
it('onClick delete, if idToDelete is equal to currentCustomerIdToEdit should dispatch ResetCustomerToEdit', () => {
127+
128+
spyOn(store, 'dispatch');
129+
130+
component.idToDelete = '1';
131+
component.currentCustomerIdToEdit = '1';
132+
component.deleteCustomer();
133+
134+
expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
135+
});
136+
114137
const params = [
115138
{ actionName: 'delete', actionType: CustomerManagementActionTypes.DELETE_CUSTOMER_SUCCESS },
116139
{ actionName: 'update', actionType: CustomerManagementActionTypes.UPDATE_CUSTOMER_SUCCESS },

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

Lines changed: 26 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,9 +35,11 @@ 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;
42+
currentCustomerIdToEdit: string;
3743
message: string;
3844
isLoading$: Observable<boolean>;
3945

@@ -47,6 +53,12 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
4753
paging: false,
4854
responsive: true,
4955
};
56+
57+
const customerIdToEdit$ = this.store.pipe(select(customerIdtoEdit));
58+
this.customerIdToEditSubscription = customerIdToEdit$.subscribe((customerId: string) => {
59+
this.currentCustomerIdToEdit = customerId;
60+
});
61+
5062
this.loadCustomersSubscription = this.actionsSubject$
5163
.pipe(filter((action: any) => action.type === CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS))
5264
.subscribe((action) => {
@@ -76,6 +88,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
7688
ngOnDestroy() {
7789
this.loadCustomersSubscription.unsubscribe();
7890
this.changeCustomerSubscription.unsubscribe();
91+
this.customerIdToEditSubscription.unsubscribe();
7992
this.dtTrigger.unsubscribe();
8093
}
8194

@@ -98,6 +111,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
98111
this.showModal = false;
99112
this.changeValueShowCustomerForm.emit(this.showCustomerForm);
100113
this.resetProjectFieldsToEdit();
114+
this.checkResetCustomerToEdit(this.idToEdit);
101115
this.store.dispatch(new SetCustomerToEdit(this.idToEdit));
102116
}
103117

@@ -109,6 +123,9 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
109123
}
110124

111125
deleteCustomer() {
126+
if (this.checkResetCustomerToEdit(this.idToDelete)) {
127+
this.resetProjectFieldsToEdit();
128+
}
112129
this.store.dispatch(new DeleteCustomer(this.idToDelete));
113130
this.showModal = false;
114131
}
@@ -124,6 +141,14 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
124141
}
125142
}
126143

144+
private checkResetCustomerToEdit(id: string): boolean {
145+
const isResetCustomerToEdit = this.currentCustomerIdToEdit === id;
146+
if (isResetCustomerToEdit) {
147+
this.store.dispatch(new ResetCustomerToEdit());
148+
}
149+
return isResetCustomerToEdit;
150+
}
151+
127152
openModal(item: Customer) {
128153
this.idToDelete = item.id;
129154
this.message = `Are you sure you want to delete ${item.name}?`;

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)