Skip to content

Commit 4863d19

Browse files
committed
fix: #87 c3-save-customers
1 parent d038c10 commit 4863d19

File tree

8 files changed

+100
-37
lines changed

8 files changed

+100
-37
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
<form style="width: 600px;" [formGroup]="customerForm" (ngSubmit)="onSubmit(customerForm.value)">
33
<div class="form-group">
44
<div
5-
*ngIf="messageToShow != ''"
6-
[ngClass]="{'bg-secondary': messageToShow == 'Data create successfully!', 'bg-primary': messageToShow != 'Data create successfully!'}"
5+
*ngIf="showAlert && messageToShow !== ''"
6+
[ngClass]="{'bg-secondary': messageToShow == 'Customer create successfully!', 'bg-primary': messageToShow != 'Customer create successfully!'}"
77
class="alert alert-dismissible fade fade-in show text-white"
88
role="alert"
99
>
1010
<strong>{{ messageToShow }}</strong>
11-
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
12-
<span aria-hidden="true">&times;</span>
13-
</button>
1411
</div>
1512

1613
<input

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
33
import { CreateCustomerComponent } from './create-customer';
44
import { MockStore, provideMockStore } from '@ngrx/store/testing';
55
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
6-
import { FormBuilder, FormGroup } from '@angular/forms';
6+
import { FormBuilder } from '@angular/forms';
77
import { Customer } from 'src/app/modules/shared/models/customer.model';
88

