Skip to content

Commit 43d48d9

Browse files
authored
fix: TT-282 added conditions in method onSubmit and added array of activities in component project-list-hover (#700)
1 parent 8b5068a commit 43d48d9

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
class="form-control"
77
formControlName="activity_id"
88
[class.is-invalid]="activity_id.invalid && activity_id.touched"
9+
#autofocus
910
required>
10-
<option *ngFor="let activity of activities" value="{{ activity.id }}">{{ activity.name }}</option>
11+
<option *ngFor="let activity of activities" value="{{ activity.id }}" class = "id">{{ activity.name }}</option>
1112
</select>
1213
</div>
1314

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,12 @@ describe('EntryFieldsComponent', () => {
323323
});
324324

325325
it('dispatches an action when onSubmit is called', () => {
326+
const isEntryFormValid = spyOn(component, 'entryFormIsValidate').and.returnValue(true);
326327
spyOn(store, 'dispatch');
327328

328329
component.onSubmit();
329330

331+
expect(isEntryFormValid).toHaveBeenCalled();
330332
expect(store.dispatch).toHaveBeenCalled();
331333
});
332334

@@ -555,6 +557,37 @@ describe('EntryFieldsComponent', () => {
555557
expect(featureToggleGeneralService.isActivated).toHaveBeenCalled();
556558
});
557559
});
560+
561+
it('when a activity is not register in DB should show activatefocus in select activity', () => {
562+
const activitiesMock = [{
563+
id: 'xyz',
564+
name: 'test',
565+
description : 'test1'
566+
}];
567+
const data = {
568+
activity_id: 'xyz',
569+
description: '',
570+
start_date: moment().format(DATE_FORMAT_YEAR),
571+
start_hour: moment().format('HH:mm'),
572+
uri: ''
573+
};
574+
component.activities = activitiesMock;
575+
component.entryForm.patchValue({
576+
description: data.description,
577+
uri: data.uri,
578+
activity_id: data.activity_id,
579+
start_date: data.start_date,
580+
start_hour: data.start_hour,
581+
});
582+
component.ngOnInit();
583+
component.activateFocus();
584+
fixture.detectChanges();
585+
fixture.whenStable().then(() => {
586+
fixture.detectChanges();
587+
const autofocus = fixture.nativeElement.querySelector('select');
588+
expect(autofocus).toHaveBeenCalled();
589+
});
590+
});
558591
});
559592

560593

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { AzureAdB2CService } from './../../../login/services/azure.ad.b2c.service';
21
import { FeatureToggleGeneralService } from './../../../shared/feature-toggles/feature-toggle-general/feature-toggle-general.service';
32
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
43
import { EntryActionTypes, LoadActiveEntry, UpdateCurrentOrLastEntry, UpdateEntry, UpdateEntryRunning } from './../../store/entry.actions';
5-
import { filter, map } from 'rxjs/operators';
6-
import { Component, OnDestroy, OnInit } from '@angular/core';
4+
import { filter} from 'rxjs/operators';
5+
import { Component, OnDestroy, OnInit, ElementRef, ViewChild } from '@angular/core';
76
import { FormBuilder, FormGroup } from '@angular/forms';
87
import { Store, ActionsSubject, select } from '@ngrx/store';
98
import { Activity, NewEntry } from '../../../shared/models';
109
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
1110
import { TechnologyState } from '../../../shared/store/technology.reducers';
1211
import { ActivityState, LoadActivities } from '../../../activities-management/store';
1312
import * as entryActions from '../../store/entry.actions';
14-
import { get } from 'lodash';
13+
import { get, head } from 'lodash';
1514
import * as moment from 'moment';
1615
import { ToastrService } from 'ngx-toastr';
1716
import { formatDate } from '@angular/common';
1817
import { getTimeEntriesDataSource } from '../../store/entry.selectors';
1918
import { DATE_FORMAT } from 'src/environments/environment';
20-
import { Subscription, Observable } from 'rxjs';
21-
import { FeatureManagerService } from './../../../shared/feature-toggles/feature-toggle-manager.service';
19+
import { Subscription, } from 'rxjs';
2220
import { FeatureToggle } from './../../../../../environments/enum';
2321
import { CookieService } from 'ngx-cookie-service';
2422

