Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Extract doubled code into a method

Henriette Darged6fed428fb5c
diff --git a/frontend/src/app/shared/components/datepicker/wp-modal-date-picker/wp-modal-date-picker.component.ts b/frontend/src/app/shared/components/datepicker/wp-modal-date-picker/wp-modal-date-picker.component.ts
index b6a8a2f2944..5c4e17b587a 100644
--- a/frontend/src/app/shared/components/datepicker/wp-modal-date-picker/wp-modal-date-picker.component.ts
+++ b/frontend/src/app/shared/components/datepicker/wp-modal-date-picker/wp-modal-date-picker.component.ts
@@ -144,34 +144,13 @@ export class OpWpModalDatePickerComponent extends UntilDestroyedMixin implements
this.ensureHoveredSelection(instance.calendarContainer);
},
onChange: (dates:Date[], _datestr, instance) => {
- // Todo: Make code better
if (this.fieldName === 'due_date') {
this.dueDate = dates[0];
-
- if (this.dueDateFieldId) {
- const dueDateField = document.getElementById(this.dueDateFieldId) as HTMLInputElement;
- dueDateField.value = this.timezoneService.formattedISODate(this.dueDate);
- dueDateField.dispatchEvent(new Event('input'));
- }
-
- // Toggle the active field
- if (this.startDateFieldId) {
- document.getElementById(this.startDateFieldId)?.focus();
- }
+ this.setDateFieldAndFocus(this.dueDate, this.dueDateFieldId, this.startDateFieldId);
this.fieldName = 'start_date';
} else {
this.startDate = dates[0];
-
- if (this.startDateFieldId) {
- const dueDateField = document.getElementById(this.startDateFieldId) as HTMLInputElement;
- dueDateField.value = this.timezoneService.formattedISODate(this.startDate);
- dueDateField.dispatchEvent(new Event('input'));
- }
-
- // Toggle the active field
- if (this.dueDateFieldId) {
- document.getElementById(this.dueDateFieldId)?.focus();
- }
+ this.setDateFieldAndFocus(this.startDate, this.startDateFieldId, this.dueDateFieldId);
this.fieldName = 'due_date';
}
@@ -218,4 +197,17 @@ export class OpWpModalDatePickerComponent extends UntilDestroyedMixin implements
)
.subscribe(() => calendarContainer.classList.add('flatpickr-container-suppress-hover'));
}
+
+ private setDateFieldAndFocus(date:Date, fieldId:string | null, nextFieldId:string | null):void {
+ if (fieldId) {
+ const field = document.getElementById(fieldId) as HTMLInputElement;
+ field.value = this.timezoneService.formattedISODate(date);
+ field.dispatchEvent(new Event('input'));
+ }
+
+ // Toggle focus to the next field
+ if (nextFieldId) {
+ document.getElementById(nextFieldId)?.focus();
+ }
+ }
}