Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload icon #2386

Merged
merged 47 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
34466b0
Added svg image
gabrielgiroe1 Jan 19, 2024
23797b4
Added arrow path rouded square to all components
gabrielgiroe1 Jan 19, 2024
753c210
Refactor image rendering for reload button
gabrielgiroe1 Jan 23, 2024
1a6d8cc
Added button tag
gabrielgiroe1 Jan 23, 2024
ff251e6
Added has many refresh controller
gabrielgiroe1 Jan 23, 2024
610734c
Import has many refresh controller
gabrielgiroe1 Jan 23, 2024
716e823
Update id for has_many_refresh_turbo_frame_id_value
gabrielgiroe1 Jan 24, 2024
d0c5515
Refresh turbo frame for turboFrameIdValue
gabrielgiroe1 Jan 24, 2024
2708773
Update turbo_frame_id_value with turbo_frame
gabrielgiroe1 Jan 24, 2024
25aac67
Get access for turbo_frame inside of panel_component
gabrielgiroe1 Jan 24, 2024
d479ff0
Add reload_buton option on hasBaseField
gabrielgiroe1 Jan 30, 2024
5232a6f
Add concern with reload_icon
gabrielgiroe1 Jan 30, 2024
58a030c
Add concern with reload_icon
gabrielgiroe1 Jan 30, 2024
808a024
Add reload_button field
gabrielgiroe1 Jan 30, 2024
cc7629f
Get value of reload_button
gabrielgiroe1 Jan 30, 2024
cd97e5f
Pass option value to PanelComponent
gabrielgiroe1 Jan 30, 2024
9ae5e8f
Get access to the field
gabrielgiroe1 Jan 30, 2024
1623e9f
Remove has_many_refresh_controller.js
gabrielgiroe1 Jan 30, 2024
42ad96b
Update import of HasManyRefreshController
gabrielgiroe1 Jan 30, 2024
bd34160
Fetch the parent turbo frame
gabrielgiroe1 Jan 30, 2024
66c5847
Change has_many_refresh_controller.js to panel_refresh_controller.js
gabrielgiroe1 Jan 30, 2024
a45dbdf
Use svg helper to load the svg
gabrielgiroe1 Jan 30, 2024
5629a3e
Get access to svg method
gabrielgiroe1 Jan 30, 2024
f79c22f
Pass reload_button to panel component
gabrielgiroe1 Jan 30, 2024
9c4f8bd
Get value of reload_button
gabrielgiroe1 Jan 30, 2024
fb07d17
Remove comment
gabrielgiroe1 Jan 30, 2024
c8269cd
Refactor reload_icon
gabrielgiroe1 Jan 30, 2024
60e900c
Add a space before if
gabrielgiroe1 Jan 31, 2024
45a0fbe
Remove att_reader for reload button
gabrielgiroe1 Jan 31, 2024
0d8b297
Remove attr_accessor for reload button
gabrielgiroe1 Jan 31, 2024
50aa820
Include attr_accesor for reload button inside of concerns
gabrielgiroe1 Jan 31, 2024
1901e73
Fix typo
gabrielgiroe1 Jan 31, 2024
44e2cf4
Refactor reload_button to use Avo::ExecutionContext
gabrielgiroe1 Jan 31, 2024
749bcda
Update reviews field configuration
gabrielgiroe1 Feb 5, 2024
030c3e6
Handle dynamic reloading based on the provided execution context
gabrielgiroe1 Feb 5, 2024
5a43594
Refactor admin check method name
gabrielgiroe1 Feb 5, 2024
b012f9f
Add id to reload button
gabrielgiroe1 Feb 6, 2024
d435f57
Add system test to verify that pressing the reload button updates the…
gabrielgiroe1 Feb 6, 2024
ee48640
Remove id for reload button
gabrielgiroe1 Feb 6, 2024
663767e
Remove extra semicolon from import statement in panel_refresh_control…
gabrielgiroe1 Feb 6, 2024
bfc9854
Updated SVG reference
gabrielgiroe1 Feb 6, 2024
20fd4f7
Delete svg file
gabrielgiroe1 Feb 7, 2024
88e3aa2
Refactor Turbo Frame reload feature test
gabrielgiroe1 Feb 7, 2024
a787e83
Remove alt for svg
gabrielgiroe1 Feb 7, 2024
dce0055
Fix eslint error
gabrielgiroe1 Feb 7, 2024
04188d7
Refactoring panel_refresh_controller.js
gabrielgiroe1 Feb 7, 2024
f26b716
Update app/javascript/js/controllers/fields/panel_refresh_controller.js
adrianthedev Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/components/avo/panel_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<% end %>
<div class="text-2xl tracking-normal font-semibold text-gray-800 truncate items-center flex flex-1" data-target="title">
<span><%= @name %></span>
<% if @reload_button %>
<%= button_tag data: { controller: "panel-refresh", action: "click->panel-refresh#refresh" } do %>
<%= svg "heroicons/outline/arrow-path", class: "ml-2 text-2xl h-6 " %>
<% end %>
<% end %>
</div>
<% if description.present? %>
<div class="text-sm tracking-normal font-medium text-gray-600" data-target="description">
Expand Down
5 changes: 4 additions & 1 deletion app/components/avo/panel_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Avo::PanelComponent < ViewComponent::Base
include Avo::ApplicationHelper

attr_reader :title # deprecating title in favor of name
attr_reader :name
attr_reader :classes
Expand All @@ -15,7 +17,7 @@ class Avo::PanelComponent < ViewComponent::Base
renders_one :footer_tools
renders_one :footer

