Skip to content

Commit

Permalink
Avoid sending sort events twice
Browse files Browse the repository at this point in the history
SortableJS sends two events if you drag an element from one
nesteable elements container to another. Leading to incrementing
the target lists positions to high.

Since acts_as_list maintains both lists correctly we can simply
suppress the second event on the source list.
  • Loading branch information
tvdeyen committed Feb 27, 2024
1 parent df533be commit fa8a966
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/javascript/alchemy_admin/sortable_elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ function onSort(event) {
params.parent_element_id = parentElement.dataset.elementId
}

post(Alchemy.routes.order_admin_elements_path, params).then((response) => {
const data = response.data
Alchemy.growl(data.message)
Alchemy.PreviewWindow.refresh()
item.updateTitle(data.preview_text)
})
// Only send the request if the item was moved to a different container
// or sorted in the same list. Not on the old list in order to avoid incrementing
// the position of the other elements.
if (event.target === event.to) {
post(Alchemy.routes.order_admin_elements_path, params).then((response) => {
const data = response.data
Alchemy.growl(data.message)
Alchemy.PreviewWindow.refresh()
item.updateTitle(data.preview_text)
})
}
}

function onEnd() {
Expand Down

0 comments on commit fa8a966

Please sign in to comment.