-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5433 from solidusio/elia/admin/product-components
Extract a `products/stock` component
- Loading branch information
Showing
12 changed files
with
118 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
admin/app/components/solidus_admin/products/index/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 9 additions & 17 deletions
26
admin/app/components/solidus_admin/products/status/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Products::Status::Component < SolidusAdmin::BaseComponent | ||
COLORS = { | ||
STATUSES = { | ||
available: :green, | ||
discontinued: :red | ||
}.freeze | ||
|
||
# @param product [Spree::Product] | ||
def initialize(product:) | ||
@product = product | ||
def self.from_product(product) | ||
new(status: product.available? ? :available : :discontinued) | ||
end | ||
|
||
def initialize(status:) | ||
@status = status | ||
end | ||
|
||
def call | ||
render component('ui/badge').new( | ||
name: t(".#{status}"), | ||
color: COLORS.fetch(status) | ||
name: t(".#{@status}"), | ||
color: STATUSES.fetch(@status) | ||
) | ||
end | ||
|
||
# @return [Symbol] | ||
# :available when the product is available | ||
# :discontinued when the product is not available | ||
def status | ||
if @product.available? | ||
:available | ||
else | ||
:discontinued | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
admin/app/components/solidus_admin/products/stock/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Products::Stock::Component < SolidusAdmin::BaseComponent | ||
def self.from_product(product) | ||
new( | ||
on_hand: product.total_on_hand, | ||
variants_count: product.variants.count, | ||
) | ||
end | ||
|
||
def initialize(on_hand:, variants_count:) | ||
@on_hand = on_hand | ||
@variants_count = variants_count | ||
end | ||
|
||
def call | ||
stock_info = | ||
case @on_hand | ||
when Float::INFINITY | ||
tag.span t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest' | ||
when 1..Float::INFINITY | ||
tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-forest' | ||
else | ||
tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-red-500' | ||
end | ||
|
||
variant_info = t('.for_variants', count: @variants_count) | ||
|
||
tag.div safe_join([stock_info, variant_info], ' ') | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
admin/app/components/solidus_admin/products/stock/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
en: | ||
stock: | ||
infinity: '∞' | ||
in_stock: '%{on_hand} in stock' | ||
for_variants: 'for %{count} variants' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 15 additions & 16 deletions
31
...pec/components/previews/solidus_admin/products/status/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
<table> | ||
<thead> | ||
<tr> | ||
<% definitions.each_key do |status| %> | ||
<th class="px-3 py-1 text-gray-500 text-center"><%= status.to_s.humanize %></th> | ||
<% end %> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<% definitions.each_value do |component| %> | ||
<th class="px-3 py-1"><%= render component %></th> | ||
<% end %> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Available | ||
</h6> | ||
|
||
<%= render current_component.new(status: :available) %> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Discontinued | ||
</h6> | ||
|
||
<%= render current_component.new(status: :discontinued) %> | ||
</div> |
10 changes: 10 additions & 0 deletions
10
admin/spec/components/previews/solidus_admin/products/stock/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "products/stock" | ||
class SolidusAdmin::Products::Stock::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
render_with_template | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
...spec/components/previews/solidus_admin/products/stock/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
In Stock | ||
</h6> | ||
|
||
<%= render current_component.new(on_hand: 32, variants_count: 12) %> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Infinite stock | ||
</h6> | ||
|
||
<%= render current_component.new(on_hand: Float::INFINITY, variants_count: 12) %> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Out of stock | ||
</h6> | ||
|
||
<%= render current_component.new(on_hand: 0, variants_count: 12) %> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Negative stock | ||
</h6> | ||
|
||
<%= render current_component.new(on_hand: -10, variants_count: 12) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/products/stock/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::Products::Stock::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
end |