Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Jan 19, 2025
1 parent 82938e2 commit 37d221b
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ GEM
logger (~> 1.5)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.3.4)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
countries (7.1.0)
unaccent (~> 0.3)
Expand Down Expand Up @@ -370,7 +370,7 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.4)
logger (1.6.5)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -431,7 +431,7 @@ GEM
prettier_print (1.2.1)
prop_initializer (0.2.0)
zeitwerk (>= 2.6.18)
psych (5.2.2)
psych (5.2.3)
date
stringio
public_suffix (6.0.1)
Expand Down Expand Up @@ -492,7 +492,7 @@ GEM
rb-inotify (0.11.1)
ffi (~> 1.0)
rbs (2.8.4)
rdoc (6.10.0)
rdoc (6.11.0)
psych (>= 4.0.0)
redis (5.3.0)
redis-client (>= 0.22.0)
Expand Down
2 changes: 1 addition & 1 deletion app/components/avo/discreet_information_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex gap-2 ml-2 mt-1">
<% items.each do |item| %>
<%= content_tag element_tag(item), **element_attributes(item), class: "flex gap-1 text-xs font-normal text-gray-600 hover:text-gray-900", title: item.tooltip, data: {tippy: :tooltip} do %>
<%= content_tag element_tag(item), **element_attributes(item), class: "flex gap-1 text-xs font-normal text-gray-600 hover:text-gray-900", title: item.tooltip, data: {tippy: :tooltip, **data(item)} do %>
<%= item.label if item.label.present? %> <%= helpers.svg item.icon, class: "text-2xl h-4" %>
<% end %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions app/components/avo/discreet_information_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ def element_attributes(item)
{}
end
end

def data(item) = item.data || {}
end
4 changes: 4 additions & 0 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def avo_close_modal
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID)
end

def avo_turbo_reload
turbo_stream_action_tag :turbo_reload
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/javascript/avo.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Mousetrap.bind('r r r', () => {
// Cpture scroll position
scrollTop = document.scrollingElement.scrollTop

Turbo.visit(window.location.href, { action: 'replace' })
window.StreamActions.turbo_reload()
})

function isMac() {
Expand Down
5 changes: 5 additions & 0 deletions app/javascript/js/custom-stream-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ StreamActions.close_filters_dropdown = function () {
document.querySelector('.filters-dropdown-selector').classList.add('hidden')
}

// Uses Turbo to refresh the page
StreamActions.turbo_reload = function () {
window.Turbo.visit(window.location.href, { action: 'replace' })
}

StreamActions.open_filter = function () {
const id = this.getAttribute('unique-id')
setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<div
data-controller="<%= ["action", @action.get_stimulus_controllers].join(" ") %>"
data-action-no-confirmation-value="<%= @action.no_confirmation %>"
data-action-no-confirmation-value="<%= @action.no_confirmation? %>"
data-action-resource-name-value="<%= @resource.model_key %>"
data-resource-id="<%= params[:id] %>"
class="hidden text-slate-800"
Expand Down
20 changes: 17 additions & 3 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class BaseAction
include Avo::Concerns::HasActionStimulusControllers
include Avo::Concerns::Hydration

DATA_ATTRIBUTES = {turbo_frame: Avo::MODAL_FRAME_ID}

class_attribute :name, default: nil
class_attribute :message
class_attribute :confirm_button_label
Expand Down Expand Up @@ -52,8 +54,8 @@ def to_param
to_s
end

def link_arguments(resource:, arguments: {}, **args)
path = Avo::Services::URIService.parse(resource.record&.persisted? ? resource.record_path : resource.records_path)
def path(resource:, arguments: {}, **args)
Avo::Services::URIService.parse(resource.record&.persisted? ? resource.record_path : resource.records_path)
.append_paths("actions")
.append_query(
**{
Expand All @@ -63,8 +65,10 @@ def link_arguments(resource:, arguments: {}, **args)
}.compact
)
.to_s
end

[path, {turbo_frame: Avo::MODAL_FRAME_ID}]
def link_arguments(resource:, arguments: {}, **args)
[path(resource:, arguments:, **args), DATA_ATTRIBUTES]
end

# Encrypt the arguments so we can pass sensible data as a query param.
Expand Down Expand Up @@ -365,6 +369,16 @@ def disabled?
!enabled?
end

def no_confirmation?
Avo::ExecutionContext.new(
target: no_confirmation,
action: self,
resource: @resource,
view: @view,
arguments:
).handle
end

private

def add_message(body, type = :info)
Expand Down
19 changes: 13 additions & 6 deletions lib/avo/discreet_information.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ def timestamp_item(item)
def parse_payload(item)
return unless item.is_a?(Hash)

args = {
record:,
resource:,
view:
}

DiscreetInformationItem.new(
tooltip: Avo::ExecutionContext.new(target: item[:tooltip], record: record, resource: self, view: view).handle,
icon: Avo::ExecutionContext.new(target: item[:icon], record: record, resource: self, view: view).handle,
url: Avo::ExecutionContext.new(target: item[:url], record: record, resource: self, view: view).handle,
url_target: Avo::ExecutionContext.new(target: item[:url_target], record: record, resource: self, view: view).handle,
label: Avo::ExecutionContext.new(target: item[:label], record: record, resource: self, view: view).handle
tooltip: Avo::ExecutionContext.new(target: item[:tooltip], **args).handle,
icon: Avo::ExecutionContext.new(target: item[:icon], **args).handle,
url: Avo::ExecutionContext.new(target: item[:url], **args).handle,
url_target: Avo::ExecutionContext.new(target: item[:url_target], **args).handle,
data: Avo::ExecutionContext.new(target: item[:data], **args).handle,
label: Avo::ExecutionContext.new(target: item[:label], **args).handle
)
end

DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, :url_target, :label, keyword_init: true) unless defined?(DiscreetInformationItem)
DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, :url_target, :data, :label, keyword_init: true) unless defined?(DiscreetInformationItem)
end
1 change: 1 addition & 0 deletions spec/dummy/app/avo/actions/toggle_published.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Avo::Actions::TogglePublished < Avo::BaseAction
self.message = "Are you sure, sure?"
self.confirm_button_label = "Toggle"
self.cancel_button_label = "Don't toggle yet"
self.no_confirmation = -> { arguments[:no_confirmation] || false }

