Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= render component("ui/modal").new(
title: t(".title"),
open: false,
id: "confirm"
) do |modal| %>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t(".cancel"), class: "confirm-cancel") %>
</form>
<%= render component("ui/button").new(text: t(".confirm"), id: "confirm-accept", scheme: :danger) %>
<% end %>
<% end %>
25 changes: 25 additions & 0 deletions admin/app/components/solidus_admin/layout/confirm/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# Component wrapper for confirmation dialog to use with rolemodel/turbo-confirm.
#
# The modal is rendered in the layout initially hidden.
# To have it open to confirm user's action, place the "data-turbo-confirm" on the submitter (or any other element that
# supports "data-turbo-confirm" attribute: form, link with "data-turbo-method") with the text you want to have in the
# modal title:
# <form>
# <button type=submit data-turbo-confirm="Are you sure?">Submit</button>
# </form>
#
# <form data-turbo-confirm="Confirm saving">
# </form>
#
# You can add more details in the body of the modal using "data-confirm-details" attribute:
# <button data-turbo-confirm="Are you sure?" data-confirm-details="This cannot be undone.">Submit</button>
#
# To customize "Confirm" button text use "data-confirm-button" attribute:
# <button data-turbo-confirm="This action is not reversible" data-confirm-button="Proceed">Submit</button>
#
# For more details see https://github.com/RoleModel/turbo-confirm.

class SolidusAdmin::Layout::Confirm::Component < SolidusAdmin::BaseComponent
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
cancel: "Cancel"
confirm: "Confirm"
title: "Are you sure?"
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
size: :s,
title: t("spree.delete"),
icon: 'close-line',
"data-controller": "confirm",
"data-confirm-text-value": t("spree.are_you_sure"),
"data-turbo-confirm": t("spree.are_you_sure")
) %>
<% end %>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@
tag: :button,
text: t(".delete"),
scheme: :danger,
"data-action": "click->#{stimulus_id}#confirmDelete",
"data-#{stimulus_id}-message-param": t(".delete_confirmation"),
"data-turbo-confirm": t(".delete_confirmation")
) %>
<% end %>
<% end %>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="min-w-[40rem] max-h-screen pointer-events-auto cursor-auto relative transform overflow-auto rounded-lg bg-white text-left shadow-xl max-w-lg divide-y divide-gray-100">

<header class="flex items-center justify-between p-4 sticky top-0 bg-inherit">
<h3 class="text-xl font-semibold text-gray-900">
<h3 class="text-xl font-semibold text-gray-900 modal-title">
<%= @title %>
</h3>
<form method="dialog">
Expand All @@ -23,9 +23,7 @@
</form>
</header>

<div class="p-4">
<%= content %>
</div>
<div class="p-4 empty:hidden modal-body"><%= content %></div>

<% if actions? %>
<footer class="p-4 flex gap-2 justify-end sticky bottom-0 bg-inherit">
Expand Down
8 changes: 7 additions & 1 deletion admin/app/components/solidus_admin/ui/modal/component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static values = {
openOnConnect: { type: Boolean, default: true }
};

connect() {
this.element.showModal()
if (this.openOnConnectValue) {
this.element.showModal();
}
}
}
4 changes: 2 additions & 2 deletions admin/app/components/solidus_admin/ui/modal/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class SolidusAdmin::UI::Modal::Component < SolidusAdmin::BaseComponent
renders_one :actions

def initialize(title:, close_path: nil, open: false, **attributes)
def initialize(title:, close_path: nil, open: true, **attributes)
@title = title
@close_path = close_path
@attributes = attributes
@attributes[:open] = open
@attributes.merge! stimulus_value(name: "open-on-connect", value: open)
end
end
24 changes: 11 additions & 13 deletions admin/app/components/solidus_admin/ui/table/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class extends Controller {
"batchHeader",
"tableBody",
"selectedRowsCount",
"batchActionButton",
]

static classes = ["selectedRow"]
Expand Down Expand Up @@ -123,19 +124,6 @@ export default class extends Controller {
return this.checkboxTargets.filter((checkbox) => checkbox.checked)
}

confirmAction(event) {
const message = event.params.message
.replace("${count}", this.selectedRows().length)
.replace(
"${resource}",
this.selectedRows().length > 1 ? event.params.resourcePlural : event.params.resourceSingular
)

if (!confirm(message)) {
event.preventDefault()
}
}

render() {
const selectedRows = this.selectedRows()

Expand Down Expand Up @@ -172,5 +160,15 @@ export default class extends Controller {
checkbox.checked = true
else if (selectedRows.length > 0) checkbox.indeterminate = true
})

// Update confirmation text
this.batchActionButtonTargets.forEach((button) => {
button.dataset.confirmDetails = button.dataset.confirmationTemplate
.replace("${count}", selectedRows.length)
.replace(
"${resource}",
this.selectedRows().length > 1 ? button.dataset.resourcePlural : button.dataset.resourceSingular,
);
});
}
}
10 changes: 6 additions & 4 deletions admin/app/components/solidus_admin/ui/table/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ def render_batch_action_button(batch_action)
}

