1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-03 20:45:21 +02:00

Handle correctly turbo_stream request format

This commit is contained in:
Elvis Serrão 2025-01-28 15:16:05 -03:00
parent 62d6efcb93
commit 4087275751
2 changed files with 52 additions and 45 deletions

View file

@ -24,12 +24,13 @@ class CategoriesController < ApplicationController
redirect_target_url = request.referer || categories_path redirect_target_url = request.referer || categories_path
respond_to do |format| respond_to do |format|
format.html { redirect_back_or_to categories_path, notice: t(".success") }
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) } format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }
end end
else else
set_categories set_categories
render :new, status: :unprocessable_entity respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("form", partial: "form", locals: { category: @category, categories: @categories }) }
end
end end
end end
@ -38,10 +39,15 @@ class CategoriesController < ApplicationController
def update def update
if @category.update(category_params) if @category.update(category_params)
flash[:notice] = t(".success")
redirect_back_or_to categories_path, notice: t(".success") respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, categories_path) }
end
else else
render :edit, status: :unprocessable_entity respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("form", partial: "form", locals: { category: @category, categories: @categories }) }
end
end end
end end

View file

@ -1,48 +1,49 @@
<%# locals: (category:, categories:) %> <%# locals: (category:, categories:) %>
<%= turbo_frame_tag 'form' do %>
<div data-controller="color-avatar">
<%= styled_form_with model: category, class: "space-y-4" do |f| %>
<section class="space-y-4">
<div class="w-fit m-auto">
<%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %>
</div>
<div data-controller="color-avatar"> <div class="flex gap-2 items-center justify-center">
<%= styled_form_with model: category, class: "space-y-4", data: { turbo_frame: :_top } do |f| %> <% Category::COLORS.each do |color| %>
<section class="space-y-4"> <label class="relative">
<div class="w-fit m-auto"> <%= f.radio_button :color, color, class: "sr-only peer", data: { action: "change->color-avatar#handleColorChange" } %>
<%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %> <div class="w-6 h-6 rounded-full cursor-pointer peer-checked:ring-2 peer-checked:ring-offset-2 peer-checked:ring-blue-500" style="background-color: <%= color %>"></div>
</div> </label>
<% end %>
</div>
<div class="flex gap-2 items-center justify-center"> <% if category.errors.any? %>
<% Category::COLORS.each do |color| %> <%= render "shared/form_errors", model: category %>
<label class="relative">
<%= f.radio_button :color, color, class: "sr-only peer", data: { action: "change->color-avatar#handleColorChange" } %>
<div class="w-6 h-6 rounded-full cursor-pointer peer-checked:ring-2 peer-checked:ring-offset-2 peer-checked:ring-blue-500" style="background-color: <%= color %>"></div>
</label>
<% end %> <% end %>
</div>
<% if category.errors.any? %> <div class="flex flex-wrap gap-2 justify-center mb-4">
<%= render "shared/form_errors", model: category %> <% Category.icon_codes.each do |icon| %>
<% end %> <label class="relative">
<%= f.radio_button :lucide_icon, icon, class: "sr-only peer" %>
<div class="p-1 rounded cursor-pointer hover:bg-gray-100 peer-checked:bg-gray-100 border-1 border-transparent peer-checked:border-gray-500">
<%= lucide_icon icon, class: "w-5 h-5" %>
</div>
</label>
<% end %>
</div>
<div class="flex flex-wrap gap-2 justify-center mb-4"> <div class="space-y-2">
<% Category.icon_codes.each do |icon| %> <%= f.select :classification, [["Income", "income"], ["Expense", "expense"]], { label: "Classification" }, required: true %>
<label class="relative"> <%= f.text_field :name, placeholder: t(".placeholder"), required: true, autofocus: true, label: "Name", data: { color_avatar_target: "name" } %>
<%= f.radio_button :lucide_icon, icon, class: "sr-only peer" %> <% unless category.parent? %>
<div class="p-1 rounded cursor-pointer hover:bg-gray-100 peer-checked:bg-gray-100 border-1 border-transparent peer-checked:border-gray-500"> <%= f.select :parent_id, categories.pluck(:name, :id), { include_blank: "(unassigned)", label: "Parent category (optional)" }, disabled: category.parent? %>
<%= lucide_icon icon, class: "w-5 h-5" %> <% end %>
</div> </div>
</label> </section>
<% end %>
</div>
<div class="space-y-2"> <section>
<%= f.select :classification, [["Income", "income"], ["Expense", "expense"]], { label: "Classification" }, required: true %> <%= hidden_field_tag :transaction_id, params[:transaction_id] %>
<%= f.text_field :name, placeholder: t(".placeholder"), required: true, autofocus: true, label: "Name", data: { color_avatar_target: "name" } %> <%= f.submit %>
<% unless category.parent? %> </section>
<%= f.select :parent_id, categories.pluck(:name, :id), { include_blank: "(unassigned)", label: "Parent category (optional)" }, disabled: category.parent? %> <% end %>
<% end %> </div>
</div> <% end %>
</section>
<section>
<%= hidden_field_tag :transaction_id, params[:transaction_id] %>
<%= f.submit %>
</section>
<% end %>
</div>