diff --git a/app/assets/javascripts/orders.js.coffee b/app/assets/javascripts/orders.js.coffee index 1a92dfe..c881b24 100644 --- a/app/assets/javascripts/orders.js.coffee +++ b/app/assets/javascripts/orders.js.coffee @@ -3,23 +3,23 @@ # You can use CoffeeScript in this file: http://coffeescript.org/ ready = -> $('.btn-inc').on 'click', -> - increment(this, 1) + increment($(this), 1) $('.btn-dec').on 'click', -> - increment(this, -1) + increment($(this), -1) + increment = (button, n) -> + # Fix the counter counter = $(button).closest('.form_row').find('.row_counter') - newCount = parseInt(counter.val()) + n - counter.val(Math.max(newCount, 0)) - calculatePrice() + counter.val(parseInt(counter.val()) + n) -calculatePrice = -> - price = 0 - $('#form_products').children().each(-> - price += parseInt($(this).find('.price').html()) * parseInt($(this).find('.row_counter').val()) - ) - $('#order_total_price').val(price) + # Enable or disable the dec button + counter.parent().find('.btn-dec').prop("disabled", counter.val() == '0'); + + # Update the price + oldVal = parseFloat($('#order_total_price').val()) + $('#order_total_price').val(parseFloat(oldVal + counter.parent().find('.price').val() * n).toFixed(2)) $(document).ready(ready) $(document).on('page:load', ready) diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index c67ef86..fbd81aa 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -28,11 +28,6 @@ $gray-medium-light: #eaeaea; box-sizing: border-box; } - -.thumbnail { - height: 150px; -} - /* miscellaneous */ .nowrap { white-space: nowrap; diff --git a/app/assets/stylesheets/orders.css.scss b/app/assets/stylesheets/orders.css.scss index 7150136..7415069 100644 --- a/app/assets/stylesheets/orders.css.scss +++ b/app/assets/stylesheets/orders.css.scss @@ -1,16 +1,3 @@ // Place all the styles related to the Orders controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ - -.form_products { - border-width: 1px; - border-color: #000; - border-style: solid; - margin-bottom: 2px; -} - -.form_total { - border-width: 1px; - border-color: #000; - border-style: solid; -} diff --git a/app/views/order_products/_form_row.html.erb b/app/views/order_products/_form_row.html.erb index d70f60c..33ddf71 100644 --- a/app/views/order_products/_form_row.html.erb +++ b/app/views/order_products/_form_row.html.erb @@ -1,10 +1,19 @@ -
- <%= image_tag f.object.product.avatar %> - <%= content_tag(:span, euro(f.object.product.price), class: "price") %> -
- <%= render 'btn_dec', id: f.object.product_id %> - <%= f.text_field :count, class: 'form-control row_counter', value: 0 %> - <%= render 'btn_inc', id: f.object.product_id %> - <%= f.hidden_field :product_id %> +
+
+
+ <%= image_tag f.object.product.avatar %> +
+

<%= f.object.product.name %> - <%= content_tag :span, euro(f.object.product.price), class: "price" %>

+

+

+ <%= render 'btn_dec', id: f.object.product_id %> + <%= f.text_field :count, class: 'form-control row_counter', value: 0 %> + <%= f.hidden_field :price, value: f.object.product.price, class: :price %> + <%= render 'btn_inc', id: f.object.product_id %> + <%= f.hidden_field :product_id %> +
+

+
+
diff --git a/app/views/order_products/_total_price.html.erb b/app/views/order_products/_total_price.html.erb index afac642..788c006 100644 --- a/app/views/order_products/_total_price.html.erb +++ b/app/views/order_products/_total_price.html.erb @@ -1,2 +1,2 @@ <%= f.label :total_price %> -<%= f.number_field :total_price %> +<%= f.number_field :total_price, step: :any %> diff --git a/app/views/orders/new.html.erb b/app/views/orders/new.html.erb index 8cf39dc..0dacd8a 100644 --- a/app/views/orders/new.html.erb +++ b/app/views/orders/new.html.erb @@ -1,16 +1,21 @@ -

<%= @user.name %>

+

Order for <%= @user.nickname %>

-<%= form_for( @order, url: user_orders_path(@user)) do |f| %> +<%= form_for @order, url: user_orders_path(@user) do |f| %> <%= render 'application/errors', model: @order %> -
- <%= f.fields_for :order_products do |op_field| %> - <%= render 'order_products/form_row', f: op_field %> - <% end %> +
+
+ <%= f.fields_for :order_products do |op_field| %> + <%= render 'order_products/form_row', f: op_field %> + <% end %> +
-
- <%= render 'order_products/total_price', f: f %> - <%= f.submit "Order", class: "btn btn-primary " %> + +
+
+ <%= render 'order_products/total_price', f: f %> + <%= f.submit "Order", class: "btn btn-primary " %> +
<% end %>