Partial refresh of Measurements#new form

This commit is contained in:
2025-02-18 18:27:47 +01:00
parent c48bf290fd
commit 8401424efa
6 changed files with 65 additions and 55 deletions

View File

@@ -1,45 +1,26 @@
<%= tabular_form_with model: current_user, html: {id: :new_readouts_form} do |user| %>
<% if @readouts&.present? %>
<fieldset>
<%= tag.legend @ancestor_fullname unless @ancestor_fullname.empty? %>
<table class="items centered">
<tbody id="readouts">
<%= user.fields_for :readouts, @readouts do |form| %>
<% row = dom_id(form.object.quantity, :new, :readout) %>
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
<td>
<%= form.object.quantity.fullname.delete_prefix(@ancestor_fullname) %>
<%= form.hidden_field :quantity_id %>
</td>
<td>
<%= form.number_field :value, required: true, autofocus: true,
size: 10 %>
</td>
<td>
<%= form.select :unit_id, options_from_collection_for_select(
@units, :id, ->(u){ sanitize('&emsp;'*(u.base_id ? 1 : 0) + u.symbol) }
) %>
</td>
<% @readouts.each do |readout| %>
<%= tabular_fields_for 'readouts[]', readout do |form| %>
<% row = dom_id(form.object.quantity, :new, :readout) %>
<%- tag.tr id: row, onkeydown: 'processKey(event)' do %>
<td>
<%= form.object.quantity.full_name.delete_prefix(@closest_ancestor.full_name) %>
<%= form.hidden_field :quantity_id %>
</td>
<td>
<%= form.number_field :value, required: true, autofocus: true,
size: 10 %>
</td>
<td>
<%= form.select :unit_id, options_from_collection_for_select(
@units, :id, ->(u){ sanitize('&emsp;'*(u.base_id ? 1 : 0) + u.symbol) }
) %>
</td>
<td class="actions">
<%= image_link_to t(:delete), 'delete-outline', quantities_path,
class: 'dangerous',
onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
</td>
<% end %>
<% end %>
<tr id="new_readouts_actions">
<td></td>
<td></td>
<td><div class="actions"><%= user.button %></div></td>
<td><div class="actions">
<%= image_link_to t(:cancel), "close-outline", measurements_path,
class: 'dangerous', name: :cancel,
onclick: render_turbo_stream('form_close') %>
</div></td>
</tr>
</tbody>
</table>
</fieldset>
<td class="actions">
<%= image_link_to t(:delete), 'delete-outline', quantities_path,
class: 'dangerous',
onclick: render_turbo_stream('form_destroy_row', {row: row}) %>
</td>
<% end %>
<% end %>
<% end %>

View File

@@ -0,0 +1,21 @@
<%= tabular_form_with url: new_measurement_path, html: {id: :new_readouts_form} do %>
<% if @readouts&.present? %>
<fieldset>
<%= tag.legend @closest_ancestor.full_name if @closest_ancestor&.full_name %>
<table class="items centered">
<tbody id="readouts">
<tr id="new_readouts_actions">
<td></td>
<td></td>
<td><div class="actions"><%= button_tag %></div></td>
<td><div class="actions">
<%= image_link_to t(:cancel), "close-outline", measurements_path,
class: 'dangerous', name: :cancel,
onclick: render_turbo_stream('form_close') %>
</div></td>
</tr>
</tbody>
</table>
</fieldset>
<% end %>
<% end %>

View File

@@ -1,6 +1,6 @@
<div class="topside vflex">
<% if current_user.at_least(:active) %>
<%= render partial: 'form' %>
<%= render partial: 'form_frame' %>
<%# TODO: show hint when no quantities/units defined %>
<div class="hflex">
<%= select_tag :id, options_from_collection_for_select(

View File

@@ -1,3 +1,7 @@
<%= turbo_stream.replace :new_readouts_form, method: :morph do %>
<%= turbo_stream.replace :new_readouts_form do %>
<%= render partial: 'form_frame' %>
<% end if @prev_quantities.empty? %>
<%= turbo_stream.before :new_readouts_actions do %>
<%= render partial: 'form' %>
<% end %>