Basic turbo todo list implementation

Based on:

https://webcrunch.com/posts/digging-into-turbo-with-ruby-on-rails-7
This commit is contained in:
2024-07-28 23:53:35 +02:00
parent 84e6e43812
commit ea5d597fc7
8 changed files with 72 additions and 66 deletions

View File

@@ -1,4 +1,4 @@
<%= form_with(model: todo, class: "contents") do |form| %>
<%= form_with(model: todo, class: "contents", id: "#{dom_id(todo)}_form") do |form| %>
<% if todo.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(todo.errors.count, "error") %> prohibited this todo from being saved:</h2>
@@ -11,17 +11,14 @@
</div>
<% end %>
<div class="my-5">
<%= form.label :title %>
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="mb-6">
<%= form.label :title, class: "sr-only" %>
<div class="relative">
<%= form.text_field :title, class: "block shadow-sm rounded-full border border-gray-200 outline-none px-6 py-4 w-full focus:border-sky-300 focus:outline-none focus:ring-4 focus:ring-sky-50 text-lg placeholder:text-gray-400", placeholder: "Add a new todo" %>
<div class="my-5">
<%= form.label :status %>
<%= form.number_field :status, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
<div class="absolute top-0 right-0">
<%= form.submit class: "mt-px rounded-full py-4 px-5 bg-sky-500 text-white font-medium cursor-pointer border-2 border-sky-500 hover:bg-sky-600 hover:border-sky-600" %>
</div>
</div>
</div>
<% end %>

View File

@@ -1,12 +1,13 @@
<div id="<%= dom_id todo %>">
<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= todo.title %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Status:</strong>
<%= todo.status %>
</p>
</div>
<li id="<%= "#{dom_id(todo)}" %>" class="py-3">
<%= turbo_frame_tag dom_id(todo) do %>
<div class="flex items-center justify-center">
<%= link_to todo.title, edit_todo_path(todo), class: "flex-1 #{"line-through opacity-50" if todo.complete?}" %>
<% if todo.complete? %>
<%= button_to "Mark incomplete", todo_path(todo, todo: { status: 'incomplete'}), method: :patch, class: "bg-gray-50 px-3 py-2 rounded inline-flex items-center justify-center text-gray-600" %>
<% else %>
<%= button_to "Mark complete", todo_path(todo, todo: { status: 'complete'}), method: :patch, class: "bg-green-50 px-3 py-2 rounded inline-flex items-center justify-center text-green-600" %>
<% end %>
<%= button_to "Delete", todo_path(todo), method: :delete, class: "bg-red-50 px-3 py-2 rounded inline-flex items-center justify-center text-red-600" %>
</div>
<% end %>
</li>

View File

@@ -0,0 +1,7 @@
<%= turbo_stream.prepend "todos" do %>
<%= render "todo", todo: @todo %>
<% end %>
<%= turbo_stream.replace "#{dom_id(Todo.new)}_form" do %>
<%= render "form", todo: Todo.new %>
<% end %>

View File

@@ -1,8 +1,3 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing todo</h1>
<%= turbo_frame_tag dom_id(@todo) do %>
<%= render "form", todo: @todo %>
<%= link_to "Show this todo", @todo, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to todos", todos_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
<% end %>

View File

@@ -1,21 +1,16 @@
<div class="w-full">
<div class="mx-auto max-w-3xl mt-4 sm:mt-24">
<div class="mb-6">
<h1 class="font-bold text-4xl mb-6">Todos</h1>
</div>
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Todos" %>
<%= render "form", todo: Todo.new %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Todos</h1>
<%= link_to "New todo", new_todo_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="todos" class="min-w-full">
<% @todos.each do |todo| %>
<%= render todo %>
<p>
<%= link_to "Show this todo", todo, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
<%= turbo_stream_from "todos" %>
<ul id="todos" class="divide-y list-none">
<%= render @todos %>
</ul>
</div>