@@ -30,6 +28,9 @@ type Merged = TechnologyState & ProjectState & ActivityState;
3028
styleUrls: ['./entry-fields.component.scss'],
3129
})
3230
export class EntryFieldsComponent implements OnInit, OnDestroy {
31+
32+
@ViewChild('autofocus') autofocus!: ElementRef<HTMLSelectElement>;
33+
3334
entryForm: FormGroup;
3435
selectedTechnologies: string[] = [];
3536
activities: Activity[] = [];
@@ -42,14 +43,13 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
4243
actionSetDateSubscription: Subscription;
4344
isCookieFeatureToggleActive: boolean;
4445
isFeatureToggleActive: boolean;
45-
4646
constructor(
4747
private formBuilder: FormBuilder,
4848
private store: Store<Merged>,
4949
private actionsSubject$: ActionsSubject,
5050
private toastrService: ToastrService,
5151
private featureToggleGeneralService: FeatureToggleGeneralService,
52-
private cookiesService: CookieService
52+
private cookiesService: CookieService,
5353
) {
5454
this.entryForm = this.formBuilder.group({
5555
description: '',
@@ -63,7 +63,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
6363
ngOnInit(): void {
6464
this.store.dispatch(new LoadActivities());
6565
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
66-
this.loadActivitiesSubscription = this.actionsSubject$
66+
this.loadActivitiesSubscription = this.actionsSubject$
6767
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
6868
.subscribe((action) => {
6969
this.activities = action.payload.filter((item) => item.status !== 'inactive');
@@ -81,7 +81,6 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
8181
this.isFeatureToggleActive = flag;
8282
});
8383
}
84-
8584
this.loadActiveEntrySubscription = this.actionsSubject$
8685
.pipe(
8786
filter(
@@ -113,6 +112,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
113112
start_date: this.activeEntry.start_date,
114113
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en'),
115114
};
115+
this.activateFocus();
116116
});
117117
}
118118
get activity_id() {
@@ -121,6 +121,13 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
121121
get start_hour() {
122122
return this.entryForm.get('start_hour');
123123
}
124+
125+
activateFocus(){
126+
if ((this.activities.length > 0) && (this.entryForm.value.activity_id === head(this.activities).id)){
127+
this.autofocus.nativeElement.focus();
128+
}
129+
}
130+
124131
setDataToUpdate(entryData: NewEntry) {
125132
if (entryData) {
126133
this.entryForm.patchValue({
@@ -143,7 +150,9 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
143150
}
144151

145152
onSubmit() {
146-
this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, ...this.entryForm.value }));
153+
if (this.entryFormIsValidate()){
154+
this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, ...this.entryForm.value }));
155+
}
147156
}
148157

149158
onUpdateStartHour() {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe('ProjectListHoverComponent', () => {
3939
isLoading: false,
4040
message: '',
4141
},
42-
};
4342

43+
};
4444
beforeEach(
4545
waitForAsync(() => {
4646
TestBed.configureTestingModule({
@@ -69,6 +69,12 @@ describe('ProjectListHoverComponent', () => {
6969

7070
it('dispatchs a CreateEntry action on clockIn', () => {
7171
component.activeEntry = null;
72+
const activitiesMock = [{
73+
id: 'xyz',
74+
name: 'test',
75+
description : 'test1'
76+
}];
77+
component.activities = activitiesMock;
7278
spyOn(store, 'dispatch');
7379

7480
component.clockIn(1, 'customer', 'project');

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import {
1414
} from './../../../customer-management/components/projects/components/store/project.selectors';
1515
import { EntryActionTypes } from './../../store/entry.actions';
1616
import { getActiveTimeEntry } from './../../store/entry.selectors';
17+
import { Activity, } from '../../../shared/models';
18+
import { LoadActivities } from './../../../activities-management/store/activity-management.actions';
19+
import { allActivities } from 'src/app/modules/activities-management/store/activity-management.selectors';
20+
import { head } from 'lodash';
21+
1722
@Component({
1823
selector: 'app-project-list-hover',
1924
templateUrl: './project-list-hover.component.html',
@@ -22,13 +27,15 @@ import { getActiveTimeEntry } from './../../store/entry.selectors';
2227
export class ProjectListHoverComponent implements OnInit, OnDestroy {
2328
keyword = 'search_field';
2429
listProjects: Project[] = [];
30+
activities: Activity[] = [];
2531
activeEntry;
2632
projectsForm: FormGroup;
2733
showClockIn: boolean;
2834
updateEntrySubscription: Subscription;
2935
isLoading$: Observable<boolean>;
3036
projectsSubscription: Subscription;
3137
activeEntrySubscription: Subscription;
38+
loadActivitiesSubscription: Subscription;
3239

3340
constructor(
3441
private formBuilder: FormBuilder,
@@ -52,6 +59,11 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
5259
});
5360
this.loadActiveTimeEntry();
5461
});
62+
this.store.dispatch(new LoadActivities());
63+
const activities$ = this.store.pipe(select(allActivities));
64+
activities$.subscribe((response) => {
65+
this.activities = response;
66+
});
5567
this.updateEntrySubscription = this.actionsSubject$
5668
.pipe(filter((action: any) => action.type === EntryActionTypes.UPDATE_ENTRY_SUCCESS))
5769
.subscribe((action) => {
@@ -88,6 +100,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
88100
start_date: new Date().toISOString(),
89101
timezone_offset: new Date().getTimezoneOffset(),
90102
technologies: [],
103+
activity_id: head(this.activities).id,
91104
};
92105
this.store.dispatch(new entryActions.ClockIn(entry));
93106
this.projectsForm.setValue({ project_id: `${customerName} - ${name}` });

0 commit comments

Comments
 (0)