Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#57816] added turbo streams to update the forms

Eric Schubert996bb65e732c
diff --git a/app/components/admin/custom_fields/hierarchy/item_component.html.erb b/app/components/admin/custom_fields/hierarchy/item_component.html.erb
new file mode 100644
index 00000000000..125c0300361
--- /dev/null
+++ b/app/components/admin/custom_fields/hierarchy/item_component.html.erb
@@ -0,0 +1,55 @@
+<%#-- 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.
+
+++#%>
+
+<%=
+ component_wrapper do
+ flex_layout(align_items: :center, justify_content: :space_between) do |item_container|
+ item_container.with_column(flex_layout: true) do |item_information|
+ item_information.with_column(mr: 2) do
+ render(Primer::Beta::Text.new(font_weight: :bold)) do
+ @hierarchy_item.label
+ end
+ end
+
+ unless @hierarchy_item.short.nil?
+ item_information.with_column(mr: 2) do
+ render(Primer::Beta::Text.new(color: :subtle)) { short_text }
+ end
+ end
+ end
+
+ # Actions
+ item_container.with_column do
+ render(Primer::Beta::IconButton.new(scheme: :default,
+ icon: "kebab-horizontal",
+ "aria-label": "some aria"))
+ end
+ end
+ end
+%>
diff --git a/app/components/admin/custom_fields/hierarchy/item_component.rb b/app/components/admin/custom_fields/hierarchy/item_component.rb
new file mode 100644
index 00000000000..ddf8b1e16fc
--- /dev/null
+++ b/app/components/admin/custom_fields/hierarchy/item_component.rb
@@ -0,0 +1,47 @@
+#-- 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.
+#++
+
+module Admin
+ module CustomFields
+ module Hierarchy
+ class ItemComponent < ApplicationComponent
+ include OpTurbo::Streamable
+ include OpPrimer::ComponentHelpers
+
+ def initialize(hierarchy_item:)
+ super
+ @hierarchy_item = hierarchy_item
+ end
+
+ def short_text
+ "(#{@hierarchy_item.short})"
+ end
+ end
+ end
+ end
+end
diff --git a/app/components/admin/custom_fields/hierarchy/items_component.html.erb b/app/components/admin/custom_fields/hierarchy/items_component.html.erb
index 390fd0da2e2..46ad4c34769 100644
--- a/app/components/admin/custom_fields/hierarchy/items_component.html.erb
+++ b/app/components/admin/custom_fields/hierarchy/items_component.html.erb
@@ -28,29 +28,41 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<%=
- component_wrapper do
+ component_wrapper(tag: 'turbo-frame') do
flex_layout do |container|
-
- container.with_row(mb: 3) do
- render(Primer::Beta::BorderBox.new) do |items|
- items.with_header { @custom_field.name }
- items.with_row { "hello" }
- items.with_footer do
- render Admin::CustomFields::Hierarchy::NewItemFormComponent.new(custom_field: @custom_field)
+ if items.empty? && !show_new_item_form?
+ container.with_row(mb: 3) do
+ render Primer::Beta::Blankslate.new(border: true) do |component|
+ component.with_visual_icon(icon: "list-ordered")
+ component.with_heading(tag: :h3).with_content(I18n.t("custom_fields.admin.items.blankslate.title"))
+ component.with_description { I18n.t("custom_fields.admin.items.blankslate.description") }
end
end
- end
+ else
+ container.with_row(mb: 3) do
+ render(Primer::Beta::BorderBox.new) do |item_box|
+ item_box.with_header { @custom_field.name }
- container.with_row(mb: 3) do
- render Primer::Beta::Blankslate.new(border: true) do |component|
- component.with_visual_icon(icon: "list-ordered")
- component.with_heading(tag: :h3).with_content(I18n.t("custom_fields.admin.items.blankslate.title"))
- component.with_description { I18n.t("custom_fields.admin.items.blankslate.description") }
+ items.each do |item|
+ item_box.with_row { render Admin::CustomFields::Hierarchy::ItemComponent.new(hierarchy_item: item) }
+ end
+
+ if show_new_item_form?
+ item_box.with_footer do
+ render Admin::CustomFields::Hierarchy::NewItemFormComponent.new(custom_field: @custom_field,
+ label: @new_item_form_data[:label],
+ short: @new_item_form_data[:short])
+ end
+ end
+ end
end
end
container.with_row do
- render Primer::Beta::Button.new(scheme: :primary) do |button|
+ render Primer::Beta::Button.new(scheme: :primary,
+ tag: :a,
+ data: { turbo_stream: true },
+ href: new_custom_field_item_path(@custom_field)) do |button|
button.with_leading_visual_icon(icon: :plus)
I18n.t(:label_item)
end
diff --git a/app/components/admin/custom_fields/hierarchy/items_component.rb b/app/components/admin/custom_fields/hierarchy/items_component.rb
index 86709c25f7e..d9874760915 100644
--- a/app/components/admin/custom_fields/hierarchy/items_component.rb
+++ b/app/components/admin/custom_fields/hierarchy/items_component.rb
@@ -33,9 +33,19 @@ module Admin
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(custom_field:)
+ def initialize(custom_field:, new_item_form_data: { show: false })
super
@custom_field = custom_field
+ @new_item_form_data = new_item_form_data
+ end
+
+ def items
+ # TODO: This must be context aware (breadcrumbs)
+ @custom_field.hierarchy_root.children
+ end
+
+ def show_new_item_form?
+ @new_item_form_data[:show] || false
end
end
end
diff --git a/app/components/admin/custom_fields/hierarchy/new_item_form_component.html.erb b/app/components/admin/custom_fields/hierarchy/new_item_form_component.html.erb
index 37b757d471d..289c9eafd63 100644
--- a/app/components/admin/custom_fields/hierarchy/new_item_form_component.html.erb
+++ b/app/components/admin/custom_fields/hierarchy/new_item_form_component.html.erb
@@ -28,39 +28,10 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<%=
- component_wrapper do
- flex_layout do |container|
- container.with_row(mb: 3) do
- primer_form_with(url: "/foo") do |f|
- render_inline_form(f) do |new_item_form|
- new_item_form.group(layout: :horizontal) do |input_group|
- input_group.text_field(
- name: :label,
- label: "Label",
- visually_hide_label: true,
- required: true,
- placeholder: "Item label"
- )
- input_group.text_field(
- name: :short,
- label: "Short",
- visually_hide_label: true,
- full_width: false,
- required: false,
- placeholder: "Short label"
- )
- end
-
- new_item_form.group(layout: :horizontal, align_self: :end) do |button_group|
- button_group.button(name: :cancel, label: I18n.t(:button_cancel), scheme: :default)
- button_group.submit(name: :submit, label: I18n.t(:button_save), scheme: :primary)
- end
- end
- end
- end
- end
+ primer_form_with(
+ url: custom_field_items_path(@custom_field),
+ method: :post,
+ ) do |f|
+ render(CustomFields::Hierarchy::NewItemForm.new(f, custom_field: @custom_field, label: @label, short: @short))
end
%>
-
-
-
diff --git a/app/components/admin/custom_fields/hierarchy/new_item_form_component.rb b/app/components/admin/custom_fields/hierarchy/new_item_form_component.rb
index 98207e42091..835ab8527ef 100644
--- a/app/components/admin/custom_fields/hierarchy/new_item_form_component.rb
+++ b/app/components/admin/custom_fields/hierarchy/new_item_form_component.rb
@@ -31,7 +31,17 @@ module Admin
module Hierarchy
class NewItemFormComponent < ApplicationComponent
include OpTurbo::Streamable
- include OpPrimer::ComponentHelpers
+
+ def initialize(custom_field:, label: nil, short: nil)
+ super
+ @custom_field = custom_field
+ @label = label
+ @short = short
+ end
+
+ def items_path
+ custom_field_items_path(@custom_field)
+ end
end
end
end
diff --git a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb b/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
index 57d5e771480..85fd53023cf 100644
--- a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
@@ -31,6 +31,8 @@
module CustomFields
module Hierarchy
class InsertItemContract < Dry::Validation::Contract
+ config.messages.backend = :i18n
+
params do
required(:parent).filled
required(:label).filled(:string)
@@ -49,7 +51,7 @@ module CustomFields
rule(:label) do
if CustomField::Hierarchy::Item.exists?(parent_id: values[:parent], label: value)
- key.failure("Label must be unique within the same hierarchy level")
+ key.failure(:duplicate)
end
end
end
diff --git a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb b/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
index 9c7aa2c7556..416e71f6e9f 100644
--- a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
+++ b/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
@@ -32,6 +32,8 @@ module Admin
module CustomFields
module Hierarchy
class ItemsController < ApplicationController
+ include OpTurbo::ComponentStream
+
layout "admin"
model_object CustomField
@@ -43,8 +45,43 @@ module Admin
def index; end
+ def new
+ update_via_turbo_stream(component: ItemsComponent.new(custom_field: @custom_field,
+ new_item_form_data: { show: true }))
+ respond_with_turbo_streams
+ end
+
+ def create
+ ::CustomFields::Hierarchy::HierarchicalItemService
+ .new(@custom_field)
+ .insert_item(**item_input)
+ .either(
+ ->(_) { update_via_turbo_stream(component: ItemsComponent.new(custom_field: @custom_field)) },
+ ->(validation_result) { add_errors_to_form(validation_result) }
+ )
+
+ respond_with_turbo_streams
+ end
+
private
+ def item_input
+ input = { parent: @custom_field.hierarchy_root, label: params[:label] }
+ input[:short] = params[:short] unless params[:short].empty?
+
+ input
+ end
+
+ def add_errors_to_form(validation_result)
+ validation_result.errors(full: true).to_h.each do |attribute, errors|
+ @custom_field.errors.add(attribute, errors.join(", "))
+ end
+
+ new_item_form_data = { show: true, label: validation_result[:label], short: validation_result[:short] }
+ update_via_turbo_stream(component: ItemsComponent.new(custom_field: @custom_field, new_item_form_data:),
+ status: :unprocessable_entity)
+ end
+
def find_model_object(object_id = :custom_field_id)
super
@custom_field = @object
diff --git a/app/forms/custom_fields/hierarchy/new_item_form.rb b/app/forms/custom_fields/hierarchy/new_item_form.rb
new file mode 100644
index 00000000000..a9990722dd7
--- /dev/null
+++ b/app/forms/custom_fields/hierarchy/new_item_form.rb
@@ -0,0 +1,83 @@
+#-- 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.
+#++
+
+module CustomFields
+ module Hierarchy
+ class NewItemForm < ApplicationForm
+ form do |new_item_form|
+ new_item_form.group(layout: :horizontal) do |input_group|
+ input_group.text_field(
+ name: :label,
+ label: "Label",
+ value: @label,
+ visually_hide_label: true,
+ required: true,
+ placeholder: I18n.t("custom_fields.admin.items.placeholder.label"),
+ validation_message: validation_message_for(:label)
+ )
+ input_group.text_field(
+ name: :short,
+ label: "Short",
+ value: @short,
+ visually_hide_label: true,
+ full_width: false,
+ required: false,
+ placeholder: I18n.t("custom_fields.admin.items.placeholder.short")
+ )
+ end
+
+ new_item_form.group(layout: :horizontal, align_self: :end) do |button_group|
+ button_group.button(name: :cancel,
+ tag: :a,
+ label: I18n.t(:button_cancel),
+ scheme: :default,
+ data: { turbo_stream: true },
+ href: url_helpers.custom_field_items_path(@custom_field))
+ button_group.submit(name: :submit, label: I18n.t(:button_save), scheme: :primary)
+ end
+ end
+
+ def initialize(custom_field:, label:, short:)
+ super()
+ @custom_field = custom_field
+ @label = label
+ @short = short
+ end
+
+ private
+
+ def validation_message_for(attribute)
+ @custom_field
+ .errors
+ .messages_for(attribute)
+ .to_sentence
+ .presence
+ end
+ end
+ end
+end
diff --git a/app/services/custom_fields/create_service.rb b/app/services/custom_fields/create_service.rb
index 5466f7cd28a..6919f05b247 100644
--- a/app/services/custom_fields/create_service.rb
+++ b/app/services/custom_fields/create_service.rb
@@ -57,13 +57,7 @@ module CustomFields
if cf.is_a?(ProjectCustomField)
add_cf_to_visible_columns(cf)
elsif cf.field_format_hierarchy?
- # TODO: Use persistence service
- CustomField::Hierarchy::Item.create(custom_field: cf,
- label: nil,
- parent: nil,
- short: nil,
- sort_order: 0)
- .save
+ CustomFields::Hierarchy::HierarchicalItemService.new(cf).generate_root
end
call
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0d88e67097f..c10d610584e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -238,6 +238,9 @@ en:
blankslate:
title: "Your list of items is empty"
description: "Start by adding items to the custom field of type hierarchy. Each item can be used to create a hierarchy bellow it. To navigate and create sub-items inside a hierarchy click on the created item."
+ placeholder:
+ label: "Item label"
+ short: "Short name"
notice:
remember_items_and_projects: "Remember to set items and projects in the respective tabs for this custom field."
@@ -269,6 +272,14 @@ en:
concatenation:
single: "or"
+ dry_validation:
+ errors:
+ rules:
+ label:
+ duplicate: "must be unique within the same hierarchy level."
+ rules:
+ label: "Label"
+
global_search:
placeholder: "Search in %{app_title}"
overwritten_tabs:
@@ -1538,7 +1549,7 @@ en:
create_new_page: "Wiki page"
date:
- abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
+ abbr_day_names: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]
abbr_month_names:
[
~,
diff --git a/config/routes.rb b/config/routes.rb
index 0eaec67895d..9df364a18c8 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -180,7 +180,7 @@ Rails.application.routes.draw do
only: :destroy
resources :items,
controller: "/admin/custom_fields/hierarchy/items",
- only: :index
+ only: %i[index new create]
end
end
end