Skip to content

Commit

Permalink
🧹 Marketplace: Simplify changing Product quantities in Cart (#2157
Browse files Browse the repository at this point in the history
)

* 🧹 `Marketplace`: Simplify changing `Product` quantities in `Cart`

- #2155
- #1326

This gets rid of a bunch of nonsense from the `CartProductComponent`,
and tees up some parts that I use in the `Menu::ProductComponent`

Extracted from: #2072
  • Loading branch information
zspencer authored Jan 29, 2024
1 parent 7b9db24 commit 051551b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 50 deletions.
4 changes: 4 additions & 0 deletions app/furniture/marketplace/cart_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def price_total
product.price * quantity
end

def quantity_picker
QuantityPicker.new(cart_product: self)
end

private

def editable_cart
Expand Down
6 changes: 6 additions & 0 deletions app/furniture/marketplace/cart_product/quantity_picker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Marketplace
class CartProduct::QuantityPicker < Model
attr_accessor :cart_product
delegate :location, :quantity, to: :cart_product
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="flex flex-row justify-between items-center">
<%- if quantity_picker.quantity < 1%>
<span></span>
<%- elsif quantity_picker.quantity == 1 %>
<%= button_to("🗑️", quantity_picker.location, method: :delete) %>
<%- else %>
<%= button_to("➖", quantity_picker.location, method: :put, params: { cart_product: { quantity: quantity_picker.quantity - 1 } }) %>
<%- end %>

<span class="py-2 px-2 my-1 text-lg"><%= quantity_picker.quantity %></span>

<%= button_to("➕", quantity_picker.location, method: :put, params: { cart_product: { quantity: quantity_picker.quantity + 1 } }) %>
</div>
15 changes: 5 additions & 10 deletions app/furniture/marketplace/cart_product_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@
<%= humanized_money_with_symbol(product.price) %>
</td>
<td class="py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
<div class="flex flex-row justify-between items-center">
<%= render "buttons/minus", title: t('marketplace.cart_product_component.remove'), method: remove_method,
disabled: cart_product.quantity.zero?,
href: remove_href %>

<span data-cart-product-quantity class="py-2 px-2 my-1 text-lg"><%= quantity %></span>

<%= render "buttons/plus", method: add_method, title: t('marketplace.cart_product_component.add'),
href: add_href %>
</div>
<%- if cart_product.quantity.zero? %>
<%= button_to("Add to Cart", cart.location(child: :cart_products), method: :post, params: { cart_product: { product_id: product.id, quantity: 1 } }) %>
<%- else %>
<%= render cart_product.quantity_picker %>
<%- end %>
</td>
</tr>
30 changes: 1 addition & 29 deletions app/furniture/marketplace/cart_product_component.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Marketplace
class CartProductComponent < ApplicationComponent
attr_accessor :cart_product
delegate :name, :description, :location, to: :cart_product
delegate :name, :description, :quantity, :location, to: :cart_product
delegate :cart, :product, to: :cart_product

def initialize(cart_product:, **kwargs)
Expand All @@ -10,34 +10,6 @@ def initialize(cart_product:, **kwargs)
self.cart_product = cart_product
end

def quantity
cart_product.destroyed? ? 0 : cart_product.quantity
end

def add_quantity
quantity + 1
end

def add_method
(add_quantity == 1) ? :post : :put
end

def add_href
cart_product.location(query_params: {cart_product: {quantity: add_quantity, product_id: product.id}})
end

def remove_quantity
[quantity - 1, 0].max
end

def remove_method
remove_quantity.zero? ? :delete : :put
end

def remove_href
cart_product.location(query_params: {cart_product: {quantity: remove_quantity, product_id: product.id}})
end

def dom_id
super(product).gsub("product", "cart_product")
end
Expand Down
3 changes: 0 additions & 3 deletions app/furniture/marketplace/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ en:
update:
success: "Changed %{quantity} %{product} on Cart"
failure: "Could not change %{quantity} %{product} on Cart"
cart_product_component:
remove: Remove from Cart
add: Add to Cart
payment_settings:
index:
link_to: "Payment Settings"
2 changes: 1 addition & 1 deletion spec/furniture/marketplace/buying_products_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def url_options

def add_product_to_cart(product)
within("##{dom_id(product).gsub("product", "cart_product")}") do
click_link(t("marketplace.cart_product_component.add"))
click_button("Add to Cart")
end
end

Expand Down
21 changes: 14 additions & 7 deletions spec/furniture/marketplace/cart_product_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@
let(:cart) { create(:marketplace_cart) }
let(:marketplace) { cart.marketplace }
let(:product) { create(:marketplace_product, :with_description, :with_photo) }
let(:cart_product) { create(:marketplace_cart_product, cart: cart, product: product) }
let(:cart_product) { create(:marketplace_cart_product, cart:, product:, quantity: 5) }

let(:component) { described_class.new(cart_product: cart_product, current_person: operator) }

it { is_expected.to have_content(product.name) }
it { is_expected.to have_content(product.description) }
it { is_expected.to have_content(helpers.humanized_money_with_symbol(product.price)) }
it { is_expected.to have_link(I18n.t("marketplace.cart_product_component.add")) }
it { is_expected.to have_link(I18n.t("marketplace.cart_product_component.remove")) }
it { is_expected.to have_css("img[src*='#{product.photo.filename}']") }
it { is_expected.to have_button("➕") }
it { is_expected.to have_button("➖") }

context "when the product is not yet in the cart" do
let(:cart_product) { build(:marketplace_cart_product, cart: cart, product: product, quantity: 0) }
context "when the quantity is 0" do
let(:cart_product) { build(:marketplace_cart_product, cart:, product:, quantity: 0) }

it { is_expected.to have_no_link(I18n.t("marketplace.cart_product_component.remove")) }
it { is_expected.to have_no_button("➖") }
it { is_expected.to have_button("Add to Cart") }
end

context "when the quantity is 1" do
let(:cart_product) { build(:marketplace_cart_product, cart:, product:, quantity: 1) }

it { is_expected.to have_button("➕") }
it { is_expected.to have_button("🗑️") }
end
end

0 comments on commit 051551b

Please sign in to comment.