Skip to content

Commit 48eedd1

Browse files
PaulRC-ioetjosepato87
authored andcommitted
TT-85 fix: refactor start date and end date
1 parent b575cc1 commit 48eedd1

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/app/modules/shared/components/details-fields/details-fields.component.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
4242
activities: Activity[] = [];
4343
goingToWorkOnThis = false;
4444
shouldRestartEntry = false;
45-
starDateValue;
46-
endDateValue;
4745

4846
constructor(private formBuilder: FormBuilder, private store: Store<Merged>,
4947
private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {
@@ -141,8 +139,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
141139
uri: this.entryToEdit.uri,
142140
technology: '',
143141
});
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');
146142
} else {
147143
this.cleanForm();
148144
}
@@ -201,36 +197,60 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
201197
this.closeModal?.nativeElement?.click();
202198
}
203199

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+
204230
onSubmit() {
205231
if (this.entryForm.invalid) {
206232
this.toastrService.warning('Make sure to select a project and activity');
207233
return;
208234
}
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();
217238

218239
const entry = {
219240
project_id: this.entryForm.value.project_id,
220241
activity_id: this.entryForm.value.activity_id,
221242
technologies: get(this, 'selectedTechnologies', []),
222243
description: this.entryForm.value.description,
223-
start_date: this.starDateValue,
224-
end_date: this.endDateValue,
244+
start_date: startDateToSubmit,
245+
end_date: endDateToSubmit,
225246
uri: this.entryForm.value.uri,
226247
timezone_offset: new Date().getTimezoneOffset(),
227248
};
228249
if (this.goingToWorkOnThis) {
229250
delete entry.end_date;
230251
}
231-
const isStartDateInTheFuture = moment(startDate).isAfter(moment());
232-
const isEndDateInTheFuture = moment(endDate).isAfter(moment());
233-
if (isStartDateInTheFuture || isEndDateInTheFuture) {
252+
253+
if (this.timeEntryIsInTheFuture()) {
234254
this.toastrService.error('You cannot start a time-entry in the future');
235255
return;
236256
}

0 commit comments

Comments
 (0)