Skip to content
Prev Previous commit
Next Next commit
fix: #87 c3-save-customers
  • Loading branch information
daros10 committed Apr 17, 2020
commit 4863d19f218317d6082df717e5687fee9ecd54b2
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<form style="width: 600px;" [formGroup]="customerForm" (ngSubmit)="onSubmit(customerForm.value)">
<div class="form-group">
<div
*ngIf="messageToShow != ''"
[ngClass]="{'bg-secondary': messageToShow == 'Data create successfully!', 'bg-primary': messageToShow != 'Data create successfully!'}"
*ngIf="showAlert && messageToShow !== ''"
[ngClass]="{'bg-secondary': messageToShow == 'Customer create successfully!', 'bg-primary': messageToShow != 'Customer create successfully!'}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:
Customer created successfully!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

class="alert alert-dismissible fade fade-in show text-white"
role="alert"
>
<strong>{{ messageToShow }}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>

<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateCustomerComponent } from './create-customer';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
import { FormBuilder, FormGroup } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sort imports by module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

import { Customer } from 'src/app/modules/shared/models/customer.model';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The customer.model must be added in the index file in 'src/app/modules/shared/models/index.ts'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


describe('CreateCustomerComponent', () => {
Expand All @@ -17,6 +17,12 @@ describe('CreateCustomerComponent', () => {
message: '',
};

const customerData: Customer = {
name: 'aa',
description: 'bb',
tenant_id: 'cc',
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [CreateCustomerComponent],
Expand All @@ -41,26 +47,60 @@ describe('CreateCustomerComponent', () => {
expect(component).toBeTruthy();
});

it('should call resetCustomerForm', async(() => {
it('should call resetCustomerForm', () => {
spyOn(component.customerForm, 'reset');

component.resetCustomerForm();

expect(component.customerForm.reset).toHaveBeenCalled();
}));

it('onSubmit and dispatch CreateCustomer action', async(() => {
const customerData: Customer = {
name: 'aa',
description: 'bb',
tenant_id: 'cc',
};
});

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

component.onSubmit(customerData);

expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData));
}));
});

it('should call resetCustomerForm', () => {
spyOn(component.customerForm, 'reset');

component.resetCustomerForm();

expect(component.customerForm.reset).toHaveBeenCalled();
});

it('should be enable tabs and show message Customer create successfully! ', () => {
component.isActiveItemTabs = false;
component.messageToShow = '';

spyOn(store, 'dispatch');

component.ngOnInit();

component.onSubmit(customerData);

component.setStatusOnScreen('Customer create successfully!');

expect(component.messageToShow).toEqual('Customer create successfully!');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Customer created successfully

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expect(component.isActiveItemTabs).toBeTrue();
});

it('should be disabled tabs and show message Something went wrong creating customer! ', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it('should disable tabs and show a message "An error occurred, try again later." '

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

component.isActiveItemTabs = false;
component.messageToShow = '';

spyOn(store, 'dispatch');

component.ngOnInit();

component.onSubmit(customerData);

component.setStatusOnScreen('Something went wrong creating customer!');

expect(component.messageToShow).toEqual('Something went wrong creating customer!');
expect(component.isActiveItemTabs).toBeFalse();
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { Store } from '@ngrx/store';
import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sort imports by module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

import { Subscription } from 'rxjs';
import { getStatusMessage } from 'src/app/modules/customer-management/store/customer-management.selectors';

@Component({
selector: 'app-create-customer',
templateUrl: './create-customer.html',
styleUrls: ['./create-customer.scss'],
})
export class CreateCustomerComponent {
export class CreateCustomerComponent implements OnInit, OnDestroy {
customerForm: FormGroup;
@Input() isActiveItemTabs: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name would be:
areTabsActive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Output() changeValueIsActiveItemTabs = new EventEmitter<boolean>();
showSuccessAlert = false;
showAlert = false;
messageToShow = '';
response = '';
saveSubscription: Subscription;

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

ngOnInit() {
const messages$ = this.store.pipe(select(getStatusMessage));
this.saveSubscription = messages$.subscribe((valueMessage) => {
this.setStatusOnScreen(valueMessage);
console.log(valueMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, delete this console.log

Copy link
Contributor Author

@daros10 daros10 Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

});
}

ngOnDestroy() {
this.isActiveItemTabs = false;
this.saveSubscription.unsubscribe();
}

onSubmit(customerData) {
this.store.dispatch(new CreateCustomer(customerData));
this.store.subscribe((state) => {
this.response = Object.values(state)[2].message;
if (this.response === 'Data create successfully!' || undefined) {
this.isActiveItemTabs = true;
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
this.messageToShow = this.response;
} else {
this.messageToShow = this.response;
}
});
this.messageToShow = '';
this.showAlert = true;
}

setStatusOnScreen(message: string) {
if (message === 'Customer create successfully!') {
this.messageToShow = message;
this.isActiveItemTabs = true;
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
} else if (message === 'Something went wrong creating customer!') {
this.messageToShow = message;
this.isActiveItemTabs = false;
this.changeValueIsActiveItemTabs.emit(this.isActiveItemTabs);
}
}

resetCustomerForm() {
this.showAlert = false;
this.customerForm.reset();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input } from '@angular/core';
import { Component } from '@angular/core';

@Component({
selector: 'app-management-customer-projects',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function customerManagementReducer(
...state,
data: [action.payload],
isLoading: false,
message: 'Data create successfully!',
message: 'Customer create successfully!',
};
}

Expand All @@ -42,5 +42,8 @@ export function customerManagementReducer(
message: 'Something went wrong creating customer!',
};
}

default:
return state;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';

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

const getCustomerState = createFeatureSelector<CustomerState>('customers');
export const getStatusMessage = createSelector(getCustomerState, (messageState) => {
if (messageState) {
return messageState.message;
}
});
2 changes: 1 addition & 1 deletion src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export interface State {
export const reducers: ActionReducerMap<State> = {
projects: projectReducer,
activities: activityManagementReducer,
technologies: technologyReducer,
customers: customerManagementReducer,
technologies: technologyReducer,
};

export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
2 changes: 1 addition & 1 deletion src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as keys from './keys';

export const environment = {
production: false,
timeTrackerApiUrl: 'https://timetracker-api.azurewebsites.net',
timeTrackerApiUrl: 'https://tsheets-apim.azure-api.net',
stackexchangeApiUrl: 'https://api.stackexchange.com',
};

Expand Down