From 8bca5b3c7edcb2ed7fdd415f6681282ae915473b Mon Sep 17 00:00:00 2001 From: elasticspoon Date: Wed, 14 Feb 2024 15:22:20 -0500 Subject: [PATCH] Refactor request item javascript (#4105) * feat: move js behavior to stimulus controller feat: replaces dataset with template * refactor: make methods more explict make methods more clear fix merge conflicts --- .../partners/multi_item_form_helper.rb | 20 +++++++--- .../controllers/request_item_controller.js | 40 +++++++++++++++++++ .../individuals_requests/new.html.erb | 33 ++------------- app/views/partners/requests/new.html.erb | 32 +-------------- 4 files changed, 59 insertions(+), 66 deletions(-) create mode 100644 app/javascript/controllers/request_item_controller.js diff --git a/app/helpers/partners/multi_item_form_helper.rb b/app/helpers/partners/multi_item_form_helper.rb index a8addd66a9..2962dded2c 100644 --- a/app/helpers/partners/multi_item_form_helper.rb +++ b/app/helpers/partners/multi_item_form_helper.rb @@ -1,15 +1,23 @@ module Partners module MultiItemFormHelper def remove_item_button(label, soft: false) - link_to label, 'javascript:void(0)', class: 'btn btn-warning', data: { remove_item: soft ? "soft" : "hard" } + link_to label, 'javascript:void(0)', class: 'btn btn-warning', + data: { action: 'click->request-item#removeItem:prevent', remove_soft: soft ? true : false} end def add_item_button(label, container: ".fields", &block) - link_to( - label, "javascript:void(0)", - class: "btn btn-outline-primary", - data: { add_target: container, add_template: capture(&block) } - ) + content_tag :div do + concat( + link_to(label, "javascript:void(0)", class: "btn btn-outline-primary", + data: { + request_item_target: 'addButton', add_target: container, + action: "click->request-item#addItem:prevent" + }) + ) + concat( + content_tag(:template, capture(&block), data: { request_item_target: 'addTemplate' }) + ) + end end end end diff --git a/app/javascript/controllers/request_item_controller.js b/app/javascript/controllers/request_item_controller.js new file mode 100644 index 0000000000..92469f2dda --- /dev/null +++ b/app/javascript/controllers/request_item_controller.js @@ -0,0 +1,40 @@ +import { Controller } from "@hotwired/stimulus"; + +// Connects to data-controller="request-item" +export default class extends Controller { + static targets = ["addButton", "addDest", "addTemplate"]; + + addItem() { + const template = this.addTemplateTarget.content.firstElementChild.innerHTML; + + const uniqId = new Date().getTime(); + const rendered = this.setUniqIds(template, uniqId); + + this.addDestTarget.insertAdjacentHTML("beforeend", rendered); + } + + removeItem(event) { + console.log(event.target); + const wrapper = event.target.closest("tr"); + const removeSoft = event.target.dataset.removeSoft === "false"; + + if (removeSoft) { + wrapper.remove(); + } else { + const destroyField = wrapper.querySelector("input[name*='_destroy']"); + if (destroyField) destroyField.value = 1; + wrapper.style.display = "none"; + } + } + + // This regex replaces [number] with [templateId] + // or _number_ with _templateId_ + // Ex: name="request[items_attributes][0][name]" => name="request[items_attributes][897123413][name]" + // Ex: id="request_items_attributes_0_name" => id="request_items_attributes_9871239487_name" + setUniqIds(template, templateId) { + return template.replace( + /([\[_])([0-9]+)([\]_])/g, + "$1" + templateId + "$3", + ); + } +} diff --git a/app/views/partners/individuals_requests/new.html.erb b/app/views/partners/individuals_requests/new.html.erb index 16ff5b67e3..e7eab24a58 100644 --- a/app/views/partners/individuals_requests/new.html.erb +++ b/app/views/partners/individuals_requests/new.html.erb @@ -30,7 +30,8 @@ <%= render partial: 'partners/requests/error' %> <% end %> - <%= simple_form_for @request, url: partners_individuals_requests_path, html: {role: 'form', class: 'form-horizontal'} do |form| %> + <%= simple_form_for @request, url: partners_individuals_requests_path, html: {role: 'form', class: 'form-horizontal'}, + data: {controller: 'request-item'} do |form| %> <%= form.input :comments, label: "Comments:", as: :text, class: "form-control", wrapper: :input_group %> @@ -42,7 +43,7 @@ - + <%= render 'partners/individuals_requests/item_request', form: form %> @@ -66,31 +67,3 @@ - - diff --git a/app/views/partners/requests/new.html.erb b/app/views/partners/requests/new.html.erb index d2dafba6bc..6e643267d2 100644 --- a/app/views/partners/requests/new.html.erb +++ b/app/views/partners/requests/new.html.erb @@ -31,7 +31,7 @@ <% end %> <%= simple_form_for @partner_request, url: partners_requests_path(@partner_request, :organization_id => current_partner.organization.id), - html: {role: 'form', class: 'form-horizontal'}, method: :post do |form| %> + html: {role: 'form', class: 'form-horizontal'}, method: :post, data: { controller: 'request-item' } do |form| %> <%= form.input :comments, label: "Comments:", as: :text, class: "form-control", wrapper: :input_group %> @@ -41,7 +41,7 @@ - + <%= render 'item_request', form: form %>
Quantity
@@ -64,31 +64,3 @@ - -