if batch_action.require_confirmation
params["data-action"] = "click->#{stimulus_id}#confirmAction"
params["data-#{stimulus_id}-message-param"] = t(
params["data-turbo-confirm"] = t(".are_you_sure")
params["data-confirmation-template"] = t(
".action_confirmation",
action: batch_action.label.downcase
)
params["data-#{stimulus_id}-resource-singular-param"] = @data.singular_name.downcase
params["data-#{stimulus_id}-resource-plural-param"] = @data.plural_name.downcase
params["data-confirm-button"] = batch_action.label
params["data-resource-singular"] = @data.singular_name.downcase
params["data-resource-plural"] = @data.plural_name.downcase
params.merge! stimulus_target("batchActionButton")
end

render component("ui/button").new(**params)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
en:
are_you_sure: "Are you sure?"
no_resources_found: "No %{resources} found"
rows_selected: 'selected'
select_all: 'Select all'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
text: t('.clear_key'),
scheme: :secondary,
type: :submit,
"data-action": "click->#{stimulus_id}#confirm",
"data-#{stimulus_id}-message-param": t(".confirm_clear_key"),
"data-turbo-confirm": t(".confirm.title"),
"data-confirm-details": t(".confirm.clear.details"),
"data-confirm-button": t(".confirm.clear.button"),
) %>
<% end %>

Expand All @@ -26,8 +27,9 @@
text: t('.regenerate_key'),
scheme: :secondary,
type: :submit,
"data-action": "click->#{stimulus_id}#confirm",
"data-#{stimulus_id}-message-param": t(".confirm_regenerate_key"),
"data-turbo-confirm": t(".confirm.title"),
"data-confirm-details": t(".confirm.regenerate.details"),
"data-confirm-button": t(".confirm.regenerate.button")
) %>
<% end %>
</div>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ en:
clear_key: Clear key
regenerate_key: Regenerate key
hidden: Hidden
confirm_clear_key: Are you sure you want to clear this user's API key? It will invalidate the existing key.
confirm_regenerate_key: Are you sure you want to regenerate this user's API key? It will invalidate the existing key.
confirm:
title: Are you sure?
clear:
details: Are you sure you want to clear this user's API key? It will invalidate the existing key.
button: Clear
regenerate:
details: Are you sure you want to regenerate this user's API key? It will invalidate the existing key.
button: Regenerate
1 change: 1 addition & 0 deletions admin/app/javascript/solidus_admin/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import "@hotwired/turbo-rails"
import "vendor/custom_elements"
import "solidus_admin/controllers"
import "solidus_admin/web_components/solidus_select"
import "solidus_admin/turbo-confirm"

This file was deleted.

16 changes: 16 additions & 0 deletions admin/app/javascript/solidus_admin/rolemodel/turbo-confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import TC from "@rolemodel/turbo-confirm"

TC.start({
animationDuration: 0,
messageSlotSelector: ".modal-title",
contentSlots: {
body: {
contentAttribute: "confirm-details",
slotSelector: ".modal-body"
},
acceptText: {
contentAttribute: "confirm-button",
slotSelector: "#confirm-accept"
}
}
});
18 changes: 18 additions & 0 deletions admin/app/javascript/vendor/@rolemodel--turbo-confirm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions admin/app/views/layouts/solidus_admin/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@
<%= render component("ui/toast").new(text: message, scheme: key.to_sym == :error ? :error : :default) %>
<% end %>
</div>

<%= render component("layout/confirm").new %>
</body>
</html>
3 changes: 3 additions & 0 deletions admin/config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
pin "tom-select", to: "https://ga.jspm.io/npm:[email protected]/dist/esm/tom-select.complete.js"
pin "@orchidjs/sifter", to: "https://ga.jspm.io/npm:@orchidjs/[email protected]/dist/esm/sifter.js"
pin "@orchidjs/unicode-variants", to: "https://ga.jspm.io/npm:@orchidjs/[email protected]/dist/esm/index.js"

pin "@rolemodel/turbo-confirm", to: "vendor/@rolemodel--turbo-confirm.js" # @2.1.1
pin "solidus_admin/turbo-confirm", to: "solidus_admin/rolemodel/turbo-confirm.js"
6 changes: 6 additions & 0 deletions admin/lib/solidus_admin/testing_support/feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def clear_search
find('button[aria-label="Clear"]').click
end
end

def accept_turbo_confirm(title)
yield
dialog = find("dialog", text: title)
within(dialog) { find_button(id: "confirm-accept").click }
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# @component "ui/modal"
class SolidusAdmin::Layout::Confirm::ComponentPreview < ViewComponent::Preview
include SolidusAdmin::Preview

# @param title text
# @param body text
# @param button text
def overview(title: "Are you sure?", body: "You are about to delete something. This cannot be undone.", button: "Confirm")
render_with_template(locals: { title:, body:, button: })
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<section>
<div class="mb-8 text-lg">
<form>
<%= render component("ui/button").new(
type: :submit,
scheme: :secondary,
text: "Summon confirmation modal",
data: {
"turbo-confirm": title,
"confirm-details": body,
"confirm-button": button
}
) %>
</form>
</div>

<%= render current_component.new %>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ def with_text
def with_form
render_with_template
end

def with_actions
render_with_template
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SolidusAdmin::Layout::Confirm::Component, type: :component do
it "renders the overview preview" do
render_preview(:overview)
end
end
Loading