Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[61143] Relations tab: Auto-scroll to a newly-created child #17818

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def initialize(work_package:, base_errors: nil)

def submit_url_options
{ method: :post,
url: work_package_children_relations_path(@work_package) }
url: work_package_children_relations_path(@work_package, isNew: false) }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%=
render(Primer::Alpha::Dialog.new(
id: "create-work-package-dialog",

Check failure on line 3 in app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb#L3

Layout/FirstArgumentIndentation: Indent the first argument one step more than `Primer::Alpha::Dialog.new(`.
Raw output
app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb:3:4: Layout/FirstArgumentIndentation: Indent the first argument one step more than `Primer::Alpha::Dialog.new(`.
title: I18n.t(:label_work_package_new),
size: :xlarge,

Check failure on line 5 in app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb#L5

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
Raw output
app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb:5:17: Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
)) do |dialog|
dialog.with_header(variant: :large)
dialog.with_body do
render(WorkPackageRelationsTab::CreateWorkPackageChildFormComponent.new(work_package:, project:))
end

dialog.with_footer do
component_collection do |modal_footer|
modal_footer.with_component(
Primer::ButtonComponent.new(
data: { 'close-dialog-id': "create-work-package-dialog" }

Check failure on line 16 in app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb#L16

Style/QuotedSymbols: Prefer double-quoted symbols unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb:16:20: Style/QuotedSymbols: Prefer double-quoted symbols unless you need single quotes to avoid extra backslashes for escaping.
)) do

Check failure on line 17 in app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb#L17

Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.
Raw output
app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb:17:11: Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.
I18n.t(:button_cancel)
end

modal_footer.with_component(
Primer::ButtonComponent.new(
scheme: :primary,
form: "create-work-package-form",
type: :submit
)) do

Check failure on line 26 in app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb#L26

Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.
Raw output
app/components/work_package_relations_tab/create_work_package_child_dialog_component.html.erb:26:11: Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.
if @work_package.persisted?
I18n.t(:button_save)
else
I18n.t(:button_create)
end
end
end
end
end
%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

class WorkPackageRelationsTab::CreateWorkPackageChildDialogComponent < ApplicationComponent
include ApplicationHelper
include OpenProject::FormTagHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers

attr_reader :work_package, :project

def initialize(work_package:, project:)
super

@work_package = work_package
@project = project
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%=
component_wrapper do
primer_form_with(
scope: :work_package,
model: work_package,
url: work_package_children_relations_url(isNew: true),
method: :post,
html: {
id: 'create-work-package-form',

Check failure on line 9 in app/components/work_package_relations_tab/create_work_package_child_form_component.html.erb

View workflow job for this annotation

GitHub Actions / reviewdog

app/components/work_package_relations_tab/create_work_package_child_form_component.html.erb#L9

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
app/components/work_package_relations_tab/create_work_package_child_form_component.html.erb:9:12: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
data: {
controller: "work-packages--create-dialog",
application_target: "dynamic",
"work-packages--create-dialog-refresh-url-value": refresh_form_work_package_children_relations_path(project_id: @project)
}
}
) do |f|
flex_layout(mb: 3) do |modal_body|
if work_package.errors[:base].present?
modal_body.with_row do
render(Primer::Alpha::Banner.new(mb: 3, icon: :stop, scheme: :danger)) { work_package.errors[:base].join("\n") }
end
end

modal_body.with_row do
render(WorkPackages::Dialogs::CreateForm.new(f, work_package:, wrapper_id: "#create-work-package-dialog"))
end
end
end
end
%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

class WorkPackageRelationsTab::CreateWorkPackageChildFormComponent < ApplicationComponent
include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers

attr_reader :work_package, :project

def initialize(work_package:, project:)
super

