Skip to content

Commit cba828e

Browse files
committed
fix: #89 added reset_id_customer and solve some issues
1 parent b0b90d7 commit cba828e

File tree

9 files changed

+51
-19
lines changed

9 files changed

+51
-19
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<input
1515
class="form-control form-control-sm"
16-
id="name"
16+
id="customerName"
1717
type="text"
1818
formControlName="name"
1919
placeholder="Customer name"
@@ -27,7 +27,7 @@
2727
>
2828
<textarea
2929
class="form-control form-control-sm mt-2"
30-
id="description"
30+
id="customerDescription"
3131
rows="3"
3232
formControlName="description"
3333
placeholder="Customer description"

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
44

55
import { CreateCustomerComponent } from './create-customer';
66
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
7-
import { LoadCustomers } from './../../../../store/customer-management.actions';
7+
import { LoadCustomers, ResetCustomerToEdit } from './../../../../store/customer-management.actions';
88
import { Customer } from 'src/app/modules/shared/models';
99

1010
describe('CreateCustomerComponent', () => {
@@ -68,9 +68,12 @@ describe('CreateCustomerComponent', () => {
6868

6969
it('should call resetCustomerForm', () => {
7070
spyOn(component.customerForm, 'reset');
71+
spyOn(store, 'dispatch');
7172

7273
component.resetCustomerForm();
7374

75+
expect(store.dispatch).toHaveBeenCalledTimes(1);
76+
expect(store.dispatch).toHaveBeenCalledWith(new ResetCustomerToEdit());
7477
expect(component.customerForm.reset).toHaveBeenCalled();
7578
});
7679

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
CreateCustomer,
1111
LoadCustomers,
1212
UpdateCustomer,
13+
ResetCustomerToEdit,
1314
} from 'src/app/modules/customer-management/store';
1415

1516
@Component({
@@ -92,7 +93,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
9293
}
9394

9495
resetCustomerForm() {
95-
this.showAlert = false;
9696
this.customerForm.reset();
97+
this.store.dispatch(new ResetCustomerToEdit());
9798
}
9899
}

src/app/modules/customer-management/services/customer.service.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ export class CustomerService {
1414
constructor(private http: HttpClient, private service: AzureAdB2CService) {}
1515

1616
createCustomer(customerData): Observable<any> {
17-
const body = {
18-
...customerData,
19-
tenant_id: this.service.getTenantId(),
20-
};
21-
return this.http.post(this.baseUrl, body);
17+
return this.http.post(this.baseUrl, customerData);
2218
}
2319

2420
getCustomers(): Observable<any> {
@@ -32,9 +28,6 @@ export class CustomerService {
3228

3329
updateCustomer(customerData): Observable<any> {
3430
const url = `${this.baseUrl}/${customerData.id}`;
35-
const body = {
36-
...customerData,
37-
};
38-
return this.http.put(url, body);
31+
return this.http.put(url, customerData);
3932
}
4033
}

src/app/modules/customer-management/store/customer-management.actions.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ describe('CustomerManagmentActions', () => {
7676
const setCustomerToEdit = new actions.SetCustomerToEdit('abc');
7777
expect(setCustomerToEdit.type).toEqual(actions.CustomerManagementActionTypes.SET_CUSTOMER_ID_TO_EDIT);
7878
});
79+
80+
it('ResetCustomerToEdit type is CustomerManagementActionTypes.RESET_CustomerId_ID_TO_EDIT', () => {
81+
const resetCustomerIdToEdit = new actions.ResetCustomerToEdit();
82+
expect(resetCustomerIdToEdit.type).toEqual(actions.CustomerManagementActionTypes.RESET_CUSTOMER_ID_TO_EDIT);
83+
});
7984
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export enum CustomerManagementActionTypes {
1515
UPDATE_CUSTOMER_SUCCESS = '[CustomerManagement] UPDATE_CUSTOMER_SUCCESS',
1616
UPDATE_CUSTOMER_FAIL = '[CustomerManagement] UPDATE_CUSTOMER_FAIL',
1717
SET_CUSTOMER_ID_TO_EDIT = '[CustomerManagement] SET_CUSTOMER_ID_TO_EDIT',
18+
RESET_CUSTOMER_ID_TO_EDIT = '[CustomerManagement] RESET_CUSTOMER_ID_TO_EDIT',
1819
}
1920

2021
export class LoadCustomers implements Action {
@@ -92,6 +93,10 @@ export class SetCustomerToEdit implements Action {
9293
constructor(public payload: string) {}
9394
}
9495

96+
export class ResetCustomerToEdit implements Action {
97+
public readonly type = CustomerManagementActionTypes.RESET_CUSTOMER_ID_TO_EDIT;
98+
}
99+
95100
export type CustomerManagementActions =
96101
| CreateCustomer
97102
| CreateCustomerSuccess
@@ -105,4 +110,5 @@ export type CustomerManagementActions =
105110
| UpdateCustomer
106111
| UpdateCustomerSuccess
107112
| UpdateCustomerFail
108-
| SetCustomerToEdit;
113+
| SetCustomerToEdit
114+
| ResetCustomerToEdit;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,12 @@ describe('customerManagementReducer', () => {
123123

124124
expect(state.customerIdToEdit).toEqual('1');
125125
});
126+
127+
it('on ResetCustomerToEdit, should clean the customerIdToEdit variable', () => {
128+
const action = new actions.ResetCustomerToEdit();
129+
130+
const state = customerManagementReducer(initialState, action);
131+
132+
expect(state.customerIdToEdit).toEqual('');
133+
});
126134
});

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a
6161
data: state.data,
6262
isLoading: false,
6363
message: 'An error occurred, try again later.',
64+
customerIdToEdit: '',
6465
};
6566
}
6667

@@ -87,6 +88,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a
8788
data: state.data,
8889
isLoading: false,
8990
message: 'Something went wrong deleting customer!',
91+
customerIdToEdit: '',
9092
};
9193
}
9294

@@ -126,6 +128,13 @@ export const customerManagementReducer = (state: CustomerState = initialState, a
126128
};
127129
}
128130

131+
case CustomerManagementActionTypes.RESET_CUSTOMER_ID_TO_EDIT: {
132+
return {
133+
...state,
134+
customerIdToEdit: '',
135+
};
136+
}
137+
129138
default:
130139
return state;
131140
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createFeatureSelector, createSelector } from '@ngrx/store';
2+
import { Customer } from 'src/app/modules/shared/models/customer.model';
23

34
import { CustomerState } from './customer-management.reducers';
45
export const getCustomerState = createFeatureSelector<CustomerState>('customers');
@@ -10,15 +11,21 @@ export const getStatusMessage = createSelector(getCustomerState, (messageState)
1011
});
1112

1213
export const allCustomers = createSelector(getCustomerState, (state: CustomerState) => {
13-
return state.data;
14+
if (state) {
15+
return state.data;
16+
}
1417
});
1518

1619
export const customerIdtoEdit = createSelector(getCustomerState, (state: CustomerState) => {
17-
return state.customerIdToEdit;
20+
if (state) {
21+
return state.customerIdToEdit;
22+
}
1823
});
1924

2025
export const getCustomerById = createSelector(allCustomers, customerIdtoEdit, (customers, customerId) => {
21-
return customers.find((customer) => {
22-
return customer.id === customerId;
23-
});
26+
if (customers) {
27+
return customers.find((customer) => {
28+
return customer.id === customerId;
29+
});
30+
}
2431
});

0 commit comments

Comments
 (0)