Skip to content

Commit

Permalink
Task completion Stimulus controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jbhannah committed Dec 5, 2024
1 parent 98e1202 commit e290396
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def destroy

private
def task_params
params.require(:task).permit(:description, :notes, :position)
params.require(:task).permit(:description, :notes, :position, :completed)
end

def tasks
Expand Down
13 changes: 13 additions & 0 deletions app/javascript/controllers/task_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller } from "@hotwired/stimulus";
import { patch } from "@rails/request.js";

export default class extends Controller {
async toggle(event) {
event.target.setAttribute("disabled", true);

await patch(this.element.dataset.sortableUpdateUrl, {
body: { task: { completed: event.target.checked } },
responseKind: "turbo-stream",
});
}
}
3 changes: 2 additions & 1 deletion app/views/tasks/_task.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= turbo_frame_tag task, data: { "sortable-update-url": task_path(task) } do %>
<%= turbo_frame_tag task, data: { controller: "task", "sortable-update-url": task_path(task) } do %>
<%= checkbox_tag :completed, checked: task.completed?, data: { action: "task#toggle" } %>
<h1><%= link_to task, task, data: { turbo_frame: :_top } %></h1>
<%= link_to "Edit", edit_task_path(task) %>
<%= button_to "Delete", task_path(task), method: :delete %>
Expand Down

0 comments on commit e290396

Please sign in to comment.