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: TT-29 Clean the form if the customer is removed
  • Loading branch information
LEON12699 committed Apr 13, 2021
commit 083ad7d3794e5586a61d413448703ccb99cff52d
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 @@ -111,6 +112,17 @@ describe('CustomerTableListComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new DeleteCustomer('1'));
});

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

spyOn(store, 'dispatch');

component.idToDelete = '1';
component.idToEdit = '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,6 +35,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
dtElement: DataTableDirective;
loadCustomersSubscription: Subscription;
changeCustomerSubscription: Subscription;
customerIdToEditSubscription: Subscription;
showModal = false;
idToDelete: string;
idToEdit: string;
Expand All @@ -47,6 +52,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.idToEdit = customerId;
});

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

Expand Down Expand Up @@ -109,6 +121,10 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
}

deleteCustomer() {
if (this.idToDelete === this.idToEdit) {
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