1- import { AzureAdB2CService } from './../../../login/services/azure.ad.b2c.service' ;
21import { FeatureToggleGeneralService } from './../../../shared/feature-toggles/feature-toggle-general/feature-toggle-general.service' ;
32import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions' ;
43import { 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' ;
76import { FormBuilder , FormGroup } from '@angular/forms' ;
87import { Store , ActionsSubject , select } from '@ngrx/store' ;
98import { Activity , NewEntry } from '../../../shared/models' ;
109import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer' ;
1110import { TechnologyState } from '../../../shared/store/technology.reducers' ;
1211import { ActivityState , LoadActivities } from '../../../activities-management/store' ;
1312import * as entryActions from '../../store/entry.actions' ;
14- import { get } from 'lodash' ;
13+ import { get , head } from 'lodash' ;
1514import * as moment from 'moment' ;
1615import { ToastrService } from 'ngx-toastr' ;
1716import { formatDate } from '@angular/common' ;
1817import { getTimeEntriesDataSource } from '../../store/entry.selectors' ;
1918import { 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' ;
2220import { FeatureToggle } from './../../../../../environments/enum' ;
2321import { CookieService } from 'ngx-cookie-service' ;
2422
@@ -30,6 +28,9 @@ type Merged = TechnologyState & ProjectState & ActivityState;
3028 styleUrls : [ './entry-fields.component.scss' ] ,
3129} )
3230export 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 ( ) {
0 commit comments