From 003bd677e0c28da8f870e0a07bcd6ab95c82001d Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Fri, 19 Apr 2024 09:46:36 -0500 Subject: [PATCH] Fix modal rendering for new work packages --- .../work_packages/progress_controller.rb | 66 +++++++++++++------ config/routes.rb | 2 +- .../core/path-helper/path-helper.service.ts | 4 ++ 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/app/controllers/work_packages/progress_controller.rb b/app/controllers/work_packages/progress_controller.rb index 8d34771fad6a..785a62c97b39 100644 --- a/app/controllers/work_packages/progress_controller.rb +++ b/app/controllers/work_packages/progress_controller.rb @@ -40,18 +40,24 @@ class WorkPackages::ProgressController < ApplicationController helper_method :modal_class + def new + build_up_brand_new_work_package + + render modal_class.new(@work_package, + focused_field: params[:field], + touched_field_map:) + end + def edit - build_up_new_work_package + build_up_work_package render modal_class.new(@work_package, focused_field: params[:field], - touched_field_map: params.require(:work_package).permit("estimated_hours_touched", - "remaining_hours_touched", - "status_id_touched").to_h) + touched_field_map:) end def create - service_call = build_up_new_work_package + service_call = build_up_work_package if service_call.errors .map(&:attribute) @@ -75,7 +81,7 @@ def update service_call = WorkPackages::UpdateService .new(user: current_user, model: @work_package) - .call(update_work_package_params) + .call(work_package_params) if service_call.success? respond_to do |format| @@ -110,13 +116,19 @@ def set_work_package @work_package = WorkPackage.new end + def touched_field_map + params.require(:work_package).permit("estimated_hours_touched", + "remaining_hours_touched", + "status_id_touched").to_h + end + def extract_persisted_progress_attributes @persisted_progress_attributes = @work_package .attributes .slice("estimated_hours", "remaining_hours", "status_id") end - def update_work_package_params + def work_package_params params.require(:work_package) .permit(allowed_params) end @@ -130,33 +142,45 @@ def allowed_params end def reject_params_that_dont_differ_from_persisted_values - update_work_package_params.reject do |key, value| + work_package_params.reject do |key, value| @persisted_progress_attributes[key.to_s].to_f.to_s == value.to_f.to_s end end - def final_params + def filtered_work_package_params {}.tap do |filtered_params| - if params.require(:work_package)[:estimated_hours_touched] == "true" - filtered_params[:estimated_hours] = update_work_package_params.fetch("estimated_hours") - end + filtered_params[:estimated_hours] = work_package_params["estimated_hours"] if estimated_hours_touched? + filtered_params[:remaining_hours] = work_package_params["remaining_hours"] if remaining_hours_touched? + filtered_params[:status_id] = work_package_params["status_id"] if status_id_touched? + end + end - if params.require(:work_package)[:remaining_hours_touched] == "true" - filtered_params[:remaining_hours] = update_work_package_params.fetch("remaining_hours") - end + def estimated_hours_touched? + params.require(:work_package)[:estimated_hours_touched] == "true" + end - if params.require(:work_package)[:status_id_touched] == "true" - filtered_params[:status_id] = update_work_package_params.fetch("status_id") - end - end + def remaining_hours_touched? + params.require(:work_package)[:remaining_hours_touched] == "true" + end + + def status_id_touched? + params.require(:work_package)[:status_id_touched] == "true" + end + + def build_up_work_package + WorkPackages::SetAttributesService + .new(user: current_user, + model: @work_package, + contract_class: WorkPackages::CreateContract) + .call(filtered_work_package_params) end - def build_up_new_work_package + def build_up_brand_new_work_package WorkPackages::SetAttributesService .new(user: current_user, model: @work_package, contract_class: WorkPackages::CreateContract) - .call(final_params) + .call(work_package_params) end def formatted_duration(hours) diff --git a/config/routes.rb b/config/routes.rb index 202af54fff36..6c95f681534b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -523,7 +523,7 @@ end end - resource :progress, only: %i[edit update], controller: "work_packages/progress" + resource :progress, only: %i[new edit update], controller: "work_packages/progress" collection do resource :progress, only: :create, diff --git a/frontend/src/app/core/path-helper/path-helper.service.ts b/frontend/src/app/core/path-helper/path-helper.service.ts index 74f7cab322f2..d3974bc03368 100644 --- a/frontend/src/app/core/path-helper/path-helper.service.ts +++ b/frontend/src/app/core/path-helper/path-helper.service.ts @@ -265,6 +265,10 @@ export class PathHelperService { } public workPackageProgressModalPath(workPackageId:string|number) { + if (workPackageId === 'new') { + return `${this.workPackagePath(workPackageId)}/progress/new`; + } + return `${this.workPackagePath(workPackageId)}/progress/edit`; }