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.

Implement the front end desktop Main Menu to use the new navigation menus system.

Matthew Kennedy8f8b74a23516
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index 15dffc2c3b..806b2c4943 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -5,7 +5,7 @@ module Spree
validates :name, presence: true, uniqueness: true
- scope :by_name, ->(menu_name) { where name: menu_name }
scope :by_store, ->(store) { joins(:stores).where('store_id = ?', store) }
+ scope :by_name, ->(menu_name) { where name: menu_name }
end
end
diff --git a/frontend/app/controllers/spree/store_controller.rb b/frontend/app/controllers/spree/store_controller.rb
index 3be6917689..8b7812ca9d 100644
--- a/frontend/app/controllers/spree/store_controller.rb
+++ b/frontend/app/controllers/spree/store_controller.rb
@@ -36,6 +36,24 @@ module Spree
@menus = Spree::Menu.by_store(current_store)
end
+ def menu_by_name(name)
+ menu = @menus.by_name(name)
+
+ return if menu.nil?
+
+ menu[0]
+ end
+ helper_method :menu_by_name
+
+ def menu_by_name_items(name)
+ menu = @menus.by_name(name)[0] || nil
+
+ return if menu.nil?
+
+ menu.menu_items.order(:lft)
+ end
+ helper_method :menu_by_name_items
+
protected
def config_locale
diff --git a/frontend/app/helpers/spree/menu_helper.rb b/frontend/app/helpers/spree/menu_helper.rb
index 26e9ffffdc..0d823fc910 100644
--- a/frontend/app/helpers/spree/menu_helper.rb
+++ b/frontend/app/helpers/spree/menu_helper.rb
@@ -1,10 +1,4 @@
module Spree
module MenuHelper
- def build_main_menu(menu)
- "MENU #{menu.name}"
- end
-
- def build_footer_menu(menu)
- end
end
end
diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
index e7719d74e5..7b3089e792 100644
--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb
+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb
@@ -1,55 +1,71 @@
-<% if spree_navigation_data.any? %>
+<% main_menu_items = menu_by_name_items('Main Menu') %>
+
+<% if main_menu_items %>
<div class="h-100" role="navigation" aria-label="<%= Spree.t('nav_bar.desktop') %>">
<ul class="nav h-100 main-nav-bar">
- <% spree_navigation_data.each do |root| %>
- <li class="p-0 nav-item main-nav-bar-category">
- <% has_dropdown = root[:items].present? || root[:promo_banners].present? %>
- <% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %>
- <% data_attr = has_dropdown ? { toggle: "dropdown" } : {} %>
- <% aria_attr = has_dropdown ? { haspopup: true, expanded: false } : {} %>
- <%= link_to root[:title], root[:url], class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr %>
- <% if has_dropdown %>
- <div class="dropdown-menu w-100 shadow main-nav-bar-category-dropdown">
- <div class="container p-0 d-flex justify-content-xl-around mx-auto">
- <div class="row">
- <% if root[:items].present? %>
- <div class="category-links">
- <% if root[:subtitle].present? %>
- <div class="category-links-header text-uppercase">
- <%= root[:subtitle] %>
+ <% main_menu_items.each do |item| %>
+ <% if item.root? %>
+ <li class="p-0 nav-item main-nav-bar-category">
+ <% has_dropdown = item.children.present? %>
+ <% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %>
+ <% data_attr = has_dropdown ? { toggle: "dropdown" } : {} %>
+ <% aria_attr = has_dropdown ? { haspopup: true, expanded: false } : {} %>
+ <%= link_to(item.name, item.destination, class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>
+ <% if has_dropdown %>
+ <div class="dropdown-menu w-100 shadow main-nav-bar-category-dropdown">
+ <div class="container p-0 d-flex justify-content-xl-around mx-auto">
+ <div class="row">
+ <% item.children.each do |child| %>
+
+ <!-- Categories -->
+ <% if child.name == 'Categories' && child.item_type == 'Container' %>
+ <div class="category-links">
+ <div class="category-links-header text-uppercase">
+ <%= child.name %>
+ </div>
+ <% if child.children.present? %>
+ <ul class="pl-0">
+ <% child.children.each do |category_link| %>
+ <li>
+ <%= link_to category_link.name, category_link.destination, class: "text-uppercase main-nav-bar-category-links dropdown-item" %>
+ </li>
+ <% end %>
+ </ul>
+ <% end %>
</div>
<% end %>
- <ul class="pl-0">
- <% root[:items].each do |item| %>
- <li>
- <%= link_to item[:title], item[:url], class: "text-uppercase main-nav-bar-category-links dropdown-item" %>
- </li>
+
+ <!-- Promos -->
+ <% if child.name == 'Promos' && child.item_type == 'Container' %>
+ <% if child.children.present? %>
+ <% child.children.each do |promo_banner| %>
+ <%= link_to promo_banner.destination, class: "dropdown-item" do %>
+ <div class="category-image text-center mx-3">
+ <% if promo_banner.image_asset.attached? && promo_banner.image_asset.image? %>
+ <%= image_tag main_app.url_for(promo_banner.image_asset), title: promo_banner.subtitle %>
+ <% end %>
+ <div class="category-box">
+ <div class="category-box-small-text">
+ <%= promo_banner.subtitle %>
+ </div>
+ <div class="category-box-big-text">
+ <%= promo_banner.name %>
+ </div>
+ </div>
+ </div>
+ <% end %>
+ <% end %>
<% end %>
- </ul>
- </div>
- <% end %>
- <% if root[:promo_banners].present? %>
- <% root[:promo_banners].each do |promo_banner| %>
- <%= link_to promo_banner[:url], class: "dropdown-item" do %>
- <div class="category-image text-center mx-3">
- <%= main_nav_image(promo_banner[:image], promo_banner[:title]) %>
- <div class="category-box">
- <div class="category-box-small-text">
- <%= promo_banner[:subtitle] %>
- </div>
- <div class="category-box-big-text">
- <%= promo_banner[:title] %>
- </div>
- </div>
- </div>
<% end %>
+
<% end %>
- <% end %>
+ </div>
</div>
</div>
- </div>
- <% end %>
- </li>
+
+ <% end %>
+ </li>
+ <% end %>
<% end %>
</ul>
</div>
diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rb
index 13293b0fa3..472949da99 100644
--- a/sample/db/samples/menu_items.rb
+++ b/sample/db/samples/menu_items.rb
@@ -38,24 +38,24 @@ MENUS.each do |menu_id|
menu_root_sw.save!
##############
- # Catagories #
+ # Categories #
##############
menu_cat_women = Spree::MenuItem.where(
- name: 'Catagories',
+ name: 'Categories',
item_type: 'Container',
menu_id: menu_id,
parent_id: menu_root_women
).first_or_create!
menu_cat_men = Spree::MenuItem.where(
- name: 'Catagories',
+ name: 'Categories',
item_type: 'Container',
menu_id: menu_id,
parent_id: menu_root_men
).first_or_create!
menu_cat_sw = Spree::MenuItem.where(
- name: 'Catagories',
+ name: 'Categories',
item_type: 'Container',
menu_id: menu_id,
parent_id: menu_root_sw