diff --git a/modules/costs/app/components/time_entries/entry_dialog_component.html.erb b/modules/costs/app/components/time_entries/entry_dialog_component.html.erb index b3a7b7d9734c..470d6bb48df5 100644 --- a/modules/costs/app/components/time_entries/entry_dialog_component.html.erb +++ b/modules/costs/app/components/time_entries/entry_dialog_component.html.erb @@ -1,7 +1,7 @@ <%= render(Primer::Alpha::Dialog.new(title: t('caption_log_time_dialog'), size: :large, id: MODAL_ID, data: { "controller" => "time-entry", "application-target" => "dynamic" })) do |d| %> <% d.with_header(variant: :large, mb: 3) %> <% d.with_body do %> - <%= render(TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry)) %> + <%= render(TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry, show_user: show_user, show_work_package: show_work_package)) %> <% end %> <% d.with_footer(variant: :large) do %> <%= render(Primer::ButtonComponent.new(data: { 'close-dialog-id': "time-entry-dialog" })) { I18n.t("button_cancel") } %> diff --git a/modules/costs/app/components/time_entries/entry_dialog_component.rb b/modules/costs/app/components/time_entries/entry_dialog_component.rb index d35afcd7c98f..abed2599d55a 100644 --- a/modules/costs/app/components/time_entries/entry_dialog_component.rb +++ b/modules/costs/app/components/time_entries/entry_dialog_component.rb @@ -34,14 +34,15 @@ class EntryDialogComponent < ApplicationComponent MODAL_ID = "time-entry-dialog" - def initialize(time_entry:, open: false) + def initialize(time_entry:, show_user: true, show_work_package: true) super() @time_entry = time_entry - @open = open + @show_user = show_user + @show_work_package = show_work_package end private - attr_reader :time_entry, :open + attr_reader :time_entry, :open, :show_user, :show_work_package end end diff --git a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb b/modules/costs/app/components/time_entries/time_entry_form_component.html.erb index 51f396438dc3..89ce8cbd0e75 100644 --- a/modules/costs/app/components/time_entries/time_entry_form_component.html.erb +++ b/modules/costs/app/components/time_entries/time_entry_form_component.html.erb @@ -30,9 +30,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. <%= component_wrapper do primer_form_with(**form_options) do |form| flex_layout do |body| - body.with_row(mb: 3) { render(TimeEntries::UserForm.new(form)) } + body.with_row(mb: 3) { render(TimeEntries::UserForm.new(form, visible: show_user)) } body.with_row(mb: 3) { render(TimeEntries::DaysAndHoursForm.new(form)) } - body.with_row(mb: 3) { render(TimeEntries::WorkPackageForm.new(form)) } + body.with_row(mb: 3) { render(TimeEntries::WorkPackageForm.new(form, visible: show_work_package)) } body.with_row(mb: 3) { render(TimeEntries::ActivityForm.new(form)) } body.with_row(mb: 3) { render(TimeEntries::CommentsForm.new(form)) } body.with_row(mb: 3) { render(TimeEntries::CustomFieldsForm.new(form)) } 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 f7ba69bff05b..c81c15257b8d 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 @@ -33,14 +33,16 @@ class TimeEntryFormComponent < ApplicationComponent include OpTurbo::Streamable include OpPrimer::ComponentHelpers - def initialize(time_entry:) + def initialize(time_entry:, show_user: true, show_work_package: true) super() @time_entry = time_entry + @show_user = show_user + @show_work_package = show_work_package end private - attr_reader :time_entry + attr_reader :time_entry, :show_user, :show_work_package delegate :project, :work_package, to: :time_entry diff --git a/modules/costs/app/components/time_entries/user_form.rb b/modules/costs/app/components/time_entries/user_form.rb index 5869292959a6..2fbf482675ef 100644 --- a/modules/costs/app/components/time_entries/user_form.rb +++ b/modules/costs/app/components/time_entries/user_form.rb @@ -30,7 +30,14 @@ module TimeEntries class UserForm < ApplicationForm + def initialize(visible: true) + super() + @visible = visible + end + form do |f| + f.hidden name: :show_user, value: @visible + if show_user_field? f.autocompleter( name: :user_id, @@ -47,8 +54,7 @@ class UserForm < ApplicationForm resource: "principals", focusDirectly: false, multiple: false, - appendTo: "#time-entry-dialog", - disabled: !show_user_field? + appendTo: "#time-entry-dialog" } ) end @@ -59,9 +65,10 @@ class UserForm < ApplicationForm delegate :project, to: :model def show_user_field? - # Only allow setting a different user, when the user has the - # permission to log time for others in the project - User.current.allowed_in_project?(:log_time, project) + return false unless @visible + return false if project && !User.current.allowed_in_project?(:log_time, project) + + true end def user_completer_filters diff --git a/modules/costs/app/components/time_entries/work_package_form.rb b/modules/costs/app/components/time_entries/work_package_form.rb index 5ef4b1059f4d..b5fae9a69b7c 100644 --- a/modules/costs/app/components/time_entries/work_package_form.rb +++ b/modules/costs/app/components/time_entries/work_package_form.rb @@ -30,21 +30,38 @@ module TimeEntries class WorkPackageForm < ApplicationForm + def initialize(visible: true) + super() + @visible = visible + end + form do |f| - f.work_package_autocompleter name: :work_package_id, - label: TimeEntry.human_attribute_name(:work_package), - required: true, - autocomplete_options: { - hiddenFieldAction: "change->time-entry#workPackageChanged", - focusDirectly: false, - append_to: "#time-entry-dialog", - url: work_package_completer_url, - filters: work_package_completer_filters - } + f.hidden name: :show_work_package, value: @visible + + if show_work_package_field? + f.work_package_autocompleter name: :work_package_id, + label: TimeEntry.human_attribute_name(:work_package), + required: true, + autocomplete_options: { + hiddenFieldAction: "change->time-entry#workPackageChanged", + focusDirectly: false, + append_to: "#time-entry-dialog", + url: work_package_completer_url, + filters: work_package_completer_filters + } + else + f.hidden name: :work_package_id, value: model.work_package_id + end end private + def show_work_package_field? + return true if model.work_package_id.nil? + + @visible + end + def work_package_completer_url if model.persisted? ::API::V3::Utilities::PathHelper::ApiV3Path.time_entries_available_work_packages_on_edit(model.id) diff --git a/modules/costs/app/controllers/time_entries_controller.rb b/modules/costs/app/controllers/time_entries_controller.rb index e5400770b074..625e49d24c8f 100644 --- a/modules/costs/app/controllers/time_entries_controller.rb +++ b/modules/costs/app/controllers/time_entries_controller.rb @@ -41,6 +41,13 @@ class TimeEntriesController < ApplicationController authorization_checked! :dialog, :create, :update, :user_tz_caption, :refresh_form def dialog + @show_work_package = params[:work_package_id].blank? + @show_user = if @project + User.current.allowed_in_project?(:log_time, @project) + else + User.current.allowed_in_any_project?(:log_time) + end + if params[:date].present? @time_entry.spent_on = params[:date] end @@ -72,7 +79,7 @@ def refresh_form # TODO Some logic to figure out what fields we want to show (user, etc) replace_via_turbo_stream( - component: TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry) + component: TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry, **form_config_options) ) respond_with_turbo_streams @@ -86,7 +93,7 @@ def create @time_entry = call.result unless call.success? - form_component = TimeEntries::TimeEntryFormComponent.new(time_entry: @time_entry) + form_component = TimeEntries::TimeEntryFormComponent.new(time_entry: @time_entry, **form_config_options) update_via_turbo_stream(component: form_component, status: :bad_request) respond_with_turbo_streams @@ -103,7 +110,7 @@ def update @time_entry = call.result unless call.success? - form_component = TimeEntries::TimeEntryFormComponent.new(time_entry: @time_entry) + form_component = TimeEntries::TimeEntryFormComponent.new(time_entry: @time_entry, **form_config_options) update_via_turbo_stream(component: form_component, status: :bad_request) respond_with_turbo_streams @@ -112,6 +119,13 @@ def update private + def form_config_options + { + show_user: params[:time_entry][:show_user] == "true", + show_work_package: params[:time_entry][:show_work_package] == "true" + } + end + def load_and_authorize_optional_project if params[:project_id].present? @project = Project.visible.find(params[:project_id]) diff --git a/modules/costs/app/views/time_entries/dialog.turbo_stream.erb b/modules/costs/app/views/time_entries/dialog.turbo_stream.erb index bf0c45699789..c8a95c962517 100644 --- a/modules/costs/app/views/time_entries/dialog.turbo_stream.erb +++ b/modules/costs/app/views/time_entries/dialog.turbo_stream.erb @@ -1,3 +1,7 @@ <%= turbo_stream.dialog do - render(TimeEntries::EntryDialogComponent.new(time_entry: @time_entry)) + render(TimeEntries::EntryDialogComponent.new( + time_entry: @time_entry, + show_user: @show_user, + show_work_package: @show_work_package + )) end %>