-
Notifications
You must be signed in to change notification settings - Fork 1
87 save customers #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
87 save customers #139
Changes from 1 commit
016d0c6
d038c10
4863d19
f2f62e4
2cc4c8c
2e9aef9
b760c32
925becb
d4ff348
b2a4427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
||
| import { Customer } from 'src/app/modules/shared/models/customer.model'; | ||
|
||
|
|
||
| describe('CreateCustomerComponent', () => { | ||
|
|
@@ -17,6 +17,12 @@ describe('CreateCustomerComponent', () => { | |
| message: '', | ||
| }; | ||
|
|
||
| const customerData: Customer = { | ||
| name: 'aa', | ||
| description: 'bb', | ||
| tenant_id: 'cc', | ||
| }; | ||
|
|
||
| beforeEach(async(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CreateCustomerComponent], | ||
|
|
@@ -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!'); | ||
|
||
| expect(component.isActiveItemTabs).toBeTrue(); | ||
| }); | ||
|
|
||
| it('should be disabled tabs and show message Something went wrong creating customer! ', () => { | ||
|
||
| 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'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please sort imports by module.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
| @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({ | ||
|
|
@@ -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); | ||
|
||
| }); | ||
| } | ||
|
|
||
| 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,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; | ||
| } | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
Customer created successfully!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done