Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Hook up drag and drop re-positioning.

Matthew Kennedy5cfde61a2774
diff --git a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb
new file mode 100644
index 0000000000..44a7151e20
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb
@@ -0,0 +1,26 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class MenuItemsController < ResourceController
+ def reposition
+ @moved_item = Spree::MenuItem.find(params[:moved_item_id])
+ @new_parent = if params[:new_parent_id] == 'undefined' || params[:new_parent_id].nil?
+ nil
+ else
+ Spree::MenuItem.find(params[:new_parent_id])
+ end
+
+ @moved_item.move_with_index(params[:new_position_idx].to_i, @new_parent)
+ end
+
+ private
+
+ def model_class
+ Spree::MenuItem
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v2/platform/menus_controller.rb b/api/app/controllers/spree/api/v2/platform/menus_controller.rb
new file mode 100644
index 0000000000..e568dd4fab
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/platform/menus_controller.rb
@@ -0,0 +1,19 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class MenusController < ResourceController
+ private
+
+ def model_class
+ Spree::Menu
+ end
+
+ def scope_includes
+ [:menu_items]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb b/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb
new file mode 100644
index 0000000000..e3aff856be
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb
@@ -0,0 +1,13 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class MenuItemSerializer < BaseSerializer
+ include ::Spree::Api::V2::ResourceSerializerConcern
+
+ belongs_to :menu
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v2/platform/menu_serializer.rb b/api/app/serializers/spree/api/v2/platform/menu_serializer.rb
new file mode 100644
index 0000000000..2ec221508b
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/menu_serializer.rb
@@ -0,0 +1,13 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class MenuSerializer < BaseSerializer
+ include ::Spree::Api::V2::ResourceSerializerConcern
+
+ has_many :menu_items
+ end
+ end
+ end
+ end
+end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 3ee9862068..469b04da5f 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -252,6 +252,14 @@ Spree::Core::Engine.add_routes do
resources :credit_cards
resources :addresses
+ # Menu API
+ resources :menus
+ resource :menu_items do
+ member do
+ patch :reposition
+ end
+ end
+
# Store API
resources :stores
end
diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js
index d2130de203..470a1d11ec 100644
--- a/backend/app/assets/javascripts/spree/backend.js
+++ b/backend/app/assets/javascripts/spree/backend.js
@@ -96,10 +96,10 @@ Spree.routes.stock_items_api = function (stockLocationId) {
// API v2
Spree.routes.countries_api_v2 = Spree.pathFor('api/v2/platform/countries')
-
+Spree.routes.menus_api_v2 = Spree.pathFor('api/v2/platform/menus')
+Spree.routes.menus_items_api_v2 = Spree.pathFor('api/v2/platform/menu_items/reposition')
Spree.routes.option_types_api_v2 = Spree.pathFor('api/v2/platform/option_types')
Spree.routes.option_values_api_v2 = Spree.pathFor('api/v2/platform/option_values')
-
Spree.routes.users_api_v2 = Spree.pathFor('api/v2/platform/users')
Spree.apiV2Authentication = function() {
diff --git a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6 b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
index c4e95373dd..0f3c82170c 100644
--- a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
+++ b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
@@ -27,10 +27,19 @@ document.addEventListener('DOMContentLoaded', function() {
}
})
-function handleMenuItemMove (evt) {
+function handleMenuItemMove(evt, successCallback) {
+ const movedItem = evt.item.dataset.itemId
const newParentId = evt.to.dataset.parentId
- const position = evt.newIndex
+ const newPosition = evt.newIndex
- console.log(newParentId)
- console.log(position)
+ fetch(Spree.routes.menus_items_api_v2 + `?moved_item_id=${movedItem}&new_parent_id=${newParentId}&new_position_idx=${newPosition}`, {
+ method: 'PATCH',
+ headers: Spree.apiV2Authentication()
+ })
+ .then(response => {
+ console.log(response);
+ })
+ .catch(err => {
+ console.error(err);
+ });
}
diff --git a/core/app/models/awesome_nested_set/movable_decorator.rb b/core/app/models/awesome_nested_set/movable_decorator.rb
new file mode 100644
index 0000000000..8e4387ed97
--- /dev/null
+++ b/core/app/models/awesome_nested_set/movable_decorator.rb
@@ -0,0 +1,39 @@
+module AwesomeNestedSet
+ module MovableDecorator
+ # Had to monkey patch AwesomeNestedSet, adding the ability to sort root items.
+
+ # AwesomeNestedSet only give you .move_to_child_with_index,
+ # this works a treat for sorting any item that is nested inside a parent,
+ # but if you have items at the root level, you get nothing out of the box for re-ordering position.
+
+ # Pass the new index of the item and its new parent node.
+ # if no node is passed (node = nil), it assumes you are going to / or already are at the root level and passes the
+ # process to .move_to_root_with_index, if a node is present it uses .move_to_child_with_index from AwesomeNestedSet
+ def move_with_index(index, node = nil)
+ if node.nil?
+ move_to_root_with_index(index)
+ else
+ move_to_child_with_index(node, index)
+ end
+ end
+
+ def move_to_root_with_index(index)
+ # If we are already at level 0 the item should be root? => true
+ # and we can skip moving the item to root.
+ move_to_root unless level == 0 && root?
+
+ # Sort the positioninng of the item
+ # at root level based on its siblings.
+ my_position = siblings.to_a.index(self)
+ if my_position && my_position < index
+ move_to_right_of(siblings[index])
+ elsif my_position && my_position == index
+ # do nothing. already there.
+ else
+ move_to_left_of(siblings[index])
+ end
+ end
+ end
+end
+
+CollectiveIdea::Acts::NestedSet::Model::Movable.prepend AwesomeNestedSet::MovableDecorator
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 3a13c3ff40..ad5bfa9217 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -48,10 +48,6 @@ module Spree
end
end
- def child_index=(idx)
- move_to_child_with_index(parent, idx.to_i) unless new_record?
- end
-
private
def reset_link_attributes