From 9db1baffecc63ffd387bc943db5e880bf4392a79 Mon Sep 17 00:00:00 2001 From: Diego Tinitana Date: Tue, 28 Apr 2020 18:57:57 -0500 Subject: [PATCH] fix: #154 Filter projects and project types by customer --- .../pages/activities-management.component.ts | 2 +- .../create-customer/create-customer.spec.ts | 6 +++--- .../create-customer/create-customer.ts | 6 ++++-- .../customer-list.component.spec.ts | 1 + .../create-project-type.component.spec.ts | 13 +++++++++--- .../create-project-type.component.ts | 20 ++++++++++++++----- .../project-type-list.component.spec.ts | 7 ------- .../project-type-list.component.ts | 3 +-- .../services/project-type.service.spec.ts | 10 ++++++---- .../services/project-type.service.ts | 7 ++++--- .../store/project-type.actions.ts | 1 + .../store/project-type.effects.ts | 4 ++-- .../create-project.component.spec.ts | 17 ++++++++++++---- .../create-project.component.ts | 10 +++++++++- .../project-list.component.spec.ts | 8 -------- .../project-list/project-list.component.ts | 1 - .../services/project.service.spec.ts | 4 ++-- .../components/services/project.service.ts | 12 +++++------ .../components/store/project.actions.ts | 1 + .../components/store/project.effects.ts | 4 ++-- .../customer-management.reducers.spec.ts | 6 ++++-- .../store/customer-management.reducers.ts | 4 ++++ .../store/customer-management.selectors.ts | 11 +++++++--- .../entry-fields/entry-fields.component.ts | 2 +- 24 files changed, 97 insertions(+), 63 deletions(-) diff --git a/src/app/modules/activities-management/pages/activities-management.component.ts b/src/app/modules/activities-management/pages/activities-management.component.ts index c86dd0595..414c4d13a 100644 --- a/src/app/modules/activities-management/pages/activities-management.component.ts +++ b/src/app/modules/activities-management/pages/activities-management.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; +import { Component, OnInit, OnDestroy } from '@angular/core'; import { ActionsSubject } from '@ngrx/store'; import { Subscription } from 'rxjs'; import { ActivityManagementActionTypes } from '../store'; diff --git a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts index cca6afbf9..5d2509509 100644 --- a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts +++ b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts @@ -4,7 +4,7 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { CreateCustomerComponent } from './create-customer'; import { CustomerState, CreateCustomer } from 'src/app/modules/customer-management/store'; -import { LoadCustomers, ResetCustomerToEdit, UpdateCustomer } from './../../../../store/customer-management.actions'; +import { ResetCustomerToEdit, UpdateCustomer } from './../../../../store/customer-management.actions'; import { Customer } from 'src/app/modules/shared/models'; describe('CreateCustomerComponent', () => { @@ -17,6 +17,7 @@ describe('CreateCustomerComponent', () => { isLoading: false, message: '', customerIdToEdit: '', + customerId: '', }; const customerData: Customer = { @@ -69,9 +70,8 @@ describe('CreateCustomerComponent', () => { component.onSubmit(customerData); - expect(store.dispatch).toHaveBeenCalledTimes(2); + expect(store.dispatch).toHaveBeenCalledTimes(1); expect(store.dispatch).toHaveBeenCalledWith(new CreateCustomer(customerData)); - expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomers()); }); it('should call resetCustomerForm', () => { diff --git a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts index 9605af742..268a3aa44 100644 --- a/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts +++ b/src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts @@ -8,10 +8,11 @@ import { Customer } from 'src/app/modules/shared/models'; import { CustomerState, CreateCustomer, - LoadCustomers, UpdateCustomer, ResetCustomerToEdit, } from 'src/app/modules/customer-management/store'; +import { LoadProjectTypes } from '../../../projects-type/store'; +import { LoadProjects } from '../../../projects/components/store/project.actions'; @Component({ selector: 'app-create-customer', @@ -64,7 +65,6 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { this.customerForm.reset(); } else { this.store.dispatch(new CreateCustomer(customerData)); - this.store.dispatch(new LoadCustomers()); } this.showAlert = true; setTimeout(() => (this.showAlert = false), 3000); @@ -85,6 +85,8 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { setDataToUpdate(customerData: Customer) { if (customerData) { + this.store.dispatch(new LoadProjectTypes(customerData.id)); + this.store.dispatch(new LoadProjects(customerData.id)); this.changeValueAreTabsActives.emit(true); this.customerForm.setValue({ name: customerData.name, diff --git a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts index 4f1d47d7d..073c8d74a 100644 --- a/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts +++ b/src/app/modules/customer-management/components/customer-info/components/customer-list/customer-list.component.spec.ts @@ -17,6 +17,7 @@ describe('CustomerTableListComponent', () => { isLoading: false, message: '', customerIdToEdit: '', + customerId: '' }; beforeEach(async(() => { diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts index e47628c35..e6f872c29 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.spec.ts @@ -12,6 +12,7 @@ import { ResetProjectTypeToEdit, getProjectTypeById, } from '../../store'; +import { getCustomerId } from 'src/app/modules/customer-management/store/customer-management.selectors'; import { ProjectType } from '../../../../../shared/models/project-type.model'; describe('InputProjectTypeComponent', () => { @@ -19,6 +20,7 @@ describe('InputProjectTypeComponent', () => { let fixture: ComponentFixture; let store: MockStore; let projectTypeIdToEditMock; + let getCustomerIdMock; let allProjectTypesMock; let getProjectTypeByIdMock; let getProjectTypeByIdSelectorMock; @@ -68,12 +70,13 @@ describe('InputProjectTypeComponent', () => { it('should reset form onSubmit and dispatch UpdateProjectType action', () => { const currentState = { - data: [{ id: '1', name: 'xxx', description: 'xxxx' }], + data: [{ id: '1', name: 'xxx', description: 'xxxx', customerId: component.customerId }], isLoading: false, message: '', projectTypeIdToEdit: '1', }; + getCustomerIdMock = store.overrideSelector(getCustomerId, 'xyz'); projectTypeIdToEditMock = store.overrideSelector(projectTypeIdToEdit, currentState.projectTypeIdToEdit); allProjectTypesMock = store.overrideSelector(allProjectTypes, currentState.data); getProjectTypeByIdMock = store.overrideSelector(allProjectTypesMock, projectTypeIdToEditMock); @@ -106,12 +109,16 @@ describe('InputProjectTypeComponent', () => { spyOn(component.projectTypeForm, 'reset'); spyOn(store, 'dispatch'); - + component.customerId = ''; component.onSubmit(projectType); + const projectTypeData = { + ...projectType, + customer_id: '', + }; expect(component.projectTypeForm.reset).toHaveBeenCalled(); expect(store.dispatch).toHaveBeenCalledTimes(1); - expect(store.dispatch).toHaveBeenCalledWith(new CreateProjectType(projectType)); + expect(store.dispatch).toHaveBeenCalledWith(new CreateProjectType(projectTypeData)); }); it('should get name using projectTypeForm', () => { diff --git a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts index 41464515d..099e2c425 100644 --- a/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts +++ b/src/app/modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component.ts @@ -1,19 +1,23 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, OnDestroy } from '@angular/core'; import { FormBuilder, Validators, FormGroup } from '@angular/forms'; import { Store, select } from '@ngrx/store'; import { ProjectType } from '../../../../../shared/models'; import { ProjectTypeState } from '../../store'; import { CreateProjectType, ResetProjectTypeToEdit, UpdateProjectType, getProjectTypeById } from '../../store'; +import { getCustomerId } from 'src/app/modules/customer-management/store/customer-management.selectors'; +import { Subscription } from 'rxjs'; @Component({ selector: 'app-create-project-type', templateUrl: './create-project-type.component.html', styleUrls: ['./create-project-type.component.scss'], }) -export class CreateProjectTypeComponent implements OnInit { +export class CreateProjectTypeComponent implements OnInit, OnDestroy { projectTypeForm: FormGroup; projectTypeToEdit: ProjectType; + customerId: string; + getCustomerIdSubscription: Subscription; constructor(private formBuilder: FormBuilder, private store: Store) { this.projectTypeForm = this.formBuilder.group({ @@ -28,12 +32,15 @@ export class CreateProjectTypeComponent implements OnInit { this.projectTypeToEdit = projectType; this.setDataToUpdate(this.projectTypeToEdit); }); + const getCustomerId$ = this.store.pipe(select(getCustomerId)); + this.getCustomerIdSubscription = getCustomerId$.subscribe((customerId) => { + this.customerId = customerId; + }); } get name() { return this.projectTypeForm.get('name'); } - get description() { return this.projectTypeForm.get('description'); } @@ -49,7 +56,6 @@ export class CreateProjectTypeComponent implements OnInit { onSubmit(projectTypeData) { this.projectTypeForm.reset(); - if (this.projectTypeToEdit) { const projectType = { ...projectTypeData, @@ -57,7 +63,7 @@ export class CreateProjectTypeComponent implements OnInit { }; this.store.dispatch(new UpdateProjectType(projectType)); } else { - this.store.dispatch(new CreateProjectType(projectTypeData)); + this.store.dispatch(new CreateProjectType({ ...projectTypeData, customer_id: this.customerId })); this.projectTypeForm.get('description').setValue(''); } } @@ -65,4 +71,8 @@ export class CreateProjectTypeComponent implements OnInit { cancelButton() { this.store.dispatch(new ResetProjectTypeToEdit()); } + + ngOnDestroy(): void { + this.getCustomerIdSubscription.unsubscribe(); + } } diff --git a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts index 21559c777..6f1b8f7e8 100644 --- a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.spec.ts @@ -45,13 +45,6 @@ describe('ProjectTypeTableListComponent', () => { expect(component).toBeTruthy(); }); - it('onInit, LoadProjecttypes action is dispatched', () => { - spyOn(store, 'dispatch'); - - component.ngOnInit(); - expect(store.dispatch).toHaveBeenCalled(); - }); - it('onInit, projectTypes field is populated with data from store', () => { component.ngOnInit(); expect(component.projectTypes).toBe(state.data); diff --git a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts index 069b43d4a..b27a03591 100644 --- a/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts +++ b/src/app/modules/customer-management/components/projects-type/components/project-type-list/project-type-list.component.ts @@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core'; import { ITEMS_PER_PAGE } from 'src/environments/environment'; import { Store, select } from '@ngrx/store'; -import { LoadProjectTypes, DeleteProjectType, SetProjectTypeToEdit, allProjectTypes } from '../../store'; +import { DeleteProjectType, SetProjectTypeToEdit, allProjectTypes } from '../../store'; import { ProjectTypeState } from '../../store'; import { ProjectType } from '../../../../../shared/models'; @@ -20,7 +20,6 @@ export class ProjectTypeListComponent implements OnInit { constructor(private store: Store) {} ngOnInit(): void { - this.store.dispatch(new LoadProjectTypes()); const projectTypes$ = this.store.pipe(select(allProjectTypes)); projectTypes$.subscribe((response) => { this.projectTypes = response; diff --git a/src/app/modules/customer-management/components/projects-type/services/project-type.service.spec.ts b/src/app/modules/customer-management/components/projects-type/services/project-type.service.spec.ts index f8f2c4288..2c7d7c2c3 100644 --- a/src/app/modules/customer-management/components/projects-type/services/project-type.service.spec.ts +++ b/src/app/modules/customer-management/components/projects-type/services/project-type.service.spec.ts @@ -33,11 +33,11 @@ describe('Activity Service', () => { it('projectTypes are read using GET from baseUrl', () => { const projectTypesFoundSize = projectTypes.length; - service.baseUrl = 'foo'; - service.getProjectTypes().subscribe((projecttypesInResponse) => { + service.baseUrl = '/project-types'; + service.getProjectTypes({ customerId: 'xyz' }).subscribe((projecttypesInResponse) => { expect(projecttypesInResponse.length).toBe(projectTypesFoundSize); }); - const getProjectTypesRequest = httpMock.expectOne(service.baseUrl); + const getProjectTypesRequest = httpMock.expectOne(`${service.baseUrl}?customer_id=xyz`); expect(getProjectTypesRequest.request.method).toBe('GET'); getProjectTypesRequest.flush(projectTypes); }); @@ -58,7 +58,9 @@ describe('Activity Service', () => { it('ProjectTypes are delete using DELETE from baseUrl', () => { const url = `${service.baseUrl}/1`; service.deleteProjectType(projectTypes[0].id).subscribe((projectTypesInResponse) => { - expect(projectTypesInResponse.filter((activity) => activity.id !== projectTypes[0].id)).toEqual([projectTypes[1]]); + expect(projectTypesInResponse.filter((activity) => activity.id !== projectTypes[0].id)).toEqual([ + projectTypes[1], + ]); }); const getProjectTypesRequest = httpMock.expectOne(url); expect(getProjectTypesRequest.request.method).toBe('DELETE'); diff --git a/src/app/modules/customer-management/components/projects-type/services/project-type.service.ts b/src/app/modules/customer-management/components/projects-type/services/project-type.service.ts index c12beacbd..6d9e672c2 100644 --- a/src/app/modules/customer-management/components/projects-type/services/project-type.service.ts +++ b/src/app/modules/customer-management/components/projects-type/services/project-type.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs'; import { environment } from '../../../../../../environments/environment'; @@ -13,8 +13,9 @@ export class ProjectTypeService { constructor(private http: HttpClient) {} - getProjectTypes(): Observable { - return this.http.get(this.baseUrl); + getProjectTypes(customerId: any): Observable { + const params = new HttpParams().set('customer_id', customerId.customerId); + return this.http.get(this.baseUrl, { params }); } createProjectType(projectTypeData): Observable { diff --git a/src/app/modules/customer-management/components/projects-type/store/project-type.actions.ts b/src/app/modules/customer-management/components/projects-type/store/project-type.actions.ts index 3ed8b63ad..597d8f76f 100644 --- a/src/app/modules/customer-management/components/projects-type/store/project-type.actions.ts +++ b/src/app/modules/customer-management/components/projects-type/store/project-type.actions.ts @@ -22,6 +22,7 @@ export enum ProjectTypeActionTypes { export class LoadProjectTypes implements Action { public readonly type = ProjectTypeActionTypes.LOAD_PROJECT_TYPES; + constructor(public customerId?: string) {} } export class LoadProjectTypesSuccess implements Action { diff --git a/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts b/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts index 9e8a0063d..12bfa8ab0 100644 --- a/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts +++ b/src/app/modules/customer-management/components/projects-type/store/project-type.effects.ts @@ -15,8 +15,8 @@ export class ProjectTypeEffects { @Effect() getProjectTypes$: Observable = this.actions$.pipe( ofType(actions.ProjectTypeActionTypes.LOAD_PROJECT_TYPES), - mergeMap(() => - this.projectTypeService.getProjectTypes().pipe( + mergeMap((customerId) => + this.projectTypeService.getProjectTypes(customerId).pipe( map((projectTypes: ProjectType[]) => { return new actions.LoadProjectTypesSuccess(projectTypes); }), diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts index 8e2794acf..321b20b8e 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.spec.ts @@ -156,30 +156,39 @@ describe('InputProjectComponent', () => { spyOn(component.projectForm, 'reset'); spyOn(store, 'dispatch'); - + component.customerId = ''; component.onSubmit(projectForm); + const projectData = { + ...projectForm, + customer_id: '', + }; expect(component.projectForm.reset).toHaveBeenCalled(); expect(store.dispatch).toHaveBeenCalledTimes(1); - expect(store.dispatch).toHaveBeenCalledWith(new CreateProject(projectForm)); + expect(store.dispatch).toHaveBeenCalledWith(new CreateProject(projectData)); }); it('should reset form onSubmit and dispatch CreateProject action with null project_type_id', () => { const dataForm = { name: 'Test', description: 'xxx', - project_type_id: '', + project_type_id: null, }; component.projectToEdit = undefined; spyOn(component.projectForm, 'reset'); spyOn(store, 'dispatch'); + component.customerId = ''; + const projectData = { + ...dataForm, + customer_id: '', + }; component.onSubmit(dataForm); expect(dataForm.project_type_id).toBe(null); expect(component.projectForm.reset).toHaveBeenCalled(); expect(store.dispatch).toHaveBeenCalledTimes(1); - expect(store.dispatch).toHaveBeenCalledWith(new CreateProject(dataForm)); + expect(store.dispatch).toHaveBeenCalledWith(new CreateProject(projectData)); }); it('should set data in projectForm', () => { diff --git a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts index 980ca6499..fa303e86d 100644 --- a/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts +++ b/src/app/modules/customer-management/components/projects/components/create-project/create-project.component.ts @@ -8,6 +8,7 @@ import { getProjectToEdit } from '../store/project.selectors'; import { Project, ProjectType } from 'src/app/modules/shared/models'; import { allProjectTypes, ProjectTypeState } from '../../../projects-type/store'; import { Subscription } from 'rxjs'; +import { getCustomerId } from 'src/app/modules/customer-management/store/customer-management.selectors'; @Component({ selector: 'app-create-project', @@ -18,9 +19,11 @@ export class CreateProjectComponent implements OnInit, OnDestroy { projectForm; projectToEdit: Project; projectsTypes: ProjectType[] = []; + customerId: string; projectTypesSubscription: Subscription; projectToEditSubscription: Subscription; + getCustomerIdSubscription: Subscription; constructor( private formBuilder: FormBuilder, private store: Store, @@ -44,11 +47,16 @@ export class CreateProjectComponent implements OnInit, OnDestroy { this.projectTypesSubscription = projectsTypes$.subscribe((projectsType) => { this.projectsTypes = projectsType; }); + const getCustomerId$ = this.store.pipe(select(getCustomerId)); + this.getCustomerIdSubscription = getCustomerId$.subscribe((customerId) => { + this.customerId = customerId; + }); } ngOnDestroy() { this.projectToEditSubscription.unsubscribe(); this.projectTypesSubscription.unsubscribe(); + this.getCustomerIdSubscription.unsubscribe(); } onSubmit(formData) { @@ -60,7 +68,7 @@ export class CreateProjectComponent implements OnInit, OnDestroy { const projectData = { id: this.projectToEdit.id, ...formData }; this.store.dispatch(new actions.UpdateProject(projectData)); } else { - this.store.dispatch(new actions.CreateProject(formData)); + this.store.dispatch(new actions.CreateProject({ ...formData, customer_id: this.customerId })); } this.resetValuesForm(); } diff --git a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts index 2977553a2..f34c14f7a 100644 --- a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts @@ -50,14 +50,6 @@ describe('ProjectListComponent', () => { expect(component).toBeTruthy(); }); - it('onInit, LoadProjects action is dispatched', () => { - spyOn(store, 'dispatch'); - - component.ngOnInit(); - - expect(store.dispatch).toHaveBeenCalled(); - }); - it('should destroy the subscriptions', () => { component.projectsSubscription = new Subscription(); const subscription = spyOn(component.projectsSubscription, 'unsubscribe'); diff --git a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts index f33048c39..d653ffcc3 100644 --- a/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts +++ b/src/app/modules/customer-management/components/projects/components/project-list/project-list.component.ts @@ -24,7 +24,6 @@ export class ProjectListComponent implements OnInit, OnDestroy { constructor(private store: Store) {} ngOnInit(): void { - this.store.dispatch(new actions.LoadProjects()); const projects$ = this.store.pipe(select(allProjects)); this.projectsSubscription = projects$.subscribe((response) => { this.isLoading = response.isLoading; diff --git a/src/app/modules/customer-management/components/projects/components/services/project.service.spec.ts b/src/app/modules/customer-management/components/projects/components/services/project.service.spec.ts index 7e72f5d74..02a1f169c 100644 --- a/src/app/modules/customer-management/components/projects/components/services/project.service.spec.ts +++ b/src/app/modules/customer-management/components/projects/components/services/project.service.spec.ts @@ -51,10 +51,10 @@ describe('ProjectService', () => { it('projects are read using GET from url', () => { const projectsFoundSize = projectsList.length; service.url = '/projects'; - service.getProjects().subscribe((projectsInResponse) => { + service.getProjects({ customerId: 'xyz' }).subscribe((projectsInResponse) => { expect(projectsInResponse.length).toBe(projectsFoundSize); }); - const getProjectsRequest = httpMock.expectOne(service.url); + const getProjectsRequest = httpMock.expectOne(`${service.url}?customer_id=xyz`); expect(getProjectsRequest.request.method).toBe('GET'); getProjectsRequest.flush(projectsList); }); diff --git a/src/app/modules/customer-management/components/projects/components/services/project.service.ts b/src/app/modules/customer-management/components/projects/components/services/project.service.ts index a2d52a45d..f49cce4d2 100644 --- a/src/app/modules/customer-management/components/projects/components/services/project.service.ts +++ b/src/app/modules/customer-management/components/projects/components/services/project.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs'; import { environment } from '../../../../../../../environments/environment'; import { Project } from '../../../../../shared/models'; @@ -13,15 +13,13 @@ export class ProjectService { constructor(private http: HttpClient) {} - getProjects(): Observable { - return this.http.get(this.url); + getProjects(customerId: any): Observable { + const params = new HttpParams().set('customer_id', customerId.customerId); + return this.http.get(this.url, { params }); } createProject(projectData): Observable { - return this.http.post(this.url, { - ...projectData, - customer_id: 'b6e6a2f1-ce5c-49b3-8649-d154b4f3c305', - }); + return this.http.post(this.url, projectData); } updateProject(projectData): Observable { diff --git a/src/app/modules/customer-management/components/projects/components/store/project.actions.ts b/src/app/modules/customer-management/components/projects/components/store/project.actions.ts index 53f3c272f..7c9167ebb 100644 --- a/src/app/modules/customer-management/components/projects/components/store/project.actions.ts +++ b/src/app/modules/customer-management/components/projects/components/store/project.actions.ts @@ -20,6 +20,7 @@ export enum ProjectActionTypes { export class LoadProjects implements Action { public readonly type = ProjectActionTypes.LOAD_PROJECTS; + constructor(public customerId?: string) {} } export class LoadProjectsSuccess implements Action { diff --git a/src/app/modules/customer-management/components/projects/components/store/project.effects.ts b/src/app/modules/customer-management/components/projects/components/store/project.effects.ts index c4c85f782..059112081 100644 --- a/src/app/modules/customer-management/components/projects/components/store/project.effects.ts +++ b/src/app/modules/customer-management/components/projects/components/store/project.effects.ts @@ -13,8 +13,8 @@ export class ProjectEffects { @Effect() loadProjects$: Observable = this.actions$.pipe( ofType(actions.ProjectActionTypes.LOAD_PROJECTS), - mergeMap(() => - this.projectService.getProjects().pipe( + mergeMap((customerId) => + this.projectService.getProjects(customerId).pipe( map((project) => { return new actions.LoadProjectsSuccess(project); }), diff --git a/src/app/modules/customer-management/store/customer-management.reducers.spec.ts b/src/app/modules/customer-management/store/customer-management.reducers.spec.ts index 14cab351a..f69256259 100644 --- a/src/app/modules/customer-management/store/customer-management.reducers.spec.ts +++ b/src/app/modules/customer-management/store/customer-management.reducers.spec.ts @@ -3,8 +3,8 @@ import { Customer } from '../../shared/models/index'; import * as actions from './customer-management.actions'; describe('customerManagementReducer', () => { - const initialState: CustomerState = { data: [], isLoading: false, message: '', customerIdToEdit: '' }; - const customer: Customer = { name: 'aa', description: 'bb'}; + const initialState: CustomerState = { data: [], isLoading: false, message: '', customerIdToEdit: '', customerId: '' }; + const customer: Customer = { name: 'aa', description: 'bb' }; it('on LoadCustomer, isLoading is true ', () => { const action = new actions.LoadCustomers(); @@ -67,6 +67,7 @@ describe('customerManagementReducer', () => { isLoading: false, message: '', customerIdToEdit: '', + customerId: '' }; const customerToDeleteId = '1'; const action = new actions.DeleteCustomerSuccesss(customerToDeleteId); @@ -99,6 +100,7 @@ describe('customerManagementReducer', () => { isLoading: false, message: '', customerIdToEdit: '1', + customerId: '' }; const customerEdited = { name: 'xx', description: 'yy', tenant_id: 'cc', id: '1' }; const action = new actions.UpdateCustomerSuccess(customerEdited); diff --git a/src/app/modules/customer-management/store/customer-management.reducers.ts b/src/app/modules/customer-management/store/customer-management.reducers.ts index da4fb74e9..b79e99949 100644 --- a/src/app/modules/customer-management/store/customer-management.reducers.ts +++ b/src/app/modules/customer-management/store/customer-management.reducers.ts @@ -6,6 +6,7 @@ export interface CustomerState { isLoading: boolean; message: string; customerIdToEdit: string; + customerId: string; } export const initialState: CustomerState = { @@ -13,6 +14,7 @@ export const initialState: CustomerState = { isLoading: false, message: '', customerIdToEdit: '', + customerId: '' }; export const customerManagementReducer = (state: CustomerState = initialState, action: CustomerManagementActions) => { @@ -50,6 +52,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a return { ...state, data: [...state.data, action.payload], + customerId: action.payload.id, isLoading: false, message: 'Customer created successfully!', }; @@ -124,6 +127,7 @@ export const customerManagementReducer = (state: CustomerState = initialState, a case CustomerManagementActionTypes.SET_CUSTOMER_ID_TO_EDIT: { return { ...state, + customerId: action.payload, customerIdToEdit: action.payload, }; } diff --git a/src/app/modules/customer-management/store/customer-management.selectors.ts b/src/app/modules/customer-management/store/customer-management.selectors.ts index 44608a7db..63bde3fb5 100644 --- a/src/app/modules/customer-management/store/customer-management.selectors.ts +++ b/src/app/modules/customer-management/store/customer-management.selectors.ts @@ -1,6 +1,5 @@ import { createFeatureSelector, createSelector } from '@ngrx/store'; - import { CustomerState } from './customer-management.reducers'; export const getCustomerState = createFeatureSelector('customers'); @@ -22,10 +21,16 @@ export const customerIdtoEdit = createSelector(getCustomerState, (state: Custome } }); -export const getCustomerById = createSelector(allCustomers, customerIdtoEdit, (customers, customerId) => { +export const getCustomerId = createSelector(getCustomerState, (state: CustomerState) => { + if (state) { + return state.customerId; + } +}); + +export const getCustomerById = createSelector(allCustomers, customerIdtoEdit, (customers, customerIdToEdit) => { if (customers) { return customers.find((customer) => { - return customer.id === customerId; + return customer.id === customerIdToEdit; }); } }); diff --git a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts index c862600b6..2cb2eb076 100644 --- a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts +++ b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts @@ -1,5 +1,5 @@ import { getActiveTimeEntry } from './../../store/entry.selectors'; -import { Component, OnInit, ViewChild, ElementRef, Renderer2, Output, EventEmitter } from '@angular/core'; +import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { Store, select } from '@ngrx/store';