Skip to content

Commit 016d0c6

Browse files
committed
fix: #87 c1-save-customers
1 parent c772336 commit 016d0c6

File tree

53 files changed

+425
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+425
-65
lines changed

src/app/app-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { TimeEntriesComponent } from './modules/time-entries/pages/time-entries.
88
import { ActivitiesManagementComponent } from './modules/activities-management/pages/activities-management.component';
99
import { HomeComponent } from './modules/home/home.component';
1010
import { LoginComponent } from './modules/login/login.component';
11-
import { CustomerComponent } from './modules/customer-managment/pages/customer.component';
11+
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
1212

1313
const routes: Routes = [
1414
{

src/app/app.module.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ import { ProjectEffects } from './modules/project-management/store/project.effec
3636
import { TechnologyEffects } from './modules/shared/store/technology.effects';
3737
import { reducers, metaReducers } from './reducers';
3838
import { environment } from '../environments/environment';
39-
import { CustomerComponent } from './modules/customer-managment/pages/customer.component';
39+
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
4040
// tslint:disable-next-line: max-line-length
41-
import { CustomerListComponent } from './modules/customer-managment/components/customer-info/components/customer-list/customer-list.component';
42-
import { SearchComponent } from './modules/customer-managment/components/search/search.component';
41+
import { CustomerListComponent } from './modules/customer-management/components/customer-info/components/customer-list/customer-list.component';
42+
import { SearchComponent } from './modules/customer-management/components/search/search.component';
4343
// tslint:disable-next-line: max-line-length
44-
import { ManagementCustomerProjectsComponent } from './modules/customer-managment/components/management-customer-projects/management-customer-projects.component';
45-
import { CreateCustomerComponent } from './modules/customer-managment/components/customer-info/components/create-customer/create-customer';
44+
import { ManagementCustomerProjectsComponent } from './modules/customer-management/components/management-customer-projects/management-customer-projects.component';
45+
import { CreateCustomerComponent } from './modules/customer-management/components/customer-info/components/create-customer/create-customer';
4646
// tslint:disable-next-line: max-line-length
47-
import { CreateProjectComponent } from './modules/customer-managment/components/projects/components/create-project/create-project.component';
48-
import { ProjectListComponent } from './modules/customer-managment/components/projects/components/project-list/project-list.component';
47+
import { CreateProjectComponent } from './modules/customer-management/components/projects/components/create-project/create-project.component';
48+
import { ProjectListComponent } from './modules/customer-management/components/projects/components/project-list/project-list.component';
4949
// tslint:disable-next-line: max-line-length
50-
import { ProjectTypeListComponent } from './modules/customer-managment/components/projects-type/components/project-type-list/project-type-list.component';
50+
import { ProjectTypeListComponent } from './modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component';
5151
// tslint:disable-next-line: max-line-length
52-
import { CreateProjectTypeComponent } from './modules/customer-managment/components/projects-type/components/create-project-type/create-project-type.component';
52+
import { CreateProjectTypeComponent } from './modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component';
53+
import { CustomerEffects } from './modules/customer-management/store/customer-management.effects';
5354

5455
@NgModule({
5556
declarations: [
@@ -102,7 +103,7 @@ import { CreateProjectTypeComponent } from './modules/customer-managment/compone
102103
maxAge: 15, // Retains last 15 states
103104
})
104105
: [],
105-
EffectsModule.forRoot([ProjectEffects, ActivityEffects, TechnologyEffects]),
106+
EffectsModule.forRoot([ProjectEffects, ActivityEffects, CustomerEffects, TechnologyEffects]),
106107
],
107108
providers: [],
108109
bootstrap: [AppComponent],
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<div class="container">
2+
<form style="width: 600px;" [formGroup]="customerForm" (ngSubmit)="onSubmit(customerForm.value)">
3+
<div class="form-group">
4+
<div
5+
*ngIf="messageToShow != ''"
6+
[ngClass]="{'bg-secondary': messageToShow == 'Data create successfully!', 'bg-primary': messageToShow != 'Data create successfully!'}"
7+
class="alert alert-dismissible fade fade-in show text-white"
8+
role="alert"
9+
>
10+
<strong>{{ messageToShow }}</strong>
11+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
12+
<span aria-hidden="true">&times;</span>
13+
</button>
14+
</div>
15+
16+
<input
17+
class="form-control form-control-sm"
18+
id="name"
19+
type="text"
20+
formControlName="name"
21+
placeholder="Customer name"
22+
[class.is-invalid]="customerForm.invalid && customerForm.touched"
23+
required
24+
/>
25+
<span
26+
class="badge badge-pill badge-light text-danger"
27+
*ngIf="(customerForm.dirty || customerForm.touched) && customerForm.invalid"
28+
>Activity name is required</span
29+
>
30+
<textarea
31+
class="form-control form-control-sm mt-2"
32+
id="description"
33+
rows="3"
34+
formControlName="description"
35+
placeholder="Customer description"
36+
></textarea>
37+
<button type="submit" class="btn btn-sm btn-primary" [disabled]="!customerForm.valid">Save</button>
38+
<button (click)="resetCustomerForm()" id="cancel" type="button" class="btn btn-sm btn-secondary mb-2 ml-2 mt-2">
39+
Cancel
40+
</button>
41+
</div>
42+
</form>
43+
</div>
File renamed without changes.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { CreateCustomerComponent } from './create-customer';
4+
import { MockStore, provideMockStore } from '@ngrx/store/testing';
5+
import { CustomerState, CreateCustomer, CreateCustomerSuccess } from 'src/app/modules/customer-management/store';
6+
import { FormBuilder, FormGroup } from '@angular/forms';
7+
import { By } from '@angular/platform-browser';
8+
import { Customer } from 'src/app/modules/shared/models/customer.model';
9+
import { of } from 'rxjs';
10+
11+
describe('CreateCustomerComponent', () => {
12+
let component: CreateCustomerComponent;
13+
let fixture: ComponentFixture<CreateCustomerComponent>;
14+
let store: MockStore<CustomerState>;
15+
16+
const state = {
17+
data: [],
18+
isLoading: false,
19+
message: '',
20+
};
21+
22+
const message = {
23+
message() {
24+
const messageValue = 'Sucess';
25+
return messageValue;
26+
},
27+
};
28+
29+
beforeEach(async(() => {
30+
TestBed.configureTestingModule({
31+
declarations: [CreateCustomerComponent],
32+
providers: [FormBuilder, provideMockStore({ initialState: state })],
33+
}).compileComponents();
34+
}));
35+
36+
beforeEach(() => {
37+
fixture = TestBed.createComponent(CreateCustomerComponent);
38+
component = fixture.componentInstance;
39+
fixture.detectChanges();
40+
41+
store = TestBed.inject(MockStore);
42+
store.setState(state);
43+
});
44+
45+
afterEach(() => {
46+
fixture.destroy();
47+
});
48+
49+
it('component should be created', () => {
50+
expect(component).toBeTruthy();
51+
});
52+
53+
it('should call resetCustomerForm', async(() => {
54+
spyOn(component.customerForm, 'reset');
55+
56+
component.resetCustomerForm();
57+
58+
expect(component.customerForm.reset).toHaveBeenCalled();
59+
}));
60+
61+
it('onSubmit and dispatch CreateCustomer action', async(() => {
62+
const customerData: Customer = {
63+
name: 'aa',
64+
description: 'bb',
65+
tenant_id: 'cc',
66+
};
67+
68+
spyOn(store, 'dispatch');
69+
70+
component.onSubmit(customerData);
71+
72+
expect(store.dispatch).toHaveBeenCalledTimes(1);
73+
expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData));
74+
}));
75+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Component, Input, Output, EventEmitter } from '@angular/core';
2+
import { Store } from '@ngrx/store';
3+
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
4+
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
5+
6+
@Component({
7+
selector: 'app-create-customer',
8+
templateUrl: './create-customer.html',
9+
styleUrls: ['./create-customer.scss'],
10+
})
11+
export class CreateCustomerComponent {
12+
customerForm: FormGroup;
13+
@Input() isActiveItemTabs: boolean;
14+
@Output() changeValueIsActiveItemTabs = new EventEmitter<boolean>();
15+
showSuccessAlert = false;
16+
messageToShow = '';
17+
response = '';
18+
19+
constructor(private formBuilder: FormBuilder, private store: Store<CustomerState>) {
20+
this.customerForm = this.formBuilder.group({
21+
name: ['', Validators.required],
22+
description: [''],
23+
});
24+
}
25+
26+
onSubmit(customerData) {
27+
this.store.dispatch(new CreateCustomer(customerData));
28+
this.store
29+
.select((state) => state)
30+
.subscribe((state) => {
31+
this.response = Object.values(state)[2].message;
32+
if (this.response === 'Data create successfully!') {
33+
this.isActiveItemTabs = true;
34+
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
35+
this.messageToShow = this.response;
36+
} else {
37+
this.messageToShow = this.response;
38+
}
39+
});
40+
this.messageToShow = '';
41+
}
42+
43+
resetCustomerForm() {
44+
this.customerForm.reset();
45+
}
46+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)