diff --git a/app/components/form_elements/fieldset_component.html.erb b/app/components/form_elements/fieldset_component.html.erb index 2bde8ce..143da70 100644 --- a/app/components/form_elements/fieldset_component.html.erb +++ b/app/components/form_elements/fieldset_component.html.erb @@ -1,4 +1,6 @@ -<%= tag.public_send(@tag, class: "mb-6 last:mb-0") do %> +<%= tag.public_send(@tag, class: "mb-6 last:mb-0", data: { + :'field-name' => @field_name + }) do %> <% if @positioning == :vertical %> "> @@ -9,7 +11,21 @@ <%= @descripton %> <% end %> - <%= content %> + + <%= tag.p class: "flex gap-x-1", data: { + controller: @resettable ? "settings--resettable-field" : nil, + } do %> + <%= content %> + <% if @resettable %> + + Reset + + <% end %> + <% end %> <% elsif @positioning == :horizontal %> diff --git a/app/components/form_elements/fieldset_component.rb b/app/components/form_elements/fieldset_component.rb index 23fad5b..cde51fc 100644 --- a/app/components/form_elements/fieldset_component.rb +++ b/app/components/form_elements/fieldset_component.rb @@ -2,11 +2,15 @@ module FormElements class FieldsetComponent < ViewComponent::Base - def initialize(tag: "li", positioning: :vertical, title:, description: nil) + def initialize(tag: "li", positioning: :vertical, + title:, description: nil, + field_name: nil, resettable: false) @tag = tag @positioning = positioning @title = title @descripton = description + @field_name = field_name + @resettable = resettable end end end diff --git a/app/components/form_elements/fieldset_resettable_setting_component.html.erb b/app/components/form_elements/fieldset_resettable_setting_component.html.erb new file mode 100644 index 0000000..a429715 --- /dev/null +++ b/app/components/form_elements/fieldset_resettable_setting_component.html.erb @@ -0,0 +1,13 @@ +<%= render FormElements::FieldsetComponent.new( + title: @title, + description: @description, + field_name: "setting_#{@key.to_s}", + resettable: @resettable + ) do %> + <%= method("#{@type}_field").call :setting, @key, + value: Setting.public_send(@key), + data: { + :'default-value' => Setting.get_field(@key)[:default] + }, + class: "w-full" %> +<% end %> diff --git a/app/components/form_elements/fieldset_resettable_setting_component.rb b/app/components/form_elements/fieldset_resettable_setting_component.rb new file mode 100644 index 0000000..18a4609 --- /dev/null +++ b/app/components/form_elements/fieldset_resettable_setting_component.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module FormElements + class FieldsetResettableSettingComponent < ViewComponent::Base + def initialize(tag: "li", key:, type: :text, title:, description: nil) + @tag = tag + @positioning = :vertical + @title = title + @descripton = description + @key = key.to_sym + @type = type + @resettable = is_resettable?(@key) + end + + def is_resettable?(key) + default_value = Setting.get_field(key)[:default] + default_value.present? && (default_value != Setting.send(key)) + end + end +end diff --git a/app/javascript/controllers/settings/resettable_field_controller.js b/app/javascript/controllers/settings/resettable_field_controller.js new file mode 100644 index 0000000..608d902 --- /dev/null +++ b/app/javascript/controllers/settings/resettable_field_controller.js @@ -0,0 +1,10 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = [ "resetButton" ] + + resetField () { + const inputEl = this.element.querySelector('input') + inputEl.value = inputEl.dataset.defaultValue + } +} diff --git a/app/models/setting.rb b/app/models/setting.rb index 6f475e9..f32c86f 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -42,13 +42,13 @@ class Setting < RailsSettings::Base # Discourse # - field :discourse_public_url, type: :string, readonly: true, + field :discourse_public_url, type: :string, default: ENV["DISCOURSE_PUBLIC_URL"].presence field :discourse_enabled, type: :boolean, default: (ENV["DISCOURSE_PUBLIC_URL"].present?.to_s || false) - field :discourse_connect_secret, type: :string, readonly: true, + field :discourse_connect_secret, type: :string, default: ENV["DISCOURSE_CONNECT_SECRET"].presence # @@ -58,10 +58,10 @@ class Setting < RailsSettings::Base field :ejabberd_enabled, type: :boolean, default: (ENV["EJABBERD_API_URL"].present?.to_s || false) - field :ejabberd_api_url, type: :string, readonly: true, + field :ejabberd_api_url, type: :string, default: ENV["EJABBERD_API_URL"].presence - field :ejabberd_admin_url, type: :string, readonly: true, + field :ejabberd_admin_url, type: :string, default: ENV["EJABBERD_ADMIN_URL"].presence field :ejabberd_buddy_roster, type: :string, @@ -71,7 +71,7 @@ class Setting < RailsSettings::Base # Gitea # - field :gitea_public_url, type: :string, readonly: true, + field :gitea_public_url, type: :string, default: ENV["GITEA_PUBLIC_URL"].presence field :gitea_enabled, type: :boolean, @@ -81,7 +81,7 @@ class Setting < RailsSettings::Base # Lightning Network # - field :lndhub_api_url, type: :string, readonly: true, + field :lndhub_api_url, type: :string, default: ENV["LNDHUB_API_URL"].presence field :lndhub_enabled, type: :boolean, @@ -90,7 +90,7 @@ class Setting < RailsSettings::Base field :lndhub_admin_enabled, type: :boolean, default: (ENV["LNDHUB_ADMIN_UI"] || false) - field :lndhub_public_key, type: :string, readonly: true, + field :lndhub_public_key, type: :string, default: (ENV["LNDHUB_PUBLIC_KEY"] || "") field :lndhub_keysend_enabled, type: :boolean, @@ -100,7 +100,7 @@ class Setting < RailsSettings::Base # Mastodon # - field :mastodon_public_url, type: :string, readonly: true, + field :mastodon_public_url, type: :string, default: ENV["MASTODON_PUBLIC_URL"].presence field :mastodon_enabled, type: :boolean, @@ -110,7 +110,7 @@ class Setting < RailsSettings::Base # MediaWiki # - field :mediawiki_public_url, type: :string, readonly: true, + field :mediawiki_public_url, type: :string, default: ENV["MEDIAWIKI_PUBLIC_URL"].presence field :mediawiki_enabled, type: :boolean, diff --git a/app/views/admin/settings/services/_discourse.html.erb b/app/views/admin/settings/services/_discourse.html.erb index 6af5525..a4b557f 100644 --- a/app/views/admin/settings/services/_discourse.html.erb +++ b/app/views/admin/settings/services/_discourse.html.erb @@ -8,16 +8,15 @@ 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 %> - <% end %> - <%= render FormElements::FieldsetComponent.new(title: "Connect secret") do %> - <%= f.password_field :discourse_connect_secret, - value: Setting.discourse_connect_secret, - class: "w-full", disabled: true %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :discourse_public_url, + title: "Public URL" + ) %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :discourse_connect_secret, + type: :password, + title: "Connect secret" + ) %> <% end %> <% if Setting.discourse_enabled? %> diff --git a/app/views/admin/settings/services/_ejabberd.html.erb b/app/views/admin/settings/services/_ejabberd.html.erb index 87afb56..0faa4b1 100644 --- a/app/views/admin/settings/services/_ejabberd.html.erb +++ b/app/views/admin/settings/services/_ejabberd.html.erb @@ -8,16 +8,14 @@ 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 %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :ejabberd_api_url, + title: "API URL" + ) %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :ejabberd_admin_url, + title: "Admin URL" + ) %> User default settings @@ -37,23 +35,24 @@ title: "Auto-join default rooms", description: "Automatically join above default rooms in chat clients" ) %> - <%= render FormElements::FieldsetComponent.new( + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :ejabberd_buddy_roster, title: "Contact roster name", description: "Used when exchanging contacts after signup from invitation" - ) do %> - <%= f.text_field :ejabberd_buddy_roster, - value: Setting.ejabberd_buddy_roster, - class: "w-full" %> - <% end %> + ) %> Notifications <%= render FormElements::FieldsetComponent.new( title: "From address", description: "Address (JID) of the account notifications are sent from", + resettable: Setting.get_field(:xmpp_notifications_from_address)[:default] != Setting.xmpp_notifications_from_address ) do %> <%= f.text_field :xmpp_notifications_from_address, value: Setting.xmpp_notifications_from_address, + data: { + :'default-value' => Setting.get_field(:xmpp_notifications_from_address)[:default] + }, class: "w-full" %> <% end %> <% end %> diff --git a/app/views/admin/settings/services/_gitea.html.erb b/app/views/admin/settings/services/_gitea.html.erb index 7c0238d..1e91cb6 100644 --- a/app/views/admin/settings/services/_gitea.html.erb +++ b/app/views/admin/settings/services/_gitea.html.erb @@ -8,10 +8,9 @@ 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 %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :gitea_public_url, + title: "Public URL" + ) %> <% end %> diff --git a/app/views/admin/settings/services/_lndhub.html.erb b/app/views/admin/settings/services/_lndhub.html.erb index e9d3c2e..a68ad37 100644 --- a/app/views/admin/settings/services/_lndhub.html.erb +++ b/app/views/admin/settings/services/_lndhub.html.erb @@ -8,11 +8,10 @@ description: "LNDHub configuration present and wallet features enabled" ) %> <% if Setting.lndhub_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "API URL") do %> - <%= f.text_field :lndhub_api_url, - value: Setting.lndhub_api_url, - class: "w-full", disabled: true %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :lndhub_api_url, + title: "API URL" + ) %> <% end %> <%= render FormElements::FieldsetToggleComponent.new( form: f, @@ -29,10 +28,10 @@ description: "Allow users to receive invoice-less payments to their Lightning Address" ) %> <% 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, - value: Setting.lndhub_public_key, - class: "w-full", disabled: true %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :lndhub_public_key, + title: "Public key", + description: "The public key of the Lightning node used by LNDHub" + ) %> <% end %> diff --git a/app/views/admin/settings/services/_mastodon.html.erb b/app/views/admin/settings/services/_mastodon.html.erb index 1270860..0de87d2 100644 --- a/app/views/admin/settings/services/_mastodon.html.erb +++ b/app/views/admin/settings/services/_mastodon.html.erb @@ -8,10 +8,9 @@ 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 %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :mastodon_public_url, + title: "Public URL" + ) %> <% end %> diff --git a/app/views/admin/settings/services/_mediawiki.html.erb b/app/views/admin/settings/services/_mediawiki.html.erb index 2456f20..2dc0aa7 100644 --- a/app/views/admin/settings/services/_mediawiki.html.erb +++ b/app/views/admin/settings/services/_mediawiki.html.erb @@ -8,10 +8,9 @@ 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 %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :mediawiki_public_url, + title: "Public URL" + ) %> <% end %> diff --git a/app/views/admin/settings/services/_remotestorage.html.erb b/app/views/admin/settings/services/_remotestorage.html.erb index fdfa3f4..42aea32 100644 --- a/app/views/admin/settings/services/_remotestorage.html.erb +++ b/app/views/admin/settings/services/_remotestorage.html.erb @@ -1,4 +1,5 @@ RemoteStorage +Feature currently in development. <%= render FormElements::FieldsetToggleComponent.new( form: f, @@ -8,10 +9,9 @@ description: "RemoteStorage configuration present and features enabled" ) %> <% if Setting.remotestorage_enabled? %> - <%= render FormElements::FieldsetComponent.new(title: "Storage URL") do %> - <%= f.text_field :rs_storage_url, - value: Setting.rs_storage_url, - class: "w-full", disabled: true %> - <% end %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :rs_storage_url, + title: "Storage URL" + ) %> <% end %> diff --git a/spec/features/admin/settings_spec.rb b/spec/features/admin/settings_spec.rb index a522d31..c77d968 100644 --- a/spec/features/admin/settings_spec.rb +++ b/spec/features/admin/settings_spec.rb @@ -30,9 +30,7 @@ RSpec.describe 'Admin/global settings', type: :feature 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) + expect(page).to have_field("API URL", with: "http://xmpp.example.com/api") end scenario "Disable ejabberd integration" do @@ -67,5 +65,16 @@ RSpec.describe 'Admin/global settings', type: :feature do expect(page).to_not have_checked_field("setting[remotestorage_enabled]") expect(page).to_not have_field("Storage URL", disabled: true) end + + scenario "Resettable fields" do + visit admin_settings_services_path(params: { s: "ejabberd" }) + expect(page).to have_field("API URL", with: "http://xmpp.example.com/api") + expect(page).to_not have_css('input#setting_ejabberd_api_url+button') + + Setting.ejabberd_api_url = "http://example.com/foo" + visit admin_settings_services_path(params: { s: "ejabberd" }) + expect(page).to have_field("API URL", with: "http://example.com/foo") + expect(page).to have_css('input#setting_ejabberd_api_url+button') + end end end
"> @@ -9,7 +11,21 @@ <%= @descripton %>
Feature currently in development.