Skip to content

Commit

Permalink
product cart controller
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Jul 14, 2024
1 parent 513ec7c commit c22e4eb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
4 changes: 3 additions & 1 deletion app/controllers/product_cart_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def show
def remove
item = @cart.product_cart_items.find_by(product_id: params[:product_id])
item.destroy if item
redirect_to( product_cart_path, notice: 'Item removed from cart')
@cart_items = @cart.product_cart_items.includes(:product)
render "destroy"
# redirect_to( product_cart_path, notice: 'Item removed from cart')
end

private
Expand Down
29 changes: 29 additions & 0 deletions app/views/product_cart/_cart.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="mx-auto w-3/4 py-4">
<h1 class="text-2xl font-bold mb-4">Your Cart</h1>

<% if @cart.product_cart_items.any? %>
<div class="space-y-4">
<% @cart.product_cart_items.each do |item| %>
<div class="flex items-center justify-between bg-default p-4 rounded-lg shadow border border-subtle">
<div>
<h3 class="font-semibold">
<%= link_to item.product.title, user_product_path(item.product.user.username, item.product), class: "text-link" %>
</h3>
<p>Quantity: <%= item.quantity %></p>
<p>Price: <%= number_to_currency(item.product.price) %></p>
</div>
<%= button_to "Remove", product_cart_remove_path(product_id: item.product.id), method: :delete, data: {turbo_method: :delete, turbo_confirm: "delete item?", turbo_frame: "_top"}, class: "bg-red-500 text-default px-4 py-2 rounded" %>
</div>
<% end %>
</div>

<div class="mt-8">
<p class="text-xl font-bold">Total: <%= number_to_currency(@cart.total_price) %></p>
<%= form_tag product_checkout_index_path, method: :post, data: {turbo: false } do %>
<%= submit_tag "Proceed to Checkout", class: "mt-4 bg-muted text-default px-6 py-3 rounded-lg font-bold cursor-pointer" %>
<% end %>
</div>
<% else %>
<p class="p-4 rounded-sm bg-orange-200 text-black">Your cart is empty.</p>
<% end %>
</div>
5 changes: 5 additions & 0 deletions app/views/product_cart/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= turbo_stream.replace("cart-show") do %>
<%= render "cart" %>
<% end %>
30 changes: 2 additions & 28 deletions app/views/product_cart/show.erb
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
<!-- app/views/product_cart/show.html.erb -->
<div class="mx-auto w-3/4 py-4">
<h1 class="text-2xl font-bold mb-4">Your Cart</h1>

<% if @cart.product_cart_items.any? %>
<div class="space-y-4">
<% @cart.product_cart_items.each do |item| %>
<div class="flex items-center justify-between bg-default p-4 rounded-lg shadow border border-subtle">
<div>
<h3 class="font-semibold">
<%= link_to item.product.title, user_product_path(item.product.user.username, item.product), class: "text-link" %>
</h3>
<p>Quantity: <%= item.quantity %></p>
<p>Price: <%= number_to_currency(item.product.price) %></p>
</div>
<%= button_to "Remove", product_cart_remove_path(product_id: item.product.id), method: :delete, data: {turbo_method: :delete, turbo_confirm: "delete item?", turbo_frame: "_top"}, class: "bg-red-500 text-default px-4 py-2 rounded" %>
</div>
<% end %>
</div>

<div class="mt-8">
<p class="text-xl font-bold">Total: <%= number_to_currency(@cart.total_price) %></p>
<%= form_tag product_checkout_index_path, method: :post, data: {turbo: false } do %>
<%= submit_tag "Proceed to Checkout", class: "mt-4 bg-muted text-default px-6 py-3 rounded-lg font-bold cursor-pointer" %>
<% end %>
</div>
<% else %>
<p class="p-4 rounded-sm bg-orange-200 text-black">Your cart is empty.</p>
<% end %>
<div id="cart-show">
<%= render "cart" %>
</div>
12 changes: 6 additions & 6 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@

</div>

<form class="mt-10">

<% render "variants_info" %>

<div class="flex flex-col space-x-4 mb-8">
<% if @product.available? %>
<%= button_to "Add to Cart", "#", method: :post, class: "mt-10 flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 px-8 py-3 text-base font-medium text-default hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" %>
<% if @product.available? %>
<%= button_to "Add to cart", product_cart_add_path(product_id: @product.id), method: :post, class: "button-sm-outline w-full text-center justify-center" %>
<% # button_to "Add to Cart", "#", method: :post, class: "mt-10 flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 px-8 py-3 text-base font-medium text-default hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" %>
<% else %>
<p class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-red-700 bg-red-100">Sold Out</p>
<% end %>
Expand All @@ -132,11 +132,11 @@
</div>
</div>
<div>
<!--<div>
<%= link_to "Back to Products", user_products_path(@product.user.username), class: "text-brand-600 hover:text-brand-900" %>
</div>
</div>-->
</form>
</div>
<div class="py-10 lg:col-span-2 lg:col-start-1 lg:border-r lg:border-muted lg:pb-16 lg:pr-8 lg:pt-6">
Expand Down

0 comments on commit c22e4eb

Please sign in to comment.