-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: usage with turbo stream (without remote)
- Loading branch information
Showing
9 changed files
with
96 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module DestroyWithUndoResponse | ||
extend ActiveSupport::Concern | ||
|
||
private | ||
|
||
def destroy_with_undo_response(record:, job_id:, redirect:) | ||
respond_to do |format| | ||
format.html do | ||
flash[:success] = undo_flash_message(klass: record.class, job_id: job_id) | ||
redirect_to redirect | ||
end | ||
format.turbo_stream do | ||
if params[:redirect] | ||
flash[:success] = undo_flash_message(klass: record.class, job_id: job_id) | ||
redirect_to redirect | ||
else | ||
flash.now[:success] = undo_flash_message(klass: record.class, job_id: job_id, inline: true) | ||
render 'undo/destroy', locals: { record: record } | ||
end | ||
end | ||
end | ||
end | ||
|
||
def undo_flash_message(klass:, job_id:, inline: nil) | ||
timeout = defined?(klass::UNDO_TIMEOUT) ? klass::UNDO_TIMEOUT : 10 | ||
|
||
{ | ||
title: "#{klass.model_name.human} was removed", | ||
body: 'You can recover it using the undo action below.', | ||
timeout: timeout, countdown: true, | ||
action: { | ||
url: undo_path(job_id, inline: inline), | ||
method: 'delete', | ||
name: 'Undo' | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// https://jpscaletti.com/p/5/adding-custom-actions-to-hotwire-turbo-streams | ||
|
||
const ExtraActions = { | ||
addclass: function (target, content) { | ||
target.classList.add(content) | ||
}, | ||
|
||
removeclass: function (target, content) { | ||
target.classList.remove(content) | ||
} | ||
} | ||
|
||
document.addEventListener( | ||
"turbo:before-stream-render", | ||
function (event) { | ||
const stream = event.target | ||
const actionFunction = ExtraActions[stream.action] | ||
if (!actionFunction) { | ||
return // A built-in action, ignore | ||
} | ||
const target = getTarget(stream) | ||
const content = getContent(stream) | ||
actionFunction(target, content) | ||
event.preventDefault() | ||
} | ||
) | ||
|
||
function getTarget (stream) { | ||
if (stream.target) { | ||
return stream.ownerDocument?.getElementById(stream.target) | ||
} | ||
throw "target attribute is missing" | ||
} | ||
|
||
function getContent (stream) { | ||
// Quick and dirty method to extract the content of the | ||
// <template> tag. | ||
return stream.innerHTML.trim().slice(10, -11).trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<turbo-stream action="addclass" target="<%= dom_id(record) %>"> | ||
<template>hidden</template> | ||
</turbo-stream> | ||
|
||
<% flash.each do |type, data| %> | ||
<%= turbo_stream.prepend('notifications', render(NotificationComponent.new(type: type, data: data))) %> | ||
<% end %> |