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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[61689] Calculate costs only when relevant fields are changed
Christophe Bliardc7fc800d74a4
diff --git a/frontend/src/app/shared/components/remote-field-updater/remote-field-updater.component.ts b/frontend/src/app/shared/components/remote-field-updater/remote-field-updater.component.tsindex e233a3ea312..5f541cd9adb 100644--- a/frontend/src/app/shared/components/remote-field-updater/remote-field-updater.component.ts+++ b/frontend/src/app/shared/components/remote-field-updater/remote-field-updater.component.ts@@ -26,8 +26,7 @@// See COPYRIGHT and LICENSE files for more details.//++-import { ChangeDetectionStrategy, Component, ElementRef, OnInit } from '@angular/core';-import { KeyCodes } from 'core-app/shared/helpers/keyCodes.enum';+import { ChangeDetectionStrategy, Component, ElementRef, OnDestroy, OnInit } from '@angular/core';import { HttpClient } from '@angular/common/http';export const remoteFieldUpdaterSelector = 'remote-field-updater';@@ -37,7 +36,7 @@ export const remoteFieldUpdaterSelector = 'remote-field-updater';changeDetection: ChangeDetectionStrategy.OnPush,template: '',})-export class RemoteFieldUpdaterComponent implements OnInit {+export class RemoteFieldUpdaterComponent implements OnInit, OnDestroy {constructor(private elementRef:ElementRef,private http:HttpClient,@@ -52,37 +51,61 @@ export class RemoteFieldUpdaterComponent implements OnInit {private target:HTMLElement;+ private debouncedUpdaterBound:EventListener;++ private spentOnTextField:HTMLInputElement | null = null;+ private costTypeSelect:HTMLInputElement | null = null;+ private unitsTextField:HTMLInputElement | null = null;+ngOnInit():void {const element = this.elementRef.nativeElement as HTMLElement;this.form = element.closest('form') as HTMLFormElement;this.target = this.form.querySelector('.remote-field--target') as HTMLElement;+ this.costTypeSelect = this.form.querySelector('#cost_entry_cost_type_id');+ this.unitsTextField = this.form.querySelector('#cost_entry_units');this.url = element.dataset.url as string;this.htmlMode = element.dataset.mode === 'html';- const debouncedEvent = _.debounce((event:InputEvent) => {- // Do not handle the change event on input fields, because they can trigger the- // debouncedEvent after a form submission which will lead to a broken UI update.- // This can happen if the form submission is clicked when an input field is still focused.- const target = event.target as HTMLElement | null;- if (event.type === 'change' && target instanceof HTMLInputElement) {- return;- }-- // This prevents an update of the result list when- // tabbing to the result list (9),- // pressing enter (13)- // tabbing back with shift (16) and- // special cases where the tab code is not correctly recognized (undefined).- // Thus the focus is kept on the first element of the result list.- const keyCodes = [KeyCodes.TAB, KeyCodes.ENTER, KeyCodes.SHIFT];- if (event.type === 'change' || (event.which && keyCodes.indexOf(event.which) === -1)) {- this.updater();- }- }, 500);-- this.form.addEventListener('keyup', debouncedEvent);- this.form.addEventListener('change', debouncedEvent);+ this.debouncedUpdaterBound = _.debounce(this.updater.bind(this), 500);++ this.addListeners();+ }++ ngOnDestroy():void {+ this.removeListeners();+ }++ private addListeners() {+ this.addEventListenerWhenSpentOnFieldIsAdded('input', this.debouncedUpdaterBound);+ this.costTypeSelect?.addEventListener('change', this.debouncedUpdaterBound);+ this.unitsTextField?.addEventListener('input', this.debouncedUpdaterBound);+ }++ private removeListeners() {+ if (this.debouncedUpdaterBound && 'cancel' in this.debouncedUpdaterBound) {+ (this.debouncedUpdaterBound as { cancel:() => void }).cancel();+ }+ this.spentOnTextField?.removeEventListener('input', this.debouncedUpdaterBound);+ this.costTypeSelect?.removeEventListener('change', this.debouncedUpdaterBound);+ this.unitsTextField?.removeEventListener('input', this.debouncedUpdaterBound);+ }++ private addEventListenerWhenSpentOnFieldIsAdded(type:string, eventListener:EventListener) {+ // Use MutationObserver to watch for the addition of the spent_on input+ // field. This input field is a date picker.+ const observer = new MutationObserver((mutations) => {+ mutations+ .filter((mutation) => mutation.type === 'childList')+ .forEach(() => {+ if (this.spentOnTextField === null && this.form.querySelector('#cost_entry_spent_on')) {+ this.spentOnTextField = this.form.querySelector('#cost_entry_spent_on') as HTMLInputElement;+ this.spentOnTextField.addEventListener(type, eventListener);+ observer.disconnect(); // Stop observing once the element is found and listener is added+ }+ });+ });+ observer.observe(this.form, { childList: true, subtree: true });}private request(params:Record<string, string>) {diff --git a/modules/costs/spec/features/cost_entries/add_cost_entry_spec.rb b/modules/costs/spec/features/cost_entries/add_cost_entry_spec.rbindex 5f3bd2cd37e..c7b67ceda1d 100644--- a/modules/costs/spec/features/cost_entries/add_cost_entry_spec.rb+++ b/modules/costs/spec/features/cost_entries/add_cost_entry_spec.rb@@ -68,24 +68,18 @@ RSpec.describe "Work Package cost fields", :js dologin_as userend- # It is necessary to use send keys, because some UI events are triggered on keyboard input.- def fill_in_with_send_keys(field_name, with:)- fill_in(field_name, with:)- find_field(field_name).send_keys(:right)- end-it "does not show read-only fields" dofull_view.visit!full_view.select_log_unit_costs_action# Set single value, should update suffixselect "A", from: "cost_entry_cost_type_id"- fill_in_with_send_keys "cost_entry_units", with: "1"+ fill_in "cost_entry_units", with: "1"expect(page).to have_css("#cost_entry_unit_name", text: "A single")expect(page).to have_css("#cost_entry_costs", text: "1.00 EUR")- fill_in_with_send_keys "cost_entry_units", with: "2"+ fill_in "cost_entry_units", with: "2"expect(page).to have_css("#cost_entry_unit_name", text: "A plural")expect(page).to have_css("#cost_entry_costs", text: "2.00 EUR")@@ -97,7 +91,8 @@ RSpec.describe "Work Package cost fields", :js do# Override costsfind_by_id("cost_entry_costs").clickSeleniumHubWaiter.wait- fill_in_with_send_keys "cost_entry_overridden_costs", with: "15.52"+ fill_in "cost_entry_overridden_costs", with: "15.52"+click_on "Save"# Expect correct costs@@ -120,7 +115,7 @@ RSpec.describe "Work Package cost fields", :js dofull_view.visit!full_view.select_log_unit_costs_action- fill_in_with_send_keys CostEntry.human_attribute_name(:units), with: "1,42"+ fill_in CostEntry.human_attribute_name(:units), with: "1,42"expect(page).to have_css("#cost_entry_costs", text: "1,42 EUR")select "B", from: CostEntry.human_attribute_name(:cost_type)@@ -130,7 +125,7 @@ RSpec.describe "Work Package cost fields", :js do# Override costsfind_by_id("cost_entry_costs").clickSeleniumHubWaiter.wait- fill_in_with_send_keys "cost_entry_overridden_costs", with: "1.350,25"+ fill_in "cost_entry_overridden_costs", with: "1.350,25"click_on I18n.t(:button_save)@@ -152,7 +147,7 @@ RSpec.describe "Work Package cost fields", :js do# Update the costs in german localeSeleniumHubWaiter.wait- fill_in_with_send_keys "cost_entry_overridden_costs", with: "55.000,55"+ fill_in "cost_entry_overridden_costs", with: "55.000,55"click_on I18n.t(:button_save)# Add explicit wait for the updated cost value