Skip to content

Commit be5107d

Browse files
fix: TT-106 asynchronous function in the EntryFields
1 parent d467356 commit be5107d

File tree

5 files changed

+107
-11
lines changed

5 files changed

+107
-11
lines changed

src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Observable, of } from 'rxjs';
12
import { LoadActiveEntry, EntryActionTypes, UpdateEntry } from './../../store/entry.actions';
23
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
34
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
@@ -15,6 +16,7 @@ import { formatDate } from '@angular/common';
1516
import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker';
1617
import * as moment from 'moment';
1718
import { DATE_FORMAT_YEAR } from 'src/environments/environment';
19+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1820

1921
describe('EntryFieldsComponent', () => {
2022
type Merged = TechnologyState & ProjectState;
@@ -24,6 +26,7 @@ describe('EntryFieldsComponent', () => {
2426
let mockTechnologySelector;
2527
let mockProjectsSelector;
2628
let entryForm;
29+
let featureManagerService : FeatureManagerService;
2730
const actionSub: ActionsSubject = new ActionsSubject();
2831
const toastrServiceStub = {
2932
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { },
@@ -114,6 +117,7 @@ describe('EntryFieldsComponent', () => {
114117
entryForm = TestBed.inject(FormBuilder);
115118
mockTechnologySelector = store.overrideSelector(allTechnologies, state.technologies);
116119
mockProjectsSelector = store.overrideSelector(getCustomerProjects, state.projects);
120+
featureManagerService = TestBed.inject(FeatureManagerService);
117121
}));
118122

119123
beforeEach(() => {
@@ -412,4 +416,13 @@ describe('EntryFieldsComponent', () => {
412416
expect(component.selectedTechnologies).toBe(initialTechnologies);
413417
});
414418

419+
// it('When Component is created, should call the feature toggle method', () => {
420+
// spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));
421+
422+
// component.ngOnInit();
423+
424+
// expect(component.isFeatureToggleActivated).toHaveBeenCalled();
425+
// expect(component.isUserRoleToggleOn).toBe(true);
426+
// });
427+
415428
});

src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
22
import { EntryActionTypes, LoadActiveEntry } from './../../store/entry.actions';
3-
import { filter } from 'rxjs/operators';
4-
import { Component, OnInit } from '@angular/core';
3+
import { filter, map } from 'rxjs/operators';
4+
import { Component, OnDestroy, OnInit } from '@angular/core';
55
import { FormBuilder, FormGroup } from '@angular/forms';
66
import { Store, ActionsSubject, select } from '@ngrx/store';
77
import { Activity, NewEntry } from '../../../shared/models';
88
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
99
import { TechnologyState } from '../../../shared/store/technology.reducers';
1010
import { ActivityState, LoadActivities } from '../../../activities-management/store';
11+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1112

1213
import * as entryActions from '../../store/entry.actions';
1314
import { get } from 'lodash';
@@ -16,6 +17,7 @@ import { ToastrService } from 'ngx-toastr';
1617
import { formatDate } from '@angular/common';
1718
import { getTimeEntriesDataSource } from '../../store/entry.selectors';
1819
import { DATE_FORMAT } from 'src/environments/environment';
20+
import { Subscription } from 'rxjs';
1921

2022
type Merged = TechnologyState & ProjectState & ActivityState;
2123

@@ -24,16 +26,24 @@ type Merged = TechnologyState & ProjectState & ActivityState;
2426
templateUrl: './entry-fields.component.html',
2527
styleUrls: ['./entry-fields.component.scss'],
2628
})
27-
export class EntryFieldsComponent implements OnInit {
29+
export class EntryFieldsComponent implements OnInit, OnDestroy {
2830
entryForm: FormGroup;
2931
selectedTechnologies: string[] = [];
3032
activities: Activity[] = [];
3133
activeEntry;
3234
newData;
3335
lastEntry;
3436
showTimeInbuttons = false;
37+
loadActivitiesSubscribe: Subscription;
38+
loadActiveEntrySubscribe: Subscription;
39+
actionSetDateSubscribe: Subscription;
40+
loadActivitiesSubject;
41+
loadActiveEntrySubject;
42+
actionSetDateSubject;
43+
exponentialGrowth: Boolean;
3544

3645
constructor(
46+
private featureManagerService: FeatureManagerService,
3747
private formBuilder: FormBuilder,
3848
private store: Store<Merged>,
3949
private actionsSubject$: ActionsSubject,
@@ -48,17 +58,24 @@ export class EntryFieldsComponent implements OnInit {
4858
});
4959
}
5060

51-
ngOnInit(): void {
61+
async ngOnInit(): Promise<void> {
62+
63+
this.exponentialGrowth = await this.isFeatureToggleActivated();
64+
65+
console.log(this.exponentialGrowth);
66+
5267
this.store.dispatch(new LoadActivities());
5368
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
54-
this.actionsSubject$
69+
this.loadActivitiesSubject = this.actionsSubject$
5570
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
5671
.subscribe((action) => {
5772
this.activities = action.payload;
5873
this.store.dispatch(new LoadActiveEntry());
5974
});
6075

61-
this.actionsSubject$
76+
this.exponentialGrowth ? this.loadActivitiesSubscribe = this.loadActivitiesSubject : this.loadActivitiesSubject;
77+
78+
this.loadActiveEntrySubject = this.actionsSubject$
6279
.pipe(
6380
filter(
6481
(action: any) =>
@@ -77,7 +94,9 @@ export class EntryFieldsComponent implements OnInit {
7794
}
7895
});
7996

80-
this.actionsSubject$
97+
this.exponentialGrowth ? this.loadActiveEntrySubscribe = this.loadActiveEntrySubject : this.loadActiveEntrySubject;
98+
99+
this.actionSetDateSubject = this.actionsSubject$
81100
.pipe(filter((action: any) => action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS))
82101
.subscribe((action) => {
83102
this.activeEntry = action.payload;
@@ -91,6 +110,8 @@ export class EntryFieldsComponent implements OnInit {
91110
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en'),
92111
};
93112
});
113+
114+
this.exponentialGrowth ? this.actionSetDateSubscribe = this.actionSetDateSubject : this.actionSetDateSubject;
94115
}
95116

96117
get activity_id() {
@@ -174,4 +195,23 @@ export class EntryFieldsComponent implements OnInit {
174195
onTechnologyRemoved($event: string[]) {
175196
this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, technologies: $event }));
176197
}
198+
199+
200+
ngOnDestroy(): void {
201+
console.log(this.exponentialGrowth);
202+
if(this.exponentialGrowth){
203+
this.loadActivitiesSubscribe.unsubscribe();
204+
this.loadActiveEntrySubscribe.unsubscribe();
205+
this.actionSetDateSubscribe.unsubscribe();
206+
}
207+
console.log('Entry Fields OnDestroy');
208+
}
209+
210+
isFeatureToggleActivated() {
211+
return this.featureManagerService.isToggleEnabledForUser('exponential-growth').pipe(
212+
map((enabled) => {
213+
return enabled === true ? true : false;
214+
})
215+
).toPromise();
216+
}
177217
}

src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
2626
updateEntrySubscription: Subscription;
2727
isLoading$: Observable<boolean>;
2828

29+
30+
/**
31+
* subscription variables
32+
*/
33+
projectsSubscribe : Subscription;
34+
activeEntrySubscribe : Subscription;
35+
2936
constructor(private formBuilder: FormBuilder, private store: Store<ProjectState>,
3037
private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {
3138
this.projectsForm = this.formBuilder.group({ project_id: null, });
@@ -35,7 +42,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
3542
ngOnInit(): void {
3643
this.store.dispatch(new actions.LoadProjects());
3744
const projects$ = this.store.pipe(select(getProjects));
38-
projects$.subscribe((projects) => {
45+
this.projectsSubscribe = projects$.subscribe((projects) => {
3946
this.listProjects = [];
4047
projects.forEach((project) => {
4148
const projectWithSearchField = {...project};
@@ -61,7 +68,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
6168
loadActiveTimeEntry() {
6269
this.store.dispatch(new entryActions.LoadActiveEntry());
6370
const activeEntry$ = this.store.pipe(select(getActiveTimeEntry));
64-
activeEntry$.subscribe((activeEntry) => {
71+
this.activeEntrySubscribe = activeEntry$.subscribe((activeEntry) => {
6572
this.activeEntry = activeEntry;
6673
if (activeEntry) {
6774
this.showClockIn = false;
@@ -111,5 +118,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
111118

112119
ngOnDestroy(): void {
113120
this.updateEntrySubscription.unsubscribe();
121+
this.projectsSubscribe.unsubscribe();
122+
this.activeEntrySubscribe.unsubscribe();
114123
}
115124
}

src/app/modules/time-clock/pages/time-clock.component.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
22
import { ActionsSubject, select, Store } from '@ngrx/store';
33
import { ToastrService } from 'ngx-toastr';
44
import { Subscription } from 'rxjs';
5-
import { filter } from 'rxjs/operators';
5+
import { filter, map } from 'rxjs/operators';
6+
import { threadId } from 'worker_threads';
67
import { AzureAdB2CService } from '../../login/services/azure.ad.b2c.service';
78
import { EntryFieldsComponent } from '../components/entry-fields/entry-fields.component';
89
import { Entry } from './../../shared/models/entry.model';
910
import { EntryActionTypes, LoadEntriesSummary, StopTimeEntryRunning } from './../store/entry.actions';
1011
import { getActiveTimeEntry } from './../store/entry.selectors';
12+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1113
@Component({
1214
selector: 'app-time-clock',
1315
templateUrl: './time-clock.component.html',
@@ -22,20 +24,39 @@ export class TimeClockComponent implements OnInit, OnDestroy {
2224
clockOutSubscription: Subscription;
2325

2426

27+
/**
28+
* subscription variables
29+
*/
30+
storeSubscribe : Subscription;
31+
storeSubject;
32+
exponentialGrowth;
33+
34+
2535
constructor(
36+
private featureManagerService: FeatureManagerService,
2637
private azureAdB2CService: AzureAdB2CService,
2738
private store: Store<Entry>,
2839
private toastrService: ToastrService,
2940
private actionsSubject$: ActionsSubject
3041
) {}
3142

3243
ngOnDestroy(): void {
44+
// if(this.exponentialGrowth){
45+
// this.storeSubscribe.unsubscribe();
46+
// }
3347
this.clockOutSubscription.unsubscribe();
3448
}
3549

3650
ngOnInit() {
51+
52+
this.isFeatureToggleActivated().subscribe((flag) => {
53+
this.exponentialGrowth = flag;
54+
// console.log('Time-clock', this.exponentialGrowth);
55+
});
56+
// console.log('Time-clock', this.exponentialGrowth);
57+
3758
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
38-
this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => {
59+
this.storeSubject = this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => {
3960
this.activeTimeEntry = activeTimeEntry;
4061
if (this.activeTimeEntry) {
4162
this.areFieldsVisible = true;
@@ -44,6 +65,8 @@ export class TimeClockComponent implements OnInit, OnDestroy {
4465
}
4566
});
4667

68+
this.exponentialGrowth ? this.storeSubscribe = this.storeSubject : this.storeSubject;
69+
4770
this.reloadSummariesOnClockOut();
4871

4972
}
@@ -72,4 +95,14 @@ export class TimeClockComponent implements OnInit, OnDestroy {
7295
this.toastrService.error('Activity is required');
7396
}
7497
}
98+
99+
isFeatureToggleActivated() {
100+
return this.featureManagerService.isToggleEnabledForUser('exponential-growth').pipe(
101+
map((enabled) => {
102+
return enabled === true ? true : false;
103+
})
104+
);
105+
}
75106
}
107+
108+

src/app/modules/time-clock/services/entry.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class EntryService {
2020
baseUrl = `${environment.timeTrackerApiUrl}/time-entries`;
2121

2222
loadActiveEntry(): Observable<any> {
23+
console.log('Method Call');
2324
return this.http.get(`${this.baseUrl}/running`);
2425
}
2526

0 commit comments

Comments
 (0)