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

Feature: Add optional confirmation on save #2965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c34ba1d
naive solution to add single confirmation option to saving
G-Simpson Jul 11, 2024
80d3266
removing erroneous tinkering
G-Simpson Jul 11, 2024
3b5fa08
Merge branch 'avo-hq:main' into feature/confirmation-message-before-save
G-Simpson Jul 11, 2024
7ca0ed0
adding back resource name
G-Simpson Jul 11, 2024
782fabe
refactoring
G-Simpson Jul 11, 2024
19fffe2
adding basic tests
G-Simpson Jul 11, 2024
c25cd26
adding trailing newline to editor
G-Simpson Jul 11, 2024
36457bf
upgraded loading_button_controller to now reset button on cancel, cha…
G-Simpson Jul 12, 2024
edf029b
updating tests for rubocop rules and DRY, insuring no memory leak on …
G-Simpson Jul 12, 2024
b0a1824
prefer double quotes
G-Simpson Jul 12, 2024
d666ea4
syntax errors
G-Simpson Jul 12, 2024
7d6aa41
moving attribute
G-Simpson Jul 13, 2024
3c60924
Merge branch 'main' into feature/confirmation-message-before-save
G-Simpson Jul 13, 2024
7fdcc0c
Merge branch 'avo-hq:main' into feature/confirmation-message-before-save
G-Simpson Jul 17, 2024
331b639
moving confirm_on_save to button initialiser
G-Simpson Jul 17, 2024
c2e5be8
Merge branch 'feature/confirmation-message-before-save' of github.com…
G-Simpson Jul 17, 2024
c7f62cb
Update app/javascript/js/controllers/loading_button_controller.js
G-Simpson Jul 17, 2024
c8fd98f
updating loading controller and snipper logic, removing unneccesary b…
G-Simpson Jul 18, 2024
a21c988
Merge branch 'main' into feature/confirmation-message-before-save
G-Simpson Jul 20, 2024
32c26c3
fixing broken conflict
G-Simpson Jul 20, 2024
b6f1a75
reverting
G-Simpson Jul 20, 2024
5ed02aa
yikes
G-Simpson Jul 20, 2024
a98762f
adding confirm_on_save back in
G-Simpson Jul 20, 2024
6786c76
Merge branch 'main' into feature/confirmation-message-before-save
Paul-Bob Jul 29, 2024
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
5 changes: 0 additions & 5 deletions app/components/avo/button_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ def initialize(path = nil, size: :md, style: :outline, color: :gray, icon: nil,
def args
if @args[:loading]
@args[:"data-controller"] = "loading-button"
@args[:"data-loading-button-confirmed-value"] = false
@args[:"data-action"] = "click->loading-button#attemptSubmit"

if @args[:confirm]
@args[:"data-loading-button-confirmation-message-value"] = @args.delete(:confirm)
end
end

@args[:class] = button_classes
Expand Down
5 changes: 4 additions & 1 deletion app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def render_save_button(control)
style: :primary,
loading: true,
type: :submit,
icon: "avo/save" do
icon: "avo/save",
data: {
turbo_confirm: @resource.confirm_on_save ? t("avo.are_you_sure") : nil
} do
control.label
end
end
Expand Down
54 changes: 24 additions & 30 deletions app/javascript/js/controllers/loading_button_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
spinnerMarkup = `<div class="button-spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>`;
spinnerMarkup = `
<div class="button-spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>`;

static values = {
confirmationMessage: String,
confirmed: Boolean,
}

attemptSubmit(e) {
// If the user has to confirm the action
if (this.confirmationMessageValue) {
this.confirmAndApply(e)
} else {
this.applyLoader()
}
connect() {
this.button.setAttribute('data-original-content', this.button.innerHTML)
this.dialog = document.getElementById('turbo-confirm')
this.dialogCloseHandler = this.handleDialogClose.bind(this)

return null
this.dialog.addEventListener('close', this.dialogCloseHandler)
}

confirmAndApply(e) {
// Intervene only if not confirmed
if (!this.confirmedValue) {
e.preventDefault()
disconnect() {
this.dialog.removeEventListener('close', this.dialogCloseHandler)
}

if (window.confirm(this.confirmationMessageValue)) {
this.applyLoader()
}
}
attemptSubmit() {
this.applyLoader()
}

get button() {
Expand All @@ -45,19 +40,18 @@ export default class extends Controller {
button.style.height = `${button.getBoundingClientRect().height}px`
button.innerHTML = this.spinnerMarkup
button.classList.add('justify-center')

setTimeout(() => {
this.markConfirmed()
button.click()
button.setAttribute('disabled', 'disabled')
}, 1)
}

markConfirmed() {
this.confirmedValue = true
handleDialogClose() {
if (this.dialog.returnValue !== 'confirm') {
this.resetButton()
}
}

markUnconfirmed() {
this.confirmedValue = false
resetButton() {
const { button } = this

button.innerHTML = button.getAttribute('data-original-content')
button.removeAttribute('disabled');
}
}
1 change: 1 addition & 0 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def current_user
class_attribute :filters_loader
class_attribute :view_types
class_attribute :grid_view
class_attribute :confirm_on_save, default: false
class_attribute :visible_on_sidebar, default: true
class_attribute :index_query, default: -> {
query
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/store.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Avo::Resources::Store < Avo::BaseResource
self.includes = [:location]
self.confirm_on_save = true

def fields
field :id, as: :id
Expand Down
56 changes: 56 additions & 0 deletions spec/features/avo/confirm_on_save_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.feature "ConfirmOnSave", type: :feature do
context "edit" do
let!(:store) { create :store, name: store_name }
let(:store_name) { "original name" }
let(:name_field) { "store[name]" }
let(:changed_name) { "new name" }

describe "when saving the record" do
it "confirms the operation" do
visit "/admin/resources/stores/#{store.id}/edit"

save

expect(page).to have_selector("#turbo-confirm button[value='confirm']")
end

it "completes the operation on confirmation" do
visit "/admin/resources/stores/#{store.id}/edit"

expect(page).to have_field name_field

fill_in "store[name]", with: changed_name

save

accept_custom_alert do
click_on "Yes, I'm sure"
end

expect(store.reload.name).to eq(changed_name)
end

it "does not complete the operation on denial" do
visit "/admin/resources/stores/#{store.id}/edit"

expect(page).to have_field name_field

fill_in "store[name]", with: changed_name

save

accept_custom_alert do
click_on "No, cancel"
end

expect(page).not_to have_selector(".button-spinner")
expect(find_field_value_element("name")).to have_text changed_name
expect(store.name).to eq(store_name)
end
end
end
end
Loading