99
describe('CreateCustomerComponent', () => {
@@ -17,6 +17,12 @@ describe('CreateCustomerComponent', () => {
1717
message: '',
1818
};
1919

20+
const customerData: Customer = {
21+
name: 'aa',
22+
description: 'bb',
23+
tenant_id: 'cc',
24+
};
25+
2026
beforeEach(async(() => {
2127
TestBed.configureTestingModule({
2228
declarations: [CreateCustomerComponent],
@@ -41,26 +47,60 @@ describe('CreateCustomerComponent', () => {
4147
expect(component).toBeTruthy();
4248
});
4349

44-
it('should call resetCustomerForm', async(() => {
50+
it('should call resetCustomerForm', () => {
4551
spyOn(component.customerForm, 'reset');
4652

4753
component.resetCustomerForm();
4854

4955
expect(component.customerForm.reset).toHaveBeenCalled();
50-
}));
51-
52-
it('onSubmit and dispatch CreateCustomer action', async(() => {
53-
const customerData: Customer = {
54-
name: 'aa',
55-
description: 'bb',
56-
tenant_id: 'cc',
57-
};
56+
});
5857

58+
it('onSubmit and dispatch CreateCustomer action', () => {
5959
spyOn(store, 'dispatch');
6060

6161
component.onSubmit(customerData);
6262

6363
expect(store.dispatch).toHaveBeenCalledTimes(1);
6464
expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData));
65-
}));
65+
});
66+
67+
it('should call resetCustomerForm', () => {
68+
spyOn(component.customerForm, 'reset');
69+
70+
component.resetCustomerForm();
71+
72+
expect(component.customerForm.reset).toHaveBeenCalled();
73+
});
74+
75+
it('should be enable tabs and show message Customer create successfully! ', () => {
76+
component.isActiveItemTabs = false;
77+
component.messageToShow = '';
78+
79+
spyOn(store, 'dispatch');
80+
81+
component.ngOnInit();
82+
83+
component.onSubmit(customerData);
84+
85+
component.setStatusOnScreen('Customer create successfully!');
86+
87+
expect(component.messageToShow).toEqual('Customer create successfully!');
88+
expect(component.isActiveItemTabs).toBeTrue();
89+
});
90+
91+
it('should be disabled tabs and show message Something went wrong creating customer! ', () => {
92+
component.isActiveItemTabs = false;
93+
component.messageToShow = '';
94+
95+
spyOn(store, 'dispatch');
96+
97+
component.ngOnInit();
98+
99+
component.onSubmit(customerData);
100+
101+
component.setStatusOnScreen('Something went wrong creating customer!');
102+
103+
expect(component.messageToShow).toEqual('Something went wrong creating customer!');
104+
expect(component.isActiveItemTabs).toBeFalse();
105+
});
66106
});
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { Component, Input, Output, EventEmitter } from '@angular/core';
2-
import { Store } from '@ngrx/store';
1+
import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
2+
import { Store, select } from '@ngrx/store';
33
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
44
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
5+
import { Subscription } from 'rxjs';
6+
import { getStatusMessage } from 'src/app/modules/customer-management/store/customer-management.selectors';
57

68
@Component({
79
selector: 'app-create-customer',
810
templateUrl: './create-customer.html',
911
styleUrls: ['./create-customer.scss'],
1012
})
11-
export class CreateCustomerComponent {
13+
export class CreateCustomerComponent implements OnInit, OnDestroy {
1214
customerForm: FormGroup;
1315
@Input() isActiveItemTabs: boolean;
1416
@Output() changeValueIsActiveItemTabs = new EventEmitter<boolean>();
15-
showSuccessAlert = false;
17+
showAlert = false;
1618
messageToShow = '';
17-
response = '';
19+
saveSubscription: Subscription;
1820

1921
constructor(private formBuilder: FormBuilder, private store: Store<CustomerState>) {
2022
this.customerForm = this.formBuilder.group({
@@ -23,22 +25,38 @@ export class CreateCustomerComponent {
2325
});
2426
}
2527

28+
ngOnInit() {
29+
const messages$ = this.store.pipe(select(getStatusMessage));
30+
this.saveSubscription = messages$.subscribe((valueMessage) => {
31+
this.setStatusOnScreen(valueMessage);
32+
console.log(valueMessage);
33+
});
34+
}
35+
36+
ngOnDestroy() {
37+
this.isActiveItemTabs = false;
38+
this.saveSubscription.unsubscribe();
39+
}
40+
2641
onSubmit(customerData) {
2742
this.store.dispatch(new CreateCustomer(customerData));
28-
this.store.subscribe((state) => {
29-
this.response = Object.values(state)[2].message;
30-
if (this.response === 'Data create successfully!' || undefined) {
31-
this.isActiveItemTabs = true;
32-
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
33-
this.messageToShow = this.response;
34-
} else {
35-
this.messageToShow = this.response;
36-
}
37-
});
38-
this.messageToShow = '';
43+
this.showAlert = true;
44+
}
45+
46+
setStatusOnScreen(message: string) {
47+
if (message === 'Customer create successfully!') {
48+
this.messageToShow = message;
49+
this.isActiveItemTabs = true;
50+
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
51+
} else if (message === 'Something went wrong creating customer!') {
52+
this.messageToShow = message;
53+
this.isActiveItemTabs = false;
54+
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
55+
}
3956
}
4057

4158
resetCustomerForm() {
59+
this.showAlert = false;
4260
this.customerForm.reset();
4361
}
4462
}

src/app/modules/customer-management/components/management-customer-projects/management-customer-projects.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input } from '@angular/core';
1+
import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-management-customer-projects',

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function customerManagementReducer(
3030
...state,
3131
data: [action.payload],
3232
isLoading: false,
33-
message: 'Data create successfully!',
33+
message: 'Customer create successfully!',
3434
};
3535
}
3636

@@ -42,5 +42,8 @@ export function customerManagementReducer(
4242
message: 'Something went wrong creating customer!',
4343
};
4444
}
45+
46+
default:
47+
return state;
4548
}
4649
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { createFeatureSelector, createSelector } from '@ngrx/store';
22

33
import { CustomerState } from './customer-management.reducers';
4+
export const getCustomerState = createFeatureSelector<CustomerState>('customers');
45

5-
const getCustomerState = createFeatureSelector<CustomerState>('customers');
6+
export const getStatusMessage = createSelector(getCustomerState, (messageState) => {
7+
if (messageState) {
8+
return messageState.message;
9+
}
10+
});

src/app/reducers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface State {
1515
export const reducers: ActionReducerMap<State> = {
1616
projects: projectReducer,
1717
activities: activityManagementReducer,
18-
technologies: technologyReducer,
1918
customers: customerManagementReducer,
19+
technologies: technologyReducer,
2020
};
2121

2222
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];

src/environments/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as keys from './keys';
55

66
export const environment = {
77
production: false,
8-
timeTrackerApiUrl: 'https://timetracker-api.azurewebsites.net',
8+
timeTrackerApiUrl: 'https://tsheets-apim.azure-api.net',
99
stackexchangeApiUrl: 'https://api.stackexchange.com',
1010
};
1111

0 commit comments

Comments
 (0)