Refactor toggles to work without JS, add specs
This commit is contained in:
parent
ca1221e9f3
commit
fa56d6b772
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
module FormElements
|
module FormElements
|
||||||
class FieldsetComponent < ViewComponent::Base
|
class FieldsetComponent < ViewComponent::Base
|
||||||
def initialize(tag: "div", title:, description: nil)
|
def initialize(tag: "li", title:, description: nil)
|
||||||
@tag = tag
|
@tag = tag
|
||||||
@title = title
|
@title = title
|
||||||
@descripton = description
|
@descripton = description
|
||||||
|
@ -11,13 +11,16 @@
|
|||||||
<%= render FormElements::ToggleComponent.new(
|
<%= render FormElements::ToggleComponent.new(
|
||||||
enabled: @enabled,
|
enabled: @enabled,
|
||||||
input_enabled: @input_enabled,
|
input_enabled: @input_enabled,
|
||||||
|
class_names: @form.present? ? "hidden" : nil,
|
||||||
data: {
|
data: {
|
||||||
:'settings--toggle-target' => "button",
|
:'settings--toggle-target' => "button",
|
||||||
action: "settings--toggle#toggleSwitch"
|
action: "settings--toggle#toggleSwitch"
|
||||||
}) %>
|
}) %>
|
||||||
<% if @form.present? %>
|
<% if @form.present? %>
|
||||||
<%= @form.hidden_field @attribute, value: @enabled.to_s,
|
<%= @form.check_box @attribute, {
|
||||||
data: { :'settings--toggle-target' => "input" } %>
|
checked: @enabled,
|
||||||
|
data: { :'settings--toggle-target' => "checkbox" }
|
||||||
|
}, "true", "false" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<%= button_tag type: "button",
|
<%= button_tag type: "button", name: "toggle", data: @data,
|
||||||
data: @data,
|
|
||||||
role: "switch", aria: { checked: @enabled.to_s },
|
role: "switch", aria: { checked: @enabled.to_s },
|
||||||
disabled: !@input_enabled,
|
disabled: !@input_enabled,
|
||||||
class: "#{ @enabled ? 'bg-blue-600' : 'bg-gray-200' }
|
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
|
relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer
|
||||||
rounded-full border-2 border-transparent transition-colors
|
rounded-full border-2 border-transparent transition-colors
|
||||||
duration-200 ease-in-out focus:outline-none focus:ring-2
|
duration-200 ease-in-out focus:outline-none focus:ring-2
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
module FormElements
|
module FormElements
|
||||||
class ToggleComponent < ViewComponent::Base
|
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
|
@enabled = !!enabled
|
||||||
@input_enabled = input_enabled
|
@input_enabled = input_enabled
|
||||||
@data = data
|
@data = data
|
||||||
|
@class_names = class_names
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
import { Controller } from "@hotwired/stimulus"
|
import { Controller } from "@hotwired/stimulus"
|
||||||
|
|
||||||
export default class extends Controller {
|
export default class extends Controller {
|
||||||
static targets = [ "button", "switch", "input" ]
|
static targets = [ "button", "switch", "checkbox" ]
|
||||||
static values = { switchEnabled: Boolean }
|
static values = { switchEnabled: Boolean }
|
||||||
|
|
||||||
|
connect () {
|
||||||
|
this.buttonTarget.classList.remove("hidden")
|
||||||
|
this.checkboxTarget.classList.add("hidden")
|
||||||
|
}
|
||||||
|
|
||||||
toggleSwitch () {
|
toggleSwitch () {
|
||||||
this.switchEnabledValue = !this.switchEnabledValue
|
this.switchEnabledValue = !this.switchEnabledValue
|
||||||
this.inputTarget.value = this.switchEnabledValue.toString()
|
this.checkboxTarget.checked = this.switchEnabledValue
|
||||||
|
|
||||||
if (this.switchEnabledValue) {
|
if (this.switchEnabledValue) {
|
||||||
this.buttonTarget.setAttribute("aria-checked", "true");
|
this.buttonTarget.setAttribute("aria-checked", "true");
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
title: "Enable Discourse integration",
|
title: "Enable Discourse integration",
|
||||||
description: "Discourse configuration present and features enabled"
|
description: "Discourse configuration present and features enabled"
|
||||||
) %>
|
) %>
|
||||||
</ul>
|
|
||||||
<% if Setting.discourse_enabled? %>
|
<% if Setting.discourse_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
||||||
<%= f.text_field :discourse_public_url,
|
<%= f.text_field :discourse_public_url,
|
||||||
@ -15,3 +14,4 @@
|
|||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
title: "Enable ejabberd integration",
|
title: "Enable ejabberd integration",
|
||||||
description: "ejabberd configuration present and features enabled"
|
description: "ejabberd configuration present and features enabled"
|
||||||
) %>
|
) %>
|
||||||
</ul>
|
|
||||||
<% if Setting.ejabberd_enabled? %>
|
<% if Setting.ejabberd_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "API URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "API URL") do %>
|
||||||
<%= f.text_field :ejabberd_api_url,
|
<%= f.text_field :ejabberd_api_url,
|
||||||
@ -20,3 +19,4 @@
|
|||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
title: "Enable Gitea integration",
|
title: "Enable Gitea integration",
|
||||||
description: "Gitea configuration present and features enabled"
|
description: "Gitea configuration present and features enabled"
|
||||||
) %>
|
) %>
|
||||||
</ul>
|
|
||||||
<% if Setting.gitea_enabled? %>
|
<% if Setting.gitea_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
||||||
<%= f.text_field :gitea_public_url,
|
<%= f.text_field :gitea_public_url,
|
||||||
@ -15,3 +14,4 @@
|
|||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
<% if Setting.lndhub_enabled? %>
|
<% if Setting.lndhub_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "API URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "API URL") do %>
|
||||||
<%= f.text_field :lndhub_api_url,
|
<%= f.text_field :lndhub_api_url,
|
||||||
tag: "li",
|
|
||||||
value: Setting.lndhub_api_url,
|
value: Setting.lndhub_api_url,
|
||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
@ -32,7 +31,6 @@
|
|||||||
<% if Setting.lndhub_keysend_enabled? %>
|
<% if Setting.lndhub_keysend_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "Public key", description: "The public key of the Lightning node used by LNDHub") do %>
|
<%= 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,
|
<%= f.text_field :lndhub_public_key,
|
||||||
tag: "li",
|
|
||||||
value: Setting.lndhub_public_key,
|
value: Setting.lndhub_public_key,
|
||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
title: "Enable Mastodon integration",
|
title: "Enable Mastodon integration",
|
||||||
description: "Mastodon configuration present and features enabled"
|
description: "Mastodon configuration present and features enabled"
|
||||||
) %>
|
) %>
|
||||||
</ul>
|
|
||||||
<% if Setting.mastodon_enabled? %>
|
<% if Setting.mastodon_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
||||||
<%= f.text_field :mastodon_public_url,
|
<%= f.text_field :mastodon_public_url,
|
||||||
@ -15,3 +14,4 @@
|
|||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
title: "Enable MediaWiki integration",
|
title: "Enable MediaWiki integration",
|
||||||
description: "MediaWiki configuration present and features enabled"
|
description: "MediaWiki configuration present and features enabled"
|
||||||
) %>
|
) %>
|
||||||
</ul>
|
|
||||||
<% if Setting.mediawiki_enabled? %>
|
<% if Setting.mediawiki_enabled? %>
|
||||||
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
<%= render FormElements::FieldsetComponent.new(title: "Public URL") do %>
|
||||||
<%= f.text_field :mediawiki_public_url,
|
<%= f.text_field :mediawiki_public_url,
|
||||||
@ -15,3 +14,4 @@
|
|||||||
class: "w-full", disabled: true %>
|
class: "w-full", disabled: true %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
@ -18,4 +18,33 @@ RSpec.describe 'Admin/global settings', type: :feature do
|
|||||||
click_button "Save"
|
click_button "Save"
|
||||||
expect(Setting.reserved_usernames).to eq(['Kosmos', 'Kredits'])
|
expect(Setting.reserved_usernames).to eq(['Kosmos', 'Kredits'])
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user