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

feat[Op#50737]: Auto retrieve provider form on select change

Kabiru Mwenja113dae482aae
diff --git a/frontend/src/stimulus/controllers/dynamic/storages/select-provider-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/storages/select-provider-form.controller.ts
new file mode 100644
index 00000000000..83447ea62a8
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/storages/select-provider-form.controller.ts
@@ -0,0 +1,58 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 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.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+
+export default class SelectProviderFormController extends Controller {
+ static targets = [
+ 'providerForm',
+ 'providerSelect',
+ ];
+
+ declare readonly providerFormTarget:HTMLFormElement;
+ declare readonly providerSelectTarget:HTMLSelectElement;
+
+ connect():void {
+ // On first load if providerTypeValue is already selected, show the provider form
+ this.fetchProviderTypeForm(this.providerSelectTarget.value);
+ }
+
+ public showProviderForm(evt:Event):void {
+ this.fetchProviderTypeForm((evt.target as HTMLSelectElement).value);
+ }
+
+ fetchProviderTypeForm(providerTypeValue:string):void {
+ if (providerTypeValue === '') {
+ return;
+ }
+
+ this.providerFormTarget.requestSubmit();
+ }
+}
diff --git a/modules/storages/app/components/storages/admin/forms/general_info_form_component.rb b/modules/storages/app/components/storages/admin/forms/general_info_form_component.rb
index c1c63751ee5..499d2db4386 100644
--- a/modules/storages/app/components/storages/admin/forms/general_info_form_component.rb
+++ b/modules/storages/app/components/storages/admin/forms/general_info_form_component.rb
@@ -34,8 +34,7 @@ module Storages::Admin::Forms
alias_method :storage, :model
options form_method: :post,
- submit_button_disabled: false,
- cancel_button_should_break_from_frame: false
+ submit_button_disabled: false
def form_url
options[:form_url] || default_form_url
@@ -46,9 +45,10 @@ module Storages::Admin::Forms
end
def cancel_button_options
- { href: cancel_button_path }.tap do |options_hash|
- options_hash[:target] = '_top' if cancel_button_should_break_from_frame
- end
+ {
+ href: cancel_button_path,
+ data: { turbo_stream: true }
+ }
end
private
@@ -56,14 +56,22 @@ module Storages::Admin::Forms
def default_form_url
case form_method
when :get, :post
- Rails.application.routes.url_helpers.admin_settings_storages_path
+ admin_settings_storages_path
when :patch, :put
- Rails.application.routes.url_helpers.admin_settings_storage_path(storage)
+ admin_settings_storage_path(storage)
end
end
def cancel_button_path
- options[:cancel_button_path] || Rails.application.routes.url_helpers.admin_settings_storages_path
+ options.fetch(:cancel_button_path) do
+ if storage.new_record?
+ new_admin_settings_storage_path
+ elsif storage.persisted?
+ edit_admin_settings_storage_path(storage)
+ else
+ admin_settings_storages_path
+ end
+ end
end
end
end
diff --git a/modules/storages/app/components/storages/admin/select_storage_provider_component.html.erb b/modules/storages/app/components/storages/admin/select_storage_provider_component.html.erb
index 9cc8e8557f4..ebe760e5d2c 100644
--- a/modules/storages/app/components/storages/admin/select_storage_provider_component.html.erb
+++ b/modules/storages/app/components/storages/admin/select_storage_provider_component.html.erb
@@ -20,22 +20,23 @@
model: storage,
url: select_provider_admin_settings_storages_path,
data: {
- storages__select_provider_form_provider_type_value: storage.provider_type,
storages__select_provider_form_target: 'providerForm',
}
) do |form|
flex_layout do |general_info_row|
general_info_row.with_row(mb: 3) do
- render(
- Storages::Admin::ProviderTypeSelectForm.new(
- form,
- storage:,
- select_list_options: {
- # FIXME: Auto submit on provider select
- # data: { action: 'change->storages--select-provider-form#showProviderForm' },
- include_blank: I18n.t('storages.label_select_provider'),
- }
- )
+ render(Storages::Admin::ProviderTypeSelectForm.new(
+ form,
+ storage:,
+ select_list_options: {
+ data: {
+ action: 'change->storages--select-provider-form#showProviderForm',
+ storages__select_provider_form_target: 'providerSelect'
+ },
+ include_blank: I18n.t('storages.label_select_provider')
+ }
+ )
+
)
end
@@ -45,8 +46,7 @@
form,
storage:,
submit_button_options: {
- # FIXME: change to disabled: true, should Auto submit on provider select
- disabled: false,
+ disabled: true,
test_selector: 'storage-select-provider-submit-button'
},
cancel_button_options: {
diff --git a/modules/storages/app/components/storages/admin/storage_view_component.html.erb b/modules/storages/app/components/storages/admin/storage_view_component.html.erb
index 7a71b34d5cf..6b3b34319da 100644
--- a/modules/storages/app/components/storages/admin/storage_view_component.html.erb
+++ b/modules/storages/app/components/storages/admin/storage_view_component.html.erb
@@ -7,12 +7,7 @@
component.with_row(scheme: :default) do
render(OpTurbo::FrameComponent.new(id: :storage_general_info_section)) do
if storage.new_record?
- render(
- Storages::Admin::Forms::GeneralInfoFormComponent.new(
- storage,
- cancel_button_should_break_from_frame: true # Return to index page on Cancel
- )
- )
+ render(Storages::Admin::Forms::GeneralInfoFormComponent.new(storage))
else
render(Storages::Admin::GeneralInfoComponent.new(storage))
end
diff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rb
index 6cab7d6845c..de1d7532529 100644
--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb
@@ -72,6 +72,11 @@ class Storages::Admin::StoragesController < ApplicationController
contract_class: EmptyContract)
.call
.result
+
+ respond_to do |format|
+ format.html
+ format.turbo_stream
+ end
end
def select_provider
diff --git a/modules/storages/app/forms/storages/admin/provider_type_select_form.rb b/modules/storages/app/forms/storages/admin/provider_type_select_form.rb
index 37e80c9a2e0..1f9a9f489a9 100644
--- a/modules/storages/app/forms/storages/admin/provider_type_select_form.rb
+++ b/modules/storages/app/forms/storages/admin/provider_type_select_form.rb
@@ -30,7 +30,7 @@ module Storages::Admin
class ProviderTypeSelectForm < ApplicationForm
form do |storage_form|
storage_form.select_list(**@select_list_options) do |storage_provider_list|
- if @storage.persisted?
+ if @storage.provider_type.present?
storage_provider_list.option(
label: I18n.t("storages.provider_types.#{@storage.short_provider_type}.name"),
value: @storage.provider_type
@@ -60,7 +60,7 @@ module Storages::Admin
label: I18n.t('activerecord.attributes.storages/storage.provider_type'),
caption: I18n.t('storages.instructions.provider_type',
type_link_text: I18n.t('storages.instructions.type_link_text')),
- include_blank: storage.new_record?,
+ include_blank: false,
required: true,
disabled: storage.persisted?
}
diff --git a/modules/storages/app/views/storages/admin/storages/new.turbo_stream.erb b/modules/storages/app/views/storages/admin/storages/new.turbo_stream.erb
new file mode 100644
index 00000000000..036c0a4897c
--- /dev/null
+++ b/modules/storages/app/views/storages/admin/storages/new.turbo_stream.erb
@@ -0,0 +1,34 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2023 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.
+
+++#%>
+
+<%= turbo_stream.replace :select_storage_provider do %>
+ <%=
+ render(::Storages::Admin::SelectStorageProviderComponent.new(@storage))
+ %>
+<% end %>
diff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rb
index 37fa86bb4cb..f924f42117a 100644
--- a/modules/storages/spec/features/admin_storages_spec.rb
+++ b/modules/storages/spec/features/admin_storages_spec.rb
@@ -100,14 +100,16 @@ RSpec.describe 'Admin storages',
aggregate_failures 'Select provider view' do
# General information
expect(page).to have_select('storages_storage[provider_type]', with_options: %w[Nextcloud OneDrive/SharePoint])
+ expect(find_test_selector('storage-select-provider-submit-button')).to be_disabled
# Select Nextcloud
select('Nextcloud', from: 'storages_storage[provider_type]')
- click_button('Save and continue')
# OAuth application
- expect(page).to have_test_selector('storage-openproject-oauth-label', text: 'OpenProject OAuth')
- expect(page).to have_test_selector('label-openproject_oauth_application_configured-status', text: 'Incomplete')
+ wait_for do
+ expect(page).to have_test_selector('storage-openproject-oauth-label', text: 'OpenProject OAuth')
+ expect(page).to have_test_selector('label-openproject_oauth_application_configured-status', text: 'Incomplete')
+ end
# OAuth client
expect(page).to have_test_selector('storage-oauth-client-label', text: 'Nextcloud OAuth')
@@ -125,7 +127,7 @@ RSpec.describe 'Admin storages',
aggregate_failures 'General information' do
within_test_selector('storage-general-info-form') do
- fill_in 'storages_nextcloud_storage_name', with: 'My Nextcloud'
+ fill_in 'storages_nextcloud_storage_name', with: 'My Nextcloud', fill_options: { clear: :backspace }
click_button 'Save and continue'
expect(page).to have_text("Host is not a valid URL.")
@@ -226,22 +228,24 @@ RSpec.describe 'Admin storages',
aggregate_failures 'Select provider view' do
# General information
expect(page).to have_select('storages_storage[provider_type]', with_options: %w[Nextcloud OneDrive/SharePoint])
+ expect(find_test_selector('storage-select-provider-submit-button')).to be_disabled
# Select OneDrive
select('OneDrive/SharePoint', from: 'storages_storage[provider_type]')
- click_button('Save and continue')
# OAuth client
- expect(page).to have_test_selector('storage-oauth-client-label', text: 'Azure OAuth')
- expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Incomplete')
- expect(page).to have_test_selector('storage-oauth-client-id-description',
- text: "Allow OpenProject to access Azure data using OAuth " \
- "to connect OneDrive/Sharepoint.")
+ wait_for do
+ expect(page).to have_test_selector('storage-oauth-client-label', text: 'Azure OAuth')
+ expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Incomplete')
+ expect(page).to have_test_selector('storage-oauth-client-id-description',
+ text: "Allow OpenProject to access Azure data using OAuth " \
+ "to connect OneDrive/Sharepoint.")
+ end
end
aggregate_failures 'General information' do
within_test_selector('storage-general-info-form') do
- fill_in 'storages_one_drive_storage_name', with: 'My OneDrive'
+ fill_in 'storages_one_drive_storage_name', with: 'My OneDrive', fill_options: { clear: :backspace }
click_button 'Save and continue'
expect(page).to have_text("Drive can't be blank.")