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.

Storefront: added missing presenters

Damian Legawiec22e31e7b7d53
diff --git a/storefront/app/presenters/spree/featured_product_presenter.rb b/storefront/app/presenters/spree/featured_product_presenter.rb
new file mode 100644
index 0000000000..c4c8e52866
--- /dev/null
+++ b/storefront/app/presenters/spree/featured_product_presenter.rb
@@ -0,0 +1,42 @@
+module Spree
+ class FeaturedProductPresenter
+ def initialize(section, params:, currency:)
+ @section = section
+ @params = params
+ @currency = currency
+
+ initialize_selected_variants if product
+ end
+
+ attr_reader :selected_variant, :variant_from_options
+
+ delegate :product, to: :section
+
+ private
+
+ attr_reader :section, :params, :currency, :store
+
+ def initialize_selected_variants
+ options_hash = if params.present?
+ params.split(',').to_h do |option|
+ key, *value = option.split(':')
+ [key, value.join(':')]
+ end
+ else
+ {}
+ end
+
+ variant_finder = Spree::Storefront::VariantFinder.new(
+ product: product,
+ variant_id: nil,
+ options_hash: options_hash,
+ current_currency: currency
+ )
+
+ @selected_variant, @variant_from_options =
+ Rails.cache.fetch([product.cache_key_with_version, 'variant-finder', currency, options_hash].compact) do
+ variant_finder.find
+ end
+ end
+ end
+end
diff --git a/storefront/app/presenters/spree/mega_nav_presenter.rb b/storefront/app/presenters/spree/mega_nav_presenter.rb
new file mode 100644
index 0000000000..42b35b83f3
--- /dev/null
+++ b/storefront/app/presenters/spree/mega_nav_presenter.rb
@@ -0,0 +1,55 @@
+module Spree
+ class MegaNavPresenter
+ ColumnPresenter = Struct.new(:title, :links, :view_all_linkable, keyword_init: true)
+
+ def initialize(mega_nav)
+ @mega_nav = mega_nav
+ end
+
+ def main_link
+ @main_link ||=
+ if mega_nav.is_a?(Spree::PageBlocks::MegaNavWithSubcategories)
+ to_link(mega_nav.taxon)
+ else
+ mega_nav.links.first
+ end
+ end
+
+ def columns
+ @columns ||=
+ if mega_nav.is_a?(Spree::PageBlocks::MegaNavWithSubcategories)
+ subcategories = mega_nav.taxon&.self_and_descendants || []
+ main_subcategories = subcategories.find_all { |c| c.parent_id == mega_nav.taxon.id }.sort_by(&:position)
+
+ main_subcategories.first(max_columns).map do |child_category|
+ links = subcategories.find_all { |c| c.parent_id == child_category.id }.sort_by(&:position).map(&method(:to_link))
+ ColumnPresenter.new(title: child_category.name, links: links, view_all_linkable: child_category) if links.any?
+ end.compact_blank
+ elsif mega_nav.is_a?(Spree::PageBlocks::MegaNav)
+ [ColumnPresenter.new(title: main_link.label, links: mega_nav.links.drop(1), view_all_linkable: main_link.linkable)]
+ else
+ []
+ end
+ end
+
+ private
+
+ attr_reader :mega_nav
+
+ def to_link(object)
+ if object.is_a?(Spree::Taxon)
+ Spree::PageLink.new(
+ linkable: object,
+ label: object.name,
+ open_in_new_tab: false
+ )
+ else
+ object
+ end
+ end
+
+ def max_columns
+ @max_columns ||= mega_nav.featured_taxon.present? ? 3 : 4
+ end
+ end
+end