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

Update duration with debounce, same as dates

Oliver Günther3a2a3612cd3e
diff --git a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.html b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.html
index ac33614df65..cc04b3acb24 100644
--- a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.html
+++ b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.html
@@ -117,8 +117,8 @@
class="op-datepicker-modal--date-field"
[attr.data-qa-highlighted]=" showFieldAsActive('duration') || undefined"
[ngClass]="{'op-datepicker-modal--date-field_current' : showFieldAsActive('duration')}"
- [ngModel]="displayedDuration"
- (ngModelChange)="updateDuration($event)"
+ [ngModel]="durationFocused ? duration : displayedDuration"
+ (ngModelChange)="durationChanges$.next($event)"
[disabled]="!isSchedulable"
(focusin)="handleDurationFocusIn()"
(focusout)="handleDurationFocusOut()"
diff --git a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
index e7c403c85af..c031aca1335 100644
--- a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
+++ b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
@@ -53,6 +53,7 @@ import { DayElement } from 'flatpickr/dist/types/instance';
import flatpickr from 'flatpickr';
import {
debounceTime,
+ distinctUntilChanged,
filter,
map,
switchMap,
@@ -75,7 +76,6 @@ import {
setDates,
validDate,
} from 'core-app/shared/components/datepicker/helpers/date-modal.helpers';
-import { whenOutside } from 'core-app/shared/directives/focus/contain-helpers';
import { castArray } from 'lodash';
import { WeekdayService } from 'core-app/core/days/weekday.service';
@@ -206,6 +206,24 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
this.cdRef.detectChanges();
});
+ // Duration changes
+ durationChanges$ = new Subject<string>();
+
+ durationDebounced$ = this
+ .durationChanges$
+ .pipe(
+ this.untilDestroyed(),
+ distinctUntilChanged(),
+ debounceTime(500),
+ map((value) => (value === '' ? null : parseInt(value, 10))),
+ filter((val) => val !== this.duration),
+ )
+ .subscribe((value) => this.applyDurationChange(value));
+
+ // Duration is a special field as it changes its value based on its focus state
+ // which is different from the highlight state...
+ durationFocused = false;
+
private changeset:ResourceChangeset;
private datePickerInstance:DatePicker;
@@ -368,30 +386,29 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
}
handleDurationFocusIn():void {
+ this.durationFocused = true;
this.setCurrentActivatedField('duration');
}
+ handleDurationFocusOut():void {
+ this.durationFocused = false;
+ }
+
get displayedDuration():string {
if (!this.duration) {
return '';
}
- if (this.isStateOfCurrentActivatedField('duration')) {
- return this.duration.toString(10);
- }
-
return this.text.days(this.duration);
}
- handleDurationFocusOut():void {
- // Apply changed duration once we really exited the field
- whenOutside(this.durationField.nativeElement, () => this.applyChangedDuration());
- }
+ private applyDurationChange(newValue:number|null):void {
+ this.duration = newValue;
+ this.cdRef.detectChanges();
- applyChangedDuration():void {
// If we cleared duration or left it empty
// reset the value and the due date
- if (!this.duration) {
+ if (newValue === null) {
this.updateDate('end', null);
return;
}
@@ -409,14 +426,6 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
}
}
- updateDuration(value:string|number):void {
- if (value === '') {
- this.duration = null;
- } else {
- this.duration = typeof value === 'string' ? parseInt(value, 10) : value;
- }
- }
-
private get durationAsIso8601():string|null {
if (this.duration) {
return this.timezoneService.toISODuration(this.duration, 'days');
@@ -685,10 +694,10 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
private setDurationDaysFromUpstream(value:string|null) {
const durationDays = value ? this.timezoneService.toDays(value) : null;
- if (durationDays !== null && durationDays > 0) {
- this.updateDuration(durationDays);
- } else {
+ if (!durationDays || durationDays === 0) {
this.duration = null;
+ } else {
+ this.duration = durationDays;
}
}
diff --git a/spec/features/work_packages/datepicker/datepicker_logic_spec.rb b/spec/features/work_packages/datepicker/datepicker_logic_spec.rb
index 3f379420412..172993b2689 100644
--- a/spec/features/work_packages/datepicker/datepicker_logic_spec.rb
+++ b/spec/features/work_packages/datepicker/datepicker_logic_spec.rb
@@ -74,6 +74,8 @@ describe 'Datepicker modal logic test cases (WP #43539)',
date_field.activate!
date_field.expect_active!
+ # Wait for the datepicker to be initialized
+ datepicker.expect_visible
end
context 'when only start_date set, updating duration (test case 1)' do
diff --git a/spec/support/components/datepicker/datepicker.rb b/spec/support/components/datepicker/datepicker.rb
index d1ccb47a2c9..c42b6717583 100644
--- a/spec/support/components/datepicker/datepicker.rb
+++ b/spec/support/components/datepicker/datepicker.rb
@@ -17,7 +17,7 @@ module Components
end
def expect_visible
- expect(flatpickr_container).to be_present
+ expect(container).to have_selector('.flatpickr-calendar .flatpickr-current-month', wait: 10)
end
##
diff --git a/spec/support/components/datepicker/work_package_datepicker.rb b/spec/support/components/datepicker/work_package_datepicker.rb
index a42333c5c6b..a19d8720959 100644
--- a/spec/support/components/datepicker/work_package_datepicker.rb
+++ b/spec/support/components/datepicker/work_package_datepicker.rb
@@ -33,13 +33,13 @@ module Components
##
# Expect start date
def expect_start_date(value)
- expect(container).to have_field('startDate', with: value, wait: 10)
+ expect(container).to have_field('startDate', with: value, wait: 20)
end
##
# Expect due date
def expect_due_date(value)
- expect(container).to have_field('endDate', with: value, wait: 10)
+ expect(container).to have_field('endDate', with: value, wait: 20)
end
##