Skip to content

Commit df0ae39

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

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,22 @@ describe('DetailsFieldsComponent', () => {
316316
});
317317

318318
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()}`);
319+
const currentDate = moment().format('YYYY-MM-DD');
320+
const startHour = moment().subtract(3, 'hours').format('HH:mm:ss');
321+
const expectedStartDate = new Date(`${currentDate}T${startHour.trim()}`);
322322

323323
component.entryToEdit = {...entryToEdit, start_date: expectedStartDate };
324324
fixture.componentInstance.ngOnChanges();
325325

326326
component.entryForm.patchValue({ description: 'test' });
327327

328-
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
328+
expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate);
329329
});
330330

331331
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()}`);
332+
const currentDate = moment().format('YYYY-MM-DD');
333+
const startHour = moment().format('HH:mm:ss');
334+
const startDate = new Date(`${currentDate}T${startHour.trim()}`);
335335

336336
component.entryToEdit = {...entryToEdit, start_date: startDate };
337337
fixture.componentInstance.ngOnChanges();
@@ -341,26 +341,26 @@ describe('DetailsFieldsComponent', () => {
341341
component.entryForm.patchValue({start_hour: updatedStartHour});
342342

343343
const expectedStartDate = moment(updatedStartDate).seconds(0).millisecond(0).toISOString();
344-
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
344+
expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate);
345345
});
346346

347347
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()}`);
348+
const currentDate = moment().format('YYYY-MM-DD');
349+
const endHour = moment().subtract(3, 'hours').format('HH:mm:ss');
350+
const expectedEndDate = new Date(`${currentDate}T${endHour.trim()}`);
351351

352352
component.entryToEdit = {...entryToEdit, end_date: expectedEndDate };
353353
fixture.componentInstance.ngOnChanges();
354354

355355
component.entryForm.patchValue({ description: 'test' });
356356

357-
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
357+
expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate);
358358
});
359359

360360
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()}`);
361+
const currentDate = moment().format('YYYY-MM-DD');
362+
const endHour = moment().format('HH:mm:ss');
363+
const endDate = new Date(`${currentDate}T${endHour.trim()}`);
364364

365365
component.entryToEdit = {...entryToEdit, end_date: endDate };
366366
fixture.componentInstance.ngOnChanges();
@@ -370,7 +370,7 @@ describe('DetailsFieldsComponent', () => {
370370
component.entryForm.patchValue({end_hour: updatedEndHour});
371371

372372
const expectedEndDate = moment(updatedEndDate).seconds(0).millisecond(0).toISOString();
373-
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
373+
expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate);
374374
});
375375

376376
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: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

0 commit comments

Comments
 (0)