Skip to content

Commit

Permalink
form knows when to render the user and work package field
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Dec 19, 2024
1 parent 92ab5c9 commit 8ca8100
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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") } %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 12 additions & 5 deletions modules/costs/app/components/time_entries/user_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
37 changes: 27 additions & 10 deletions modules/costs/app/components/time_entries/work_package_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions modules/costs/app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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])
Expand Down
6 changes: 5 additions & 1 deletion modules/costs/app/views/time_entries/dialog.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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 %>

0 comments on commit 8ca8100

Please sign in to comment.