Browse Source

custom stream actions

main
pvincent 1 week ago
parent
commit
78498c2c6a
  1. 5
      app/controllers/edge_controller.rb
  2. 7
      app/helpers/turbo_stream_actions_helper.rb
  3. 23
      app/javascript/application.js
  4. 19
      app/javascript/initializers/confirm.js
  5. 1
      app/javascript/initializers/drive.js
  6. 6
      app/javascript/initializers/index.js
  7. 4
      app/javascript/initializers/stream_actions.js
  8. 6
      app/views/edge/index.html.erb
  9. 12
      config/importmap.rb
  10. 1
      config/routes.rb

5
app/controllers/edge_controller.rb

@ -1,4 +1,7 @@
class EdgeController < ApplicationController class EdgeController < ApplicationController
def index
def index; end
def turbo_edit
render turbo_stream: turbo_stream.console_log(text: 'Hello world server-side')
end end
end end

7
app/helpers/turbo_stream_actions_helper.rb

@ -0,0 +1,7 @@
module TurboStreamActionsHelper
def console_log(text)
turbo_stream_action_tag :console_log, text: text
end
end
Turbo::Streams::TagBuilder.prepend(TurboStreamActionsHelper)

23
app/javascript/application.js

@ -1,23 +1,2 @@
import "@hotwired/turbo-rails"
Turbo.config.drive.progressBarDelay = 300
Turbo.config.forms.confirm = (title) => {
const dialog = document.getElementById("main-dialog")
const dialogTitle = dialog.querySelector('[data-modal=title]')
const previousTitleContent = dialogTitle.textContent
dialogTitle.textContent = title
dialog.showModal()
return new Promise((resolve, _) => {
dialog.addEventListener(
"close",
() => {
dialogTitle.textContent = previousTitleContent
resolve(dialog.returnValue == "confirm")
},
{ once: true },
);
});
}
import "initializers"
import "controllers" import "controllers"

19
app/javascript/initializers/confirm.js

@ -0,0 +1,19 @@
Turbo.config.forms.confirm = (title) => {
const dialog = document.getElementById("main-dialog")
const dialogTitle = dialog.querySelector('[data-modal=title]')
const previousTitleContent = dialogTitle.textContent
dialogTitle.textContent = title
dialog.showModal()
return new Promise((resolve, _) => {
dialog.addEventListener(
"close",
() => {
dialogTitle.textContent = previousTitleContent
resolve(dialog.returnValue == "confirm")
},
{ once: true },
);
});
}

1
app/javascript/initializers/drive.js

@ -0,0 +1 @@
Turbo.config.drive.progressBarDelay = 300

6
app/javascript/initializers/index.js

@ -0,0 +1,6 @@
import "@hotwired/turbo-rails"
import "initializers/drive"
import "initializers/confirm"
import "initializers/stream_actions"

4
app/javascript/initializers/stream_actions.js

@ -0,0 +1,4 @@
Turbo.StreamActions.console_log = function () {
const text = this.getAttribute("text")
console.log(text)
}

6
app/views/edge/index.html.erb

@ -9,3 +9,9 @@
<%= button_to 'Delete', @meeting, method: :delete, class: "btn btn-error", data: { turbo_confirm: 'merci de confirmer'} %> <%= button_to 'Delete', @meeting, method: :delete, class: "btn btn-error", data: { turbo_confirm: 'merci de confirmer'} %>
</li> </li>
</ul> </ul>
<ul class="m-2 flex gap-2">
<li>
<%= button_to 'Turbo', edge_turbo_edit_path, class: "btn btn-accent" %>
</li>
</ul>

12
config/importmap.rb

@ -1,7 +1,9 @@
# Pin npm packages by running ./bin/importmap # Pin npm packages by running ./bin/importmap
pin "application"
pin "@hotwired/turbo-rails", to: "turbo.min.js"
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin_all_from "app/javascript/controllers", under: "controllers"
pin 'application'
pin '@hotwired/turbo-rails', to: 'turbo.min.js'
pin '@hotwired/stimulus', to: 'stimulus.min.js'
pin '@hotwired/stimulus-loading', to: 'stimulus-loading.js'
pin_all_from 'app/javascript/controllers', under: 'controllers'
pin_all_from 'app/javascript/initializers', under: 'initializers'

1
config/routes.rb

@ -1,6 +1,7 @@
Rails.application.routes.draw do Rails.application.routes.draw do
root to: 'edge#index' root to: 'edge#index'
get 'edge/index' get 'edge/index'
post 'edge/turbo_edit'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.

Loading…
Cancel
Save