11import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions' ;
22import { EntryActionTypes , LoadActiveEntry } from './../../store/entry.actions' ;
3- import { filter } from 'rxjs/operators' ;
3+ import { filter } from 'rxjs/operators' ;
44import { Component , OnDestroy , OnInit , ElementRef , ViewChild } from '@angular/core' ;
55import { FormBuilder , FormGroup } from '@angular/forms' ;
66import { Store , ActionsSubject , select } from '@ngrx/store' ;
@@ -15,7 +15,7 @@ import { ToastrService } from 'ngx-toastr';
1515import { formatDate } from '@angular/common' ;
1616import { getTimeEntriesDataSource } from '../../store/entry.selectors' ;
1717import { DATE_FORMAT } from 'src/environments/environment' ;
18- import { Subscription , } from 'rxjs' ;
18+ import { Subscription } from 'rxjs' ;
1919
2020type Merged = TechnologyState & ProjectState & ActivityState ;
2121
@@ -25,7 +25,6 @@ type Merged = TechnologyState & ProjectState & ActivityState;
2525 styleUrls : [ './entry-fields.component.scss' ] ,
2626} )
2727export class EntryFieldsComponent implements OnInit , OnDestroy {
28-
2928 @ViewChild ( 'autofocus' ) autofocus ! : ElementRef < HTMLSelectElement > ;
3029
3130 entryForm : FormGroup ;
@@ -44,7 +43,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
4443 private formBuilder : FormBuilder ,
4544 private store : Store < Merged > ,
4645 private actionsSubject$ : ActionsSubject ,
47- private toastrService : ToastrService ,
46+ private toastrService : ToastrService
4847 ) {
4948 this . entryForm = this . formBuilder . group ( {
5049 description : '' ,
@@ -58,12 +57,14 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
5857 ngOnInit ( ) : void {
5958 this . store . dispatch ( new LoadActivities ( ) ) ;
6059 this . store . dispatch ( new entryActions . LoadEntries ( new Date ( ) . getMonth ( ) + 1 , new Date ( ) . getFullYear ( ) ) ) ;
61- this . loadActivitiesSubscription = this . actionsSubject$
60+ this . loadActivitiesSubscription = this . actionsSubject$
6261 . pipe ( filter ( ( action : any ) => action . type === ActivityManagementActionTypes . LOAD_ACTIVITIES_SUCCESS ) )
6362 . subscribe ( ( action ) => {
64- this . activities = action . payload . filter ( ( item ) => item . status !== 'inactive' ) . sort ( ( a , b ) => {
65- return ( a . name ) . localeCompare ( b . name ) ;
66- } ) ;
63+ this . activities = action . payload
64+ . filter ( ( item ) => item . status !== 'inactive' )
65+ . sort ( ( a , b ) => {
66+ return a . name . localeCompare ( b . name ) ;
67+ } ) ;
6768 this . store . dispatch ( new LoadActiveEntry ( ) ) ;
6869 } ) ;
6970
@@ -96,7 +97,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
9697 uri : this . activeEntry . uri ,
9798 activity_id : this . activeEntry . activity_id ,
9899 start_date : this . activeEntry . start_date ,
99- start_hour : formatDate ( this . activeEntry . start_date , 'HH:mm' , 'en' )
100+ start_hour : formatDate ( this . activeEntry . start_date , 'HH:mm' , 'en' ) ,
100101 } ;
101102 this . activateFocus ( ) ;
102103 } ) ;
@@ -108,8 +109,8 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
108109 return this . entryForm . get ( 'start_hour' ) ;
109110 }
110111
111- activateFocus ( ) {
112- if ( ( this . activities . length > 0 ) && ( this . entryForm . value . activity_id === head ( this . activities ) . id ) ) {
112+ activateFocus ( ) {
113+ if ( this . activities . length > 0 && this . entryForm . value . activity_id === head ( this . activities ) . id ) {
113114 this . autofocus . nativeElement . focus ( ) ;
114115 }
115116 }
@@ -132,11 +133,16 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
132133 }
133134
134135 entryFormIsValidate ( ) {
135- return this . entryForm . valid ;
136+ let projectName = '' ;
137+ this . store . pipe ( select ( getTimeEntriesDataSource ) ) . subscribe ( ( ds ) => {
138+ const dataToUse = ds . data . find ( ( item ) => item . project_id === this . activeEntry . project_id ) ;
139+ projectName = dataToUse . project_name ;
140+ } ) ;
141+ return this . requiredFieldsForInternalAppExist ( projectName ) && this . entryForm . valid ;
136142 }
137143
138144 onSubmit ( ) {
139- if ( this . entryFormIsValidate ( ) ) {
145+ if ( this . entryFormIsValidate ( ) ) {
140146 this . store . dispatch ( new entryActions . UpdateEntryRunning ( { ...this . newData , ...this . entryForm . value } ) ) ;
141147 }
142148 }
@@ -192,4 +198,15 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
192198 this . loadActiveEntrySubscription . unsubscribe ( ) ;
193199 this . actionSetDateSubscription . unsubscribe ( ) ;
194200 }
201+
202+ requiredFieldsForInternalAppExist ( projectName ) {
203+ const emptyFields = this . entryForm . value . uri === '' && this . entryForm . value . description === '' ;
204+ const isInternalApp = projectName . includes ( '(Applications)' ) ;
205+ if ( isInternalApp && emptyFields ) {
206+ const message = 'The description field or ticket field should not be empty' ;
207+ this . toastrService . error ( `Some fields are empty, ${ message } .` ) ;
208+ return false ;
209+ }
210+ return true ;
211+ }
195212}
0 commit comments