-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
a29dbbc
commit 8bca5b3
Showing
4 changed files
with
59 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters