Skip to content

Commit 1424d50

Browse files
PaulRC-ioetjosepato87
authored andcommitted
TT-85 fix: refactor code of time fields
1 parent 1b33951 commit 1424d50

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,25 @@ describe('DetailsFieldsComponent', () => {
245245
expect(component.saveEntry.emit).toHaveBeenCalledWith(data);
246246
});
247247

248-
it('when the current entry is not running, then the end hour input should be rendered', () => {
248+
it('when the current entry is not running, then the end date and end hour inputs should be rendered', () => {
249249
component.goingToWorkOnThis = false;
250250
fixture.detectChanges();
251251

252+
const endDateInput = fixture.debugElement.nativeElement.querySelector('#end_date');
252253
const endHourInput = fixture.debugElement.nativeElement.querySelector('#end_hour');
254+
255+
expect(endDateInput).toBeDefined();
253256
expect(endHourInput).toBeDefined();
254257
});
255258

256-
it('when the current entry is running, then the end hour input should not be rendered', () => {
259+
it('when the current entry is running, then the end date and end hour inputs should not be rendered', () => {
257260
component.goingToWorkOnThis = true;
258261
fixture.detectChanges();
259262

263+
const endDateInput = fixture.debugElement.nativeElement.querySelector('#end_date');
260264
const endHourInput = fixture.debugElement.nativeElement.querySelector('#end_hour');
265+
266+
expect(endDateInput).toBeNull();
261267
expect(endHourInput).toBeNull();
262268
});
263269

@@ -316,22 +322,22 @@ describe('DetailsFieldsComponent', () => {
316322
});
317323

318324
it('should not modify the start_date when start_hour has not been modified', () => {
319-
const dateTest = moment().format('YYYY-MM-DD');
320-
const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
321-
const expectedStartDate = new Date(`${dateTest}T${startHourTest.trim()}`);
325+
const currentDate = moment().format('YYYY-MM-DD');
326+
const startHour = moment().subtract(3, 'hours').format('HH:mm:ss');
327+
const expectedStartDate = new Date(`${currentDate}T${startHour.trim()}`);
322328

323329
component.entryToEdit = {...entryToEdit, start_date: expectedStartDate };
324330
fixture.componentInstance.ngOnChanges();
325331

326332
component.entryForm.patchValue({ description: 'test' });
327333

328-
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
334+
expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate);
329335
});
330336

331337
it('should modify the start_date when start_hour has been modified', () => {
332-
const dateTest = moment().format('YYYY-MM-DD');
333-
const startHourTest = moment().format('HH:mm:ss');
334-
const startDate = new Date(`${dateTest}T${startHourTest.trim()}`);
338+
const currentDate = moment().format('YYYY-MM-DD');
339+
const startHour = moment().format('HH:mm:ss');
340+
const startDate = new Date(`${currentDate}T${startHour.trim()}`);
335341

336342
component.entryToEdit = {...entryToEdit, start_date: startDate };
337343
fixture.componentInstance.ngOnChanges();
@@ -341,26 +347,26 @@ describe('DetailsFieldsComponent', () => {
341347
component.entryForm.patchValue({start_hour: updatedStartHour});
342348

343349
const expectedStartDate = moment(updatedStartDate).seconds(0).millisecond(0).toISOString();
344-
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
350+
expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate);
345351
});
346352

347353
it('should not modify the end_date when end_hour has not been modified', () => {
348-
const dateTest = moment().format('YYYY-MM-DD');
349-
const endtHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
350-
const expectedEndDate = new Date(`${dateTest}T${endtHourTest.trim()}`);
354+
const currentDate = moment().format('YYYY-MM-DD');
355+
const endHour = moment().subtract(3, 'hours').format('HH:mm:ss');
356+
const expectedEndDate = new Date(`${currentDate}T${endHour.trim()}`);
351357

352358
component.entryToEdit = {...entryToEdit, end_date: expectedEndDate };
353359
fixture.componentInstance.ngOnChanges();
354360

355361
component.entryForm.patchValue({ description: 'test' });
356362

357-
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
363+
expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate);
358364
});
359365

360366
it('should modify the end_date when end_hour has been modified', () => {
361-
const dateTest = moment().format('YYYY-MM-DD');
362-
const endHourTest = moment().format('HH:mm:ss');
363-
const endDate = new Date(`${dateTest}T${endHourTest.trim()}`);
367+
const currentDate = moment().format('YYYY-MM-DD');
368+
const endHour = moment().format('HH:mm:ss');
369+
const endDate = new Date(`${currentDate}T${endHour.trim()}`);
364370

365371
component.entryToEdit = {...entryToEdit, end_date: endDate };
366372
fixture.componentInstance.ngOnChanges();
@@ -370,7 +376,7 @@ describe('DetailsFieldsComponent', () => {
370376
component.entryForm.patchValue({end_hour: updatedEndHour});
371377

372378
const expectedEndDate = moment(updatedEndDate).seconds(0).millisecond(0).toISOString();
373-
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
379+
expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate);
374380
});
375381

376382
it('displays error message when the date selected is in the future', () => {

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
133133
activity_id: this.entryToEdit.activity_id,
134134
description: this.entryToEdit.description,
135135
start_date: formatDate(get(this.entryToEdit, 'start_date', ''), DATE_FORMAT, 'en'),
136-
end_date: formatDate(get(this.entryToEdit, 'end_date'), DATE_FORMAT, 'en'),
136+
end_date: formatDate(get(this.entryToEdit, 'end_date', ''), DATE_FORMAT, 'en'),
137137
start_hour: formatDate(get(this.entryToEdit, 'start_date', '00:00'), 'HH:mm', 'en'),
138138
end_hour: formatDate(get(this.entryToEdit, 'end_date', '00:00'), 'HH:mm', 'en'),
139139
uri: this.entryToEdit.uri,
@@ -197,25 +197,14 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
197197
this.closeModal?.nativeElement?.click();
198198
}
199199

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;
200+
dateToSubmit(date, hour){
201+
const entryFormDate = this.entryForm.value[date];
202+
const updatedHour = this.entryForm.value[hour];
203+
const initialDate = this.entryToEdit[date];
204+
const updatedDate = new Date(`${entryFormDate}T${updatedHour.trim()}`).toISOString();
205+
const initialHour = formatDate(get(this.entryToEdit, date, '00:00'), 'HH:mm', 'en');
206+
const dateHasNotChanged = updatedHour === initialHour;
207+
const result = dateHasNotChanged ? initialDate : updatedDate;
219208
return result;
220209
}
221210

@@ -225,8 +214,8 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
225214
return;
226215
}
227216

228-
const startDateToSubmit = this.startDateToSubmit();
229-
const endDateToSubmit = this.endDateToSubmit();
217+
const startDateToSubmit = this.dateToSubmit('start_date', 'start_hour');
218+
const endDateToSubmit = this.dateToSubmit('end_date', 'end_hour');
230219

231220
const entry = {
232221
project_id: this.entryForm.value.project_id,
@@ -257,7 +246,10 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
257246
onGoingToWorkOnThisChange(event: any) {
258247
this.goingToWorkOnThis = event.currentTarget.checked;
259248
if (!this.goingToWorkOnThis) {
260-
this.entryForm.patchValue({ end_hour: formatDate(new Date(), 'HH:mm:ss', 'en') });
249+
this.entryForm.patchValue({
250+
end_date: formatDate(get(this.entryToEdit, 'start_date', ''), DATE_FORMAT, 'en'),
251+
end_hour: formatDate(get(this.entryToEdit, 'start_date', '00:00'), 'HH:mm', 'en')
252+
});
261253
}
262254
this.shouldRestartEntry = !this.entryToEdit?.running && this.goingToWorkOnThis;
263255
}

0 commit comments

Comments
 (0)