def initialize(name: nil, description: nil, body_classes: nil, data: {}, display_breadcrumbs: false, index: nil, classes: nil, **args)
def initialize(name: nil, description: nil, body_classes: nil, data: {}, display_breadcrumbs: false, index: nil, classes: nil, reload_button: nil, **args)
# deprecating title in favor of name
@title = args[:title]
@name = name || title
Expand All @@ -25,6 +27,7 @@ def initialize(name: nil, description: nil, body_classes: nil, data: {}, display
@data = data
@display_breadcrumbs = display_breadcrumbs
@index = index
@reload_button = reload_button
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/components/avo/views/resource_index_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**@resource.stimulus_data_attributes
} do %>
<%= render_cards_component %>
<%= render Avo::PanelComponent.new(name: title, description: description, data: { component: 'resources-index' }, display_breadcrumbs: @reflection.blank?) do |c| %>
<%= render Avo::PanelComponent.new(name: title, description: description, data: { component: 'resources-index' }, display_breadcrumbs: @reflection.blank?, reload_button: reload_button) do |c| %>
gabrielgiroe1 marked this conversation as resolved.
Show resolved Hide resolved
<% c.with_tools do %>
<% @resource.render_index_controls(item: singular_resource_name.downcase).each do |control| %>
<%= render_control control %>
Expand Down
4 changes: 4 additions & 0 deletions app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,8 @@ def has_many_view_types?
def dynamic_filters_component_id
@dynamic_filters_component_id ||= "dynamic_filters_component_id_#{SecureRandom.hex(3)}"
end

def reload_button
field&.reload_icon_enabled?
end
end
2 changes: 2 additions & 0 deletions app/javascript/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DashboardCardController from './controllers/dashboard_card_controller'
import DateFieldController from './controllers/fields/date_field_controller'
import EasyMdeController from './controllers/fields/easy_mde_controller'
import FilterController from './controllers/filter_controller'
import PanelRefreshController from './controllers/fields/panel_refresh_controller'
import HiddenInputController from './controllers/hidden_input_controller'
import InputAutofocusController from './controllers/input_autofocus_controller'
import ItemSelectAllController from './controllers/item_select_all_controller'
Expand Down Expand Up @@ -48,6 +49,7 @@ application.register('boolean-filter', BooleanFilterController)
application.register('copy-to-clipboard', CopyToClipboardController)
application.register('dashboard-card', DashboardCardController)
application.register('filter', FilterController)
application.register('panel-refresh', PanelRefreshController)
application.register('hidden-input', HiddenInputController)
application.register('input-autofocus', InputAutofocusController)
application.register('item-select-all', ItemSelectAllController)
Expand Down
15 changes: 15 additions & 0 deletions app/javascript/js/controllers/fields/panel_refresh_controller.js
gabrielgiroe1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
refresh() {
const frame = this.context.scope.element.closest('turbo-frame')
this.element.querySelector('svg').classList.add('animate-spin')
if (frame) {
frame.reload()
} else {
console.error(
`Element with ID '${this.turboFrameIdValue}' not found.`,
)
}
}
}
17 changes: 17 additions & 0 deletions lib/avo/fields/concerns/reload_icon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Avo
module Fields
module Concerns
module ReloadIcon
extend ActiveSupport::Concern

included do
attr_accessor :reload_button
end

def reload_icon_enabled?
Avo::ExecutionContext.new(target: @reload_button).handle
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/avo/fields/has_base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Fields
class HasBaseField < BaseField
include Avo::Fields::Concerns::IsSearchable
include Avo::Fields::Concerns::UseResource
include Avo::Fields::Concerns::ReloadIcon

attr_accessor :display
attr_accessor :scope
Expand All @@ -14,7 +15,6 @@ class HasBaseField < BaseField

def initialize(id, **args, &block)
super(id, **args, &block)

@scope = args[:scope].present? ? args[:scope] : nil
@attach_scope = args[:attach_scope].present? ? args[:attach_scope] : nil
@display = args[:display].present? ? args[:display] : :show
Expand All @@ -24,6 +24,7 @@ def initialize(id, **args, &block)
@use_resource = args[:use_resource] || nil
@discreet_pagination = args[:discreet_pagination] || false
@link_to_child_resource = args[:link_to_child_resource] || false
@reload_button = args[:reload_button].present? ? args[:reload_button] : false
end

def field_resource
Expand Down
7 changes: 5 additions & 2 deletions spec/dummy/app/avo/resources/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ def fields

field :admin, as: :has_one
field :team_members, as: :has_many, through: :memberships, translation_key: "avo.resource_translations.team_members"
field :reviews, as: :has_many
field :reviews, as: :has_many,
reload_button: -> {
current_user.is_admin?
}

if params[:show_location_field] == '1'
if params[:show_location_field] == "1"
# Example for error message when resource is missing
field :locations, as: :has_many
end
Expand Down
25 changes: 25 additions & 0 deletions spec/system/avo/reload_button_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rails_helper"

RSpec.describe "Reload button", type: :system do
describe "reload button" do
let!(:user) { create :user, id: 39 }
let!(:team) { create :team, id: 4 }
let!(:review) { create :review, reviewable: team, user: user, body: "Initial review body" }
context "Teams" do
it "press the reload button and it will refresh the Turbo-Frame", js: true do
visit "/admin/resources/teams/4"
scroll_to find('turbo-frame[id="has_many_field_show_reviews"]')

expect(page).to have_content("Initial review body")

review.update(body: "Updated review body")

expect(page).to have_content("Initial review body")

find("button[data-controller='panel-refresh']").click

expect(page).to have_content("Updated review body")
end
end
end
end