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.

Set up API for reposition on page sections.

Matthew Kennedyb9c3afd867cb
diff --git a/api/app/controllers/spree/api/v2/platform/cms_pages_controller.rb b/api/app/controllers/spree/api/v2/platform/cms_pages_controller.rb
new file mode 100644
index 0000000000..bdccc7d38e
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/platform/cms_pages_controller.rb
@@ -0,0 +1,19 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class CmsPagesController < ResourceController
+ private
+
+ def model_class
+ Spree::CmsPage
+ end
+
+ def scope_includes
+ [:cms_sections]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb b/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb
new file mode 100644
index 0000000000..ba48768189
--- /dev/null
+++ b/api/app/controllers/spree/api/v2/platform/cms_sections_controller.rb
@@ -0,0 +1,34 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class CmsSectionsController < ResourceController
+ before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition
+
+ def reposition
+ spree_authorize! :update, @moved_section if spree_current_user.present?
+
+ @moved_section = scope.find(params[:section_id])
+ new_index = params[:new_position_idx].to_i + 1
+
+ if @moved_section && new_index
+ @moved_section.insert_at(new_index)
+ else
+ head :bad_request
+ end
+
+ if @moved_section.save
+ head :no_content
+ end
+ end
+
+ private
+
+ def model_class
+ Spree::CmsSection
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index cfcaef2e47..fe3c9cec1a 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -263,6 +263,14 @@ Spree::Core::Engine.add_routes do
end
end
+ # CMS Pages API
+ resources :cms_pages
+ resource :cms_sections 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 d41eee98bf..cecf9afa58 100644
--- a/backend/app/assets/javascripts/spree/backend.js
+++ b/backend/app/assets/javascripts/spree/backend.js
@@ -100,6 +100,7 @@ Spree.routes.stock_items_api = function (stockLocationId) {
// API v2
Spree.routes.countries_api_v2 = Spree.pathFor('api/v2/platform/countries')
+Spree.routes.section_reposition_api_v2 = Spree.pathFor('/api/v2/platform/cms_sections/reposition')
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')
diff --git a/backend/app/assets/javascripts/spree/backend/cms_pages/_index.es6 b/backend/app/assets/javascripts/spree/backend/cms_pages/_index.es6
index 4ea08fe53f..60fe8480d3 100644
--- a/backend/app/assets/javascripts/spree/backend/cms_pages/_index.es6
+++ b/backend/app/assets/javascripts/spree/backend/cms_pages/_index.es6
@@ -8,22 +8,40 @@ const el = document.getElementById('cmsPagesectionsArea')
animation: 550,
easing: 'cubic-bezier(1, 0, 0, 1)',
swapThreshold: 0.9,
- forceFallback: true
- // onEnd: function (evt) {
- // var itemEl = evt.item.getAttribute('data-section-id')
- // var newin = evt.newIndex
- // return $.ajax({
- // url: Spree.routes.classifications_api,
- // method: 'PUT',
- // dataType: 'json',
- // data: {
- // token: Spree.api_key,
- // product_id: itemEl,
- // taxon_id: $('#taxon_id').val(),
- // position: newin
- // }
- // })
- // }
+ forceFallback: true,
+ onEnd: function (evt) {
+ handleSectionReposition(evt)
+ }
})
}
})
+
+
+function handleSectionReposition(evt) {
+ const data = {
+ section_id: parseInt(evt.item.dataset.sectionId, 10),
+ new_position_idx: parseInt(evt.newIndex, 10)
+ }
+
+ fetch(Spree.routes.section_reposition_api_v2, {
+ method: 'PATCH',
+ headers: {
+ Authorization: 'Bearer ' + OAUTH_TOKEN,
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify(data)
+ })
+ .then(response => {
+ if (response.ok !== true) {
+ handleMenuItemMoveError()
+ }
+ })
+ .catch(err => {
+ console.error(err);
+ });
+}
+
+function handleMenuItemMoveError () {
+ // eslint-disable-next-line no-undef
+ show_flash('error', Spree.translations.move_could_not_be_saved)
+}
diff --git a/backend/app/assets/javascripts/spree/backend/menus/menu.es6 b/backend/app/assets/javascripts/spree/backend/menus/menu.es6
index 0e1f55eb2e..37074cebac 100644
--- a/backend/app/assets/javascripts/spree/backend/menus/menu.es6
+++ b/backend/app/assets/javascripts/spree/backend/menus/menu.es6
@@ -27,7 +27,7 @@ document.addEventListener('DOMContentLoaded', function() {
}
})
-function handleMenuItemMove(evt, successCallback) {
+function handleMenuItemMove(evt) {
const data = {
moved_item_id: parseInt(evt.item.dataset.itemId, 10),
new_parent_id: parseInt(evt.to.dataset.parentId, 10) || null,
diff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rb
index 261827dd4a..5f6cf99bb2 100644
--- a/core/app/models/spree/cms_section.rb
+++ b/core/app/models/spree/cms_section.rb
@@ -3,6 +3,8 @@ module Spree
acts_as_list scope: :cms_page
belongs_to :cms_page
+ default_scope { order(position: :asc) }
+
has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::Icon'
accepts_nested_attributes_for :icon, reject_if: :all_blank