diff --git a/app/components/form_elements/fieldset_component.rb b/app/components/form_elements/fieldset_component.rb index 04871cf..8896137 100644 --- a/app/components/form_elements/fieldset_component.rb +++ b/app/components/form_elements/fieldset_component.rb @@ -2,7 +2,7 @@ module FormElements class FieldsetComponent < ViewComponent::Base - def initialize(tag: "div", title:, description: nil) + def initialize(tag: "li", title:, description: nil) @tag = tag @title = title @descripton = description diff --git a/app/components/form_elements/fieldset_toggle_component.html.erb b/app/components/form_elements/fieldset_toggle_component.html.erb index e120f9d..504a5b5 100644 --- a/app/components/form_elements/fieldset_toggle_component.html.erb +++ b/app/components/form_elements/fieldset_toggle_component.html.erb @@ -11,13 +11,16 @@ <%= render FormElements::ToggleComponent.new( enabled: @enabled, input_enabled: @input_enabled, + class_names: @form.present? ? "hidden" : nil, data: { :'settings--toggle-target' => "button", action: "settings--toggle#toggleSwitch" }) %> <% if @form.present? %> - <%= @form.hidden_field @attribute, value: @enabled.to_s, - data: { :'settings--toggle-target' => "input" } %> + <%= @form.check_box @attribute, { + checked: @enabled, + data: { :'settings--toggle-target' => "checkbox" } + }, "true", "false" %> <% end %> <% end %> diff --git a/app/components/form_elements/toggle_component.html.erb b/app/components/form_elements/toggle_component.html.erb index 14f0a81..41a6708 100644 --- a/app/components/form_elements/toggle_component.html.erb +++ b/app/components/form_elements/toggle_component.html.erb @@ -1,8 +1,8 @@ -<%= button_tag type: "button", - data: @data, +<%= button_tag type: "button", name: "toggle", data: @data, role: "switch", aria: { checked: @enabled.to_s }, disabled: !@input_enabled, class: "#{ @enabled ? 'bg-blue-600' : 'bg-gray-200' } + #{ @class_names.present? ? @class_names : '' } relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 diff --git a/app/components/form_elements/toggle_component.rb b/app/components/form_elements/toggle_component.rb index d5ee036..d2f5eae 100644 --- a/app/components/form_elements/toggle_component.rb +++ b/app/components/form_elements/toggle_component.rb @@ -2,10 +2,11 @@ module FormElements class ToggleComponent < ViewComponent::Base - def initialize(enabled:, input_enabled: true, data: nil) + def initialize(enabled:, input_enabled: true, data: nil, class_names: nil) @enabled = !!enabled @input_enabled = input_enabled @data = data + @class_names = class_names end end end diff --git a/app/javascript/controllers/settings/toggle_controller.js b/app/javascript/controllers/settings/toggle_controller.js index 5c9cff1..587a4e0 100644 --- a/app/javascript/controllers/settings/toggle_controller.js +++ b/app/javascript/controllers/settings/toggle_controller.js @@ -1,12 +1,17 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { - static targets = [ "button", "switch", "input" ] + static targets = [ "button", "switch", "checkbox" ] static values = { switchEnabled: Boolean } + connect () { + this.buttonTarget.classList.remove("hidden") + this.checkboxTarget.classList.add("hidden") + } + toggleSwitch () { this.switchEnabledValue = !this.switchEnabledValue - this.inputTarget.value = this.switchEnabledValue.toString() + this.checkboxTarget.checked = this.switchEnabledValue if (this.switchEnabledValue) { this.buttonTarget.setAttribute("aria-checked", "true"); diff --git a/app/views/admin/settings/services/_discourse.html.erb b/app/views/admin/settings/services/_discourse.html.erb index 633fa7b..498dd5f 100644 --- a/app/views/admin/settings/services/_discourse.html.erb +++ b/app/views/admin/settings/services/_discourse.html.erb @@ -7,11 +7,11 @@ title: "Enable Discourse integration", description: "Discourse configuration present and features enabled" ) %> - -<% if Setting.discourse_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> - <%= f.text_field :discourse_public_url, - value: Setting.discourse_public_url, - class: "w-full", disabled: true %> + <% if Setting.discourse_enabled? %> + <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> + <%= f.text_field :discourse_public_url, + value: Setting.discourse_public_url, + class: "w-full", disabled: true %> + <% end %> <% end %> -<% end %> + diff --git a/app/views/admin/settings/services/_ejabberd.html.erb b/app/views/admin/settings/services/_ejabberd.html.erb index 93c5ff3..16dfc58 100644 --- a/app/views/admin/settings/services/_ejabberd.html.erb +++ b/app/views/admin/settings/services/_ejabberd.html.erb @@ -7,16 +7,16 @@ title: "Enable ejabberd integration", description: "ejabberd configuration present and features enabled" ) %> + <% if Setting.ejabberd_enabled? %> + <%= render FormElements::FieldsetComponent.new(title: "API URL") do %> + <%= f.text_field :ejabberd_api_url, + value: Setting.ejabberd_api_url, + class: "w-full", disabled: true %> + <% end %> + <%= render FormElements::FieldsetComponent.new(title: "Admin URL") do %> + <%= f.text_field :ejabberd_admin_url, + value: Setting.ejabberd_admin_url, + class: "w-full", disabled: true %> + <% end %> + <% end %> -<% if Setting.ejabberd_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "API URL") do %> - <%= f.text_field :ejabberd_api_url, - value: Setting.ejabberd_api_url, - class: "w-full", disabled: true %> - <% end %> - <%= render FormElements::FieldsetComponent.new(title: "Admin URL") do %> - <%= f.text_field :ejabberd_admin_url, - value: Setting.ejabberd_admin_url, - class: "w-full", disabled: true %> - <% end %> -<% end %> diff --git a/app/views/admin/settings/services/_gitea.html.erb b/app/views/admin/settings/services/_gitea.html.erb index 964185e..7c0238d 100644 --- a/app/views/admin/settings/services/_gitea.html.erb +++ b/app/views/admin/settings/services/_gitea.html.erb @@ -7,11 +7,11 @@ title: "Enable Gitea integration", description: "Gitea configuration present and features enabled" ) %> - -<% if Setting.gitea_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> - <%= f.text_field :gitea_public_url, - value: Setting.gitea_public_url, - class: "w-full", disabled: true %> + <% if Setting.gitea_enabled? %> + <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> + <%= f.text_field :gitea_public_url, + value: Setting.gitea_public_url, + class: "w-full", disabled: true %> + <% end %> <% end %> -<% end %> + diff --git a/app/views/admin/settings/services/_lndhub.html.erb b/app/views/admin/settings/services/_lndhub.html.erb index f65d0c3..e9d3c2e 100644 --- a/app/views/admin/settings/services/_lndhub.html.erb +++ b/app/views/admin/settings/services/_lndhub.html.erb @@ -10,7 +10,6 @@ <% if Setting.lndhub_enabled? %> <%= render FormElements::FieldsetComponent.new(title: "API URL") do %> <%= f.text_field :lndhub_api_url, - tag: "li", value: Setting.lndhub_api_url, class: "w-full", disabled: true %> <% end %> @@ -32,7 +31,6 @@ <% if Setting.lndhub_keysend_enabled? %> <%= render FormElements::FieldsetComponent.new(title: "Public key", description: "The public key of the Lightning node used by LNDHub") do %> <%= f.text_field :lndhub_public_key, - tag: "li", value: Setting.lndhub_public_key, class: "w-full", disabled: true %> <% end %> diff --git a/app/views/admin/settings/services/_mastodon.html.erb b/app/views/admin/settings/services/_mastodon.html.erb index d9dfdeb..1270860 100644 --- a/app/views/admin/settings/services/_mastodon.html.erb +++ b/app/views/admin/settings/services/_mastodon.html.erb @@ -7,11 +7,11 @@ title: "Enable Mastodon integration", description: "Mastodon configuration present and features enabled" ) %> - -<% if Setting.mastodon_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> - <%= f.text_field :mastodon_public_url, - value: Setting.mastodon_public_url, - class: "w-full", disabled: true %> + <% if Setting.mastodon_enabled? %> + <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> + <%= f.text_field :mastodon_public_url, + value: Setting.mastodon_public_url, + class: "w-full", disabled: true %> + <% end %> <% end %> -<% end %> + diff --git a/app/views/admin/settings/services/_mediawiki.html.erb b/app/views/admin/settings/services/_mediawiki.html.erb index cc032c1..2456f20 100644 --- a/app/views/admin/settings/services/_mediawiki.html.erb +++ b/app/views/admin/settings/services/_mediawiki.html.erb @@ -7,11 +7,11 @@ title: "Enable MediaWiki integration", description: "MediaWiki configuration present and features enabled" ) %> - -<% if Setting.mediawiki_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> - <%= f.text_field :mediawiki_public_url, - value: Setting.mediawiki_public_url, - class: "w-full", disabled: true %> + <% if Setting.mediawiki_enabled? %> + <%= render FormElements::FieldsetComponent.new(title: "Public URL") do %> + <%= f.text_field :mediawiki_public_url, + value: Setting.mediawiki_public_url, + class: "w-full", disabled: true %> + <% end %> <% end %> -<% end %> + diff --git a/spec/features/admin/settings_spec.rb b/spec/features/admin/settings_spec.rb index bd586c7..7a394ba 100644 --- a/spec/features/admin/settings_spec.rb +++ b/spec/features/admin/settings_spec.rb @@ -18,4 +18,33 @@ RSpec.describe 'Admin/global settings', type: :feature do click_button "Save" expect(Setting.reserved_usernames).to eq(['Kosmos', 'Kredits']) end + + describe "Service settings" do + scenario "Opening service settings shows page for first service" do + visit admin_settings_services_path + + expect(current_url).to eq(admin_settings_services_url(params: { s: "discourse" })) + end + + scenario "View ejabberd settings" do + visit admin_settings_services_path(params: { s: "ejabberd" }) + + expect(page).to have_content("Enable ejabberd integration") + expect(page).to have_field("API URL", + with: "http://xmpp.example.com/api", + disabled: true) + end + + scenario "Disable ejabberd integration" do + visit admin_settings_services_path(params: { s: "ejabberd" }) + expect(page).to have_checked_field("setting[ejabberd_enabled]") + + uncheck "setting[ejabberd_enabled]" + click_button "Save" + + expect(current_url).to eq(admin_settings_services_url(params: { s: "ejabberd" })) + expect(page).to_not have_checked_field("setting[ejabberd_enabled]") + expect(page).to_not have_field("API URL", disabled: true) + end + end end