@@ -42,8 +42,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
42
42
activities : Activity [ ] = [ ] ;
43
43
goingToWorkOnThis = false ;
44
44
shouldRestartEntry = false ;
45
- starDateValue ;
46
- endDateValue ;
47
45
48
46
constructor ( private formBuilder : FormBuilder , private store : Store < Merged > ,
49
47
private actionsSubject$ : ActionsSubject , private toastrService : ToastrService ) {
@@ -141,8 +139,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
141
139
uri : this . entryToEdit . uri ,
142
140
technology : '' ,
143
141
} ) ;
144
- this . starDateValue = formatDate ( get ( this . entryToEdit , 'start_date' , '00:00' ) , 'HH:mm' , 'en' ) ;
145
- this . endDateValue = formatDate ( get ( this . entryToEdit , 'end_date' , '00:00' ) , 'HH:mm' , 'en' ) ;
146
142
} else {
147
143
this . cleanForm ( ) ;
148
144
}
@@ -201,36 +197,60 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
201
197
this . closeModal ?. nativeElement ?. click ( ) ;
202
198
}
203
199
200
+ startDateToSubmit ( ) {
201
+ const startDate = this . entryForm . value . start_date ;
202
+ const initialStartDate = this . entryToEdit . start_date ;
203
+ const updatedStartDate = new Date ( `${ startDate } T${ this . entryForm . value . start_hour . trim ( ) } ` ) . toISOString ( ) ;
204
+ const initialStartHour = formatDate ( get ( this . entryToEdit , 'start_date' , '00:00' ) , 'HH:mm' , 'en' ) ;
205
+ const updatedStartHour = this . entryForm . value . start_hour ;
206
+ const startHourHasNotChanged = updatedStartHour === initialStartHour ;
207
+ const result = startHourHasNotChanged ? initialStartDate : updatedStartDate ;
208
+ return result ;
209
+ }
210
+
211
+ endDateToSubmit ( ) {
212
+ const endDate = this . entryForm . value . end_date ;
213
+ const initialEndDate = this . entryToEdit . end_date ;
214
+ const updatedEndDate = new Date ( `${ endDate } T${ this . entryForm . value . end_hour . trim ( ) } ` ) . toISOString ( ) ;
215
+ const initialEndHour = formatDate ( get ( this . entryToEdit , 'end_date' , '00:00' ) , 'HH:mm' , 'en' ) ;
216
+ const updatedEndHour = this . entryForm . value . end_hour ;
217
+ const endDateHasNotChanged = updatedEndHour === initialEndHour ;
218
+ const result = endDateHasNotChanged ? initialEndDate : updatedEndDate ;
219
+ return result ;
220
+ }
221
+
222
+ timeEntryIsInTheFuture ( ) {
223
+ const startDate = this . entryForm . value . start_date ;
224
+ const endDate = this . entryForm . value . end_date ;
225
+ const isStartDateInTheFuture = moment ( startDate ) . isAfter ( moment ( ) ) ;
226
+ const isEndDateInTheFuture = moment ( endDate ) . isAfter ( moment ( ) ) ;
227
+ return isStartDateInTheFuture || isEndDateInTheFuture ;
228
+ }
229
+
204
230
onSubmit ( ) {
205
231
if ( this . entryForm . invalid ) {
206
232
this . toastrService . warning ( 'Make sure to select a project and activity' ) ;
207
233
return ;
208
234
}
209
- const startDate = this . entryForm . value . start_date ;
210
- const endDate = this . entryForm . value . end_date ;
211
- this . starDateValue = this . entryForm . value . start_hour === this . starDateValue ?
212
- this . entryToEdit . start_date :
213
- new Date ( `${ startDate } T${ this . entryForm . value . start_hour . trim ( ) } ` ) . toISOString ( ) ;
214
- this . endDateValue = this . entryForm . value . end_hour === this . endDateValue ?
215
- this . entryToEdit . end_date :
216
- new Date ( `${ endDate } T${ this . entryForm . value . end_hour . trim ( ) } ` ) . toISOString ( ) ;
235
+
236
+ const startDateToSubmit = this . startDateToSubmit ( ) ;
237
+ const endDateToSubmit = this . endDateToSubmit ( ) ;
217
238
218
239
const entry = {
219
240
project_id : this . entryForm . value . project_id ,
220
241
activity_id : this . entryForm . value . activity_id ,
221
242
technologies : get ( this , 'selectedTechnologies' , [ ] ) ,
222
243
description : this . entryForm . value . description ,
223
- start_date : this . starDateValue ,
224
- end_date : this . endDateValue ,
244
+ start_date : startDateToSubmit ,
245
+ end_date : endDateToSubmit ,
225
246
uri : this . entryForm . value . uri ,
226
247
timezone_offset : new Date ( ) . getTimezoneOffset ( ) ,
227
248
} ;
228
249
if ( this . goingToWorkOnThis ) {
229
250
delete entry . end_date ;
230
251
}
231
- const isStartDateInTheFuture = moment ( startDate ) . isAfter ( moment ( ) ) ;
232
- const isEndDateInTheFuture = moment ( endDate ) . isAfter ( moment ( ) ) ;
233
- if ( isStartDateInTheFuture || isEndDateInTheFuture ) {
252
+
253
+ if ( this . timeEntryIsInTheFuture ( ) ) {
234
254
this . toastrService . error ( 'You cannot start a time-entry in the future' ) ;
235
255
return ;
236
256
}
0 commit comments