def fields
field :notify_user, as: :boolean, default: true
Expand Down
13 changes: 11 additions & 2 deletions spec/dummy/app/avo/resources/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ class Avo::Resources::Post < Avo::BaseResource
},
{
label: -> { record.published_at ? "✅" : "🙄" },
url: -> { "https://avohq.io" },
url_target: :_blank
tooltip: -> { "Post is #{record.published_at ? "published" : "draft"}. Click to toggle." },
url: -> {
Avo::Actions::TogglePublished.path(
resource: resource,
arguments: {
records: Array.wrap(record.id),
no_confirmation: true
}
)
},
data: Avo::BaseAction::DATA_ATTRIBUTES,
}
]

Expand Down
4 changes: 0 additions & 4 deletions spec/dummy/app/avo/resources/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class Avo::Resources::Product < Avo::BaseResource
},
:timestamps
]
# self.discreet_information = {
# tooltip: -> { sanitize("Product is <strong>#{record.status}</strong>", tags: %w[strong]) },
# icon: "heroicons/outline/bold"
# }
self.profile_photo = {
source: -> { record.image.attached? ? main_app.url_for(record.image.variant(resize_to_fill: [300, 300])) : nil }
}
Expand Down
40 changes: 31 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1504,9 +1504,9 @@
prettier ">=2.3.0"

"@rails/actioncable@^7.0":
version "7.0.4"
resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-7.0.4.tgz#70a3ca56809f7aaabb80af2f9c01ae51e1a8ed41"
integrity sha512-tz4oM+Zn9CYsvtyicsa/AwzKZKL+ITHWkhiu7x+xF77clh2b4Rm+s6xnOgY/sGDWoFWZmtKsE95hxBPkgQQNnQ==
version "7.2.201"
resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-7.2.201.tgz#bfb3da01b3e2462f5a18f372c52dedd7de76037f"
integrity sha512-wsTdWoZ5EfG5k3t7ORdyQF0ZmDEgN4aVPCanHAiNEwCROqibSZMXXmCbH7IDJUVri4FOeAVwwbPINI7HVHPKBw==

"@rails/activestorage@^6.1.710":
version "6.1.710"
Expand Down Expand Up @@ -5103,8 +5103,16 @@ stimulus-use@^0.50.0:
dependencies:
hotkeys-js ">=3"

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0:
name string-width-cjs
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -5204,8 +5212,14 @@ string.prototype.trimstart@^1.0.8:
define-properties "^1.2.1"
es-object-atoms "^1.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
name strip-ansi-cjs
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -5605,8 +5619,16 @@ which@^2.0.1:
dependencies:
isexe "^2.0.0"

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
name wrap-ansi-cjs
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit 37d221b

Please sign in to comment.