From 92ab5c960a1c8d805b3ab0b268f3399b7eeb0c84 Mon Sep 17 00:00:00 2001 From: Klaus Zanders Date: Wed, 18 Dec 2024 14:47:59 +0100 Subject: [PATCH] replace form when work package has changed --- .../dynamic/time-entry.controller.ts | 25 ++++++++------- .../time_entries/time_entry_form_component.rb | 6 +++- .../controllers/time_entries_controller.rb | 32 +++++++------------ modules/costs/config/routes.rb | 2 +- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts b/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts index 3f77f5325c0d..536c425e7001 100644 --- a/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts +++ b/frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts @@ -37,8 +37,9 @@ import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service import { PathHelperService } from 'core-app/core/path-helper/path-helper.service'; export default class TimeEntryController extends Controller { - static targets = ['startTimeInput', 'endTimeInput', 'hoursInput']; + static targets = ['startTimeInput', 'endTimeInput', 'hoursInput', 'form']; + declare readonly formTarget:HTMLFormElement; declare readonly startTimeInputTarget:HTMLInputElement; declare readonly hasStartTimeInputTarget:boolean; declare readonly endTimeInputTarget:HTMLInputElement; @@ -62,12 +63,16 @@ export default class TimeEntryController extends Controller { ); } - workPackageChanged(event:InputEvent) { - const workPackageId = (event.currentTarget as HTMLInputElement).value; - void this.turboRequests.request( - this.pathHelper.timeEntriesWorkPackageActivity(workPackageId), - { method: 'GET' }, - ); + workPackageChanged() { + const url = this.formTarget.dataset.refreshFormUrl as string; + const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || ''; + void this.turboRequests.request(url, { + method: 'post', + body: new FormData(this.formTarget), + headers: { + 'X-CSRF-Token': csrfToken, + }, + }); } timeInputChanged(event:InputEvent) { @@ -89,11 +94,7 @@ export default class TimeEntryController extends Controller { // We calculate the hours field if: // - We have start & end time and no hours // - We have start & end time and we have triggered the change from the end time field - if ( - startTimeInMinutes - && endTimeInMinutes - && (hoursInMinutes === 0 || initiatedBy === this.endTimeInputTarget) - ) { + if (startTimeInMinutes && endTimeInMinutes && (hoursInMinutes === 0 || initiatedBy === this.endTimeInputTarget)) { hoursInMinutes = endTimeInMinutes - startTimeInMinutes; if (hoursInMinutes <= 0) { hoursInMinutes += 24 * 60; diff --git a/modules/costs/app/components/time_entries/time_entry_form_component.rb b/modules/costs/app/components/time_entries/time_entry_form_component.rb index 0069b5b31869..f7ba69bff05b 100644 --- a/modules/costs/app/components/time_entries/time_entry_form_component.rb +++ b/modules/costs/app/components/time_entries/time_entry_form_component.rb @@ -47,7 +47,11 @@ def initialize(time_entry:) def form_options base = { model: time_entry, - data: { turbo: true }, + data: { + turbo: true, + "time-entry-target" => "form", + refresh_form_url: refresh_form_time_entries_path + }, id: "time-entry-form" } diff --git a/modules/costs/app/controllers/time_entries_controller.rb b/modules/costs/app/controllers/time_entries_controller.rb index 5fe18175e283..e5400770b074 100644 --- a/modules/costs/app/controllers/time_entries_controller.rb +++ b/modules/costs/app/controllers/time_entries_controller.rb @@ -38,7 +38,7 @@ class TimeEntriesController < ApplicationController before_action :load_or_build_and_authorize_time_entry end - authorization_checked! :dialog, :create, :update, :user_tz_caption, :time_entry_activities + authorization_checked! :dialog, :create, :update, :user_tz_caption, :refresh_form def dialog if params[:date].present? @@ -60,27 +60,19 @@ def user_tz_caption respond_with_turbo_streams end - def time_entry_activities - work_package = WorkPackage.visible.find_by(id: params[:work_package_id]) - - time_entry = TimeEntry.new(project: work_package&.project, work_package: work_package) - - form = Primer::Forms::Builder.new( - TimeEntry.model_name.param_key, - time_entry, - TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry), - { - allow_method_names_outside_object: true, - skip_default_ids: false, - data: { turbo: true }, - id: "time-entry-form", - method: :post, - builder: Primer::Forms::Builder - } - ) + def refresh_form + call = TimeEntries::SetAttributesService.new( + user: current_user, + model: TimeEntry.new, + contract_class: EmptyContract + ).call(permitted_params.time_entries) + + time_entry = call.result + + # TODO Some logic to figure out what fields we want to show (user, etc) replace_via_turbo_stream( - component: TimeEntries::ActivityForm.new(form) + component: TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry) ) respond_with_turbo_streams diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rb index 6e8716ce9413..7123fbd316bd 100644 --- a/modules/costs/config/routes.rb +++ b/modules/costs/config/routes.rb @@ -30,7 +30,7 @@ resources :time_entries, only: %i[create update] do get :dialog, on: :collection get "/users/:user_id/tz_caption", action: :user_tz_caption, on: :collection - get "/work_packages/:work_package_id/time_entry_activities", action: :time_entry_activities, on: :collection + post :refresh_form, on: :collection end scope "projects/:project_id", as: "projects" do