@work_package = work_package
@project = project
end
end
4 changes: 2 additions & 2 deletions app/components/work_package_relations_tab/index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def render_child_menu_items(menu) # rubocop:disable Metrics/AbcSize
if helpers.current_user.allowed_in_project?(:add_work_packages, work_package.project)
menu.with_item(
label: t("work_package_relations_tab.relations.new_child"),
href: new_project_work_packages_dialog_path(work_package.project, parent_id: work_package.id),
href: new_work_package_children_relation_path(project: work_package.project, parent_id: work_package.id, isNew: true),
content_arguments: {
data: { turbo_stream: true }
}
Expand All @@ -118,7 +118,7 @@ def render_child_menu_items(menu) # rubocop:disable Metrics/AbcSize

menu.with_item(
label: t("work_package_relations_tab.relations.existing_child"),
href: new_work_package_children_relation_path(work_package),
href: new_work_package_children_relation_path(work_package, isNew: false),
content_arguments: {
data: { turbo_stream: true }
}
Expand Down
68 changes: 64 additions & 4 deletions app/controllers/work_package_children_relations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,27 @@
before_action :authorize # Short-circuit early if not authorized

def new
component = WorkPackageRelationsTab::AddWorkPackageChildDialogComponent
.new(work_package: @work_package)
is_new = params[:isNew] == "true" # Convert param string to boolean

component = if is_new

WorkPackageRelationsTab::CreateWorkPackageChildDialogComponent
.new(work_package: @new_child_work_package, project: @project)
else
WorkPackageRelationsTab::AddWorkPackageChildDialogComponent
.new(work_package: @work_package)
end

respond_with_dialog(component)
end

def create
child = WorkPackage.find(params[:work_package][:id])
service_result = set_relation(child:, parent: @work_package)
if @is_new == "true"
service_result = WorkPackages::CreateService.new(user: current_user).call(create_params)
else
child = WorkPackage.find(params[:work_package][:id])
service_result = set_relation(child:, parent: @work_package)
end

respond_with_relations_tab_update(service_result, relation_to_scroll_to: service_result.result)
end
Expand All @@ -56,6 +69,19 @@
respond_with_relations_tab_update(service_result)
end

def refresh_form
call = WorkPackages::SetAttributesService.new(
user: current_user,
model: WorkPackage.new,
contract_class: EmptyContract
).call(create_params)

form_component = WorkPackageRelationsTab::CreateWorkPackageChildFormComponent.new(work_package: call.result, project: @project)

Check notice on line 79 in app/controllers/work_package_children_relations_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/work_package_children_relations_controller.rb#L79 <Layout/LineLength>

Line is too long. [131/130]
Raw output
app/controllers/work_package_children_relations_controller.rb:79:131: C: Layout/LineLength: Line is too long. [131/130]
update_via_turbo_stream(component: form_component)

respond_with_turbo_streams
end

private

def set_relation(child:, parent:)
Expand All @@ -77,7 +103,41 @@
end

def set_work_package
@is_new = params[:isNew]
if(params[:isNew] == "true")

Check notice on line 107 in app/controllers/work_package_children_relations_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/work_package_children_relations_controller.rb#L107 <Layout/SpaceAroundKeyword>

Space after keyword `if` is missing.
Raw output
app/controllers/work_package_children_relations_controller.rb:107:5: C: Layout/SpaceAroundKeyword: Space after keyword `if` is missing.
build_work_package

end
@work_package = WorkPackage.find(params[:work_package_id])
@project = @work_package.project
end
def build_work_package

Check notice on line 114 in app/controllers/work_package_children_relations_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/work_package_children_relations_controller.rb#L114 <Layout/EmptyLineBetweenDefs>

Expected 1 empty line between method definitions; found 0.
Raw output
app/controllers/work_package_children_relations_controller.rb:114:3: C: Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 0.
initial = WorkPackage.new(project: @project)

call = WorkPackages::SetAttributesService
.new(model: initial, user: current_user, contract_class: WorkPackages::CreateContract)
.call(new_params.reverse_merge(default_params(initial)))

# We ignore errors here, as we only want to build the work package
@new_child_work_package = call.result
@new_child_work_package.errors.clear
@new_child_work_package.custom_values.each { |cv| cv.errors.clear }
end

def new_params
params.permit(*PermittedParams.permitted_attributes[:new_work_package])
end

def create_params
permitted_params.update_work_package.merge(project: @project)
end

def default_params(work_package)
contract = WorkPackages::CreateContract.new(work_package, current_user)

{
type: contract.assignable_types.first,
project: @project
}
end
end
104 changes: 104 additions & 0 deletions app/controllers/work_package_create_child_relation_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# frozen_string_literal: true

# -- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
# ++

class WorkPackageCreateChildRelationController < ApplicationController
include OpTurbo::ComponentStream
include OpTurbo::DialogStreamHelper

before_action :find_project_by_project_id
before_action :build_work_package, only: %i[new]

authorize_with_permission :add_work_packages

def new
respond_with_dialog WorkPackageRelationsTab::CreateWorkPackageChildDialogComponent.new(work_package: @work_package)
end

def create
call = WorkPackages::CreateService.new(user: current_user).call(create_params)

if call.success?
flash[:notice] = I18n.t("work_package_relations_tab.relations.label_new_child_created")
redirect_back fallback_location: project_work_package_path(@project, call.result), status: :see_other
else
form_component = WorkPackageRelationsTab::CreateWorkPackageChildFormComponent.new(work_package: call.result, project: @project)

Check notice on line 51 in app/controllers/work_package_create_child_relation_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/work_package_create_child_relation_controller.rb#L51 <Layout/LineLength>

Line is too long. [133/130]
Raw output
app/controllers/work_package_create_child_relation_controller.rb:51:131: C: Layout/LineLength: Line is too long. [133/130]
update_via_turbo_stream(component: form_component, status: :bad_request)

respond_with_turbo_streams
end
end

def refresh_form
call = WorkPackages::SetAttributesService.new(
user: current_user,
model: WorkPackage.new,
contract_class: EmptyContract
).call(create_params)

form_component = WorkPackageRelationsTab::CreateWorkPackageChildFormComponent.new(work_package: call.result, project: @project)

Check notice on line 65 in app/controllers/work_package_create_child_relation_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/work_package_create_child_relation_controller.rb#L65 <Layout/LineLength>

Line is too long. [131/130]
Raw output
app/controllers/work_package_create_child_relation_controller.rb:65:131: C: Layout/LineLength: Line is too long. [131/130]
update_via_turbo_stream(component: form_component)

respond_with_turbo_streams
end

private

def build_work_package
initial = WorkPackage.new(project: @project)

call = WorkPackages::SetAttributesService
.new(model: initial, user: current_user, contract_class: WorkPackages::CreateContract)
.call(new_params.reverse_merge(default_params(initial)))

# We ignore errors here, as we only want to build the work package
@work_package = call.result
@work_package.errors.clear
@work_package.custom_values.each { |cv| cv.errors.clear }
end

def new_params
params.permit(*PermittedParams.permitted_attributes[:new_work_package])
end

def create_params
permitted_params.update_work_package.merge(project: @project)
end

def default_params(work_package)
contract = WorkPackages::CreateContract.new(work_package, current_user)

{
type: contract.assignable_types.first,
project: @project
}
end

def default_breadcrumb; end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@
end
end

resources :children_relations, only: %i[new create destroy], controller: "work_package_children_relations"
resources :children_relations, only: %i[new create destroy], controller: "work_package_children_relations" do
post :refresh_form
end

resource :progress, only: %i[new edit update], controller: "work_packages/progress"
collection do
Expand Down
Loading