diff --git a/app/assets/javascripts/ListViewPage.js b/app/assets/javascripts/ListViewPage.js
index 48c4b6b1f52be6c1dc0b30c5cf0cb3ee5775e8b2..254a7e0f1b55154c8adb5d51c8d6f8257fceee6c 100644
--- a/app/assets/javascripts/ListViewPage.js
+++ b/app/assets/javascripts/ListViewPage.js
@@ -1,28 +1,35 @@
let nextChangeIds = [];
let nextChangeQuants = [];
+let nextChangeAcqs = [];
let timer;
-function performQuantityUpdate(){
+function performUpdate(){
$.ajax({
- url: "/change_quantity",
+ url: "/change_prods",
data: {
"product_id": nextChangeIds,
- "quantity": nextChangeQuants
+ "quantity": nextChangeQuants,
+ "acquired": nextChangeAcqs
},
type: "get"
});
nextChangeIds = [];
nextChangeQuants = [];
+ nextChangeAcqs = [];
timer = null;
}
-function updateProductQuantity(field, id){
+function updateProduct(field, id, col){
let present = false;
for(let i = 0; i < nextChangeIds.length; i++){
if(nextChangeIds[i] === id){
- nextChangeQuants[i] = field.value;
+ if(col === "quantity"){
+ nextChangeQuants[i] = field.value;
+ } else if(col === "acquired"){
+ nextChangeAcqs[i] = field.checked ? 1 : 0;
+ }
present = true;
break;
}
@@ -30,10 +37,19 @@ function updateProductQuantity(field, id){
if(!present){
nextChangeIds.push(id);
- nextChangeQuants.push(field.value);
+
+ if(col === "quantity"){
+ nextChangeQuants.push(field.value);
+ nextChangeAcqs.push("");
+ } else if(col === "acquired"){
+ nextChangeQuants.push("");
+ nextChangeAcqs.push(field.checked ? 1 : 0);
+ }
+
+
}
if(!timer){
- timer = setTimeout(performQuantityUpdate, 5000);
+ timer = setTimeout(performUpdate, 5000);
}
}
\ No newline at end of file
diff --git a/app/controllers/listview_controller.rb b/app/controllers/listview_controller.rb
index 3b692bb273244a62fc5ee68c42d01d0ad30fb74b..75af7643b3a2a160af3140cd4331581fbbd2f8e6 100644
--- a/app/controllers/listview_controller.rb
+++ b/app/controllers/listview_controller.rb
@@ -1,12 +1,23 @@
class ListviewController < ApplicationController
- def change_quantity
+ def change_prods
changes = Hash[]
(0..params[:product_id].length - 1).each do |i|
- changes[Integer(params[:product_id][i])] = { 'quantity' => params[:quantity][i] }
+ change = Hash[]
+
+ unless params[:quantity].nil? || params[:quantity][i].blank?
+ change[:quantity] = params[:quantity][i]
+ end
+
+ unless params[:acquired].nil? || params[:acquired][i].blank?
+ change[:acquired] = params[:acquired][i]
+ end
+
+ changes[Integer(params[:product_id][i])] = change
end
- puts changes.keys
+ puts changes.inspect
+
Product.update(changes.keys, changes.values)
redirect_to listview_path
end
diff --git a/app/views/listview/listview.html.haml b/app/views/listview/listview.html.haml
index 792eed4d747504347dc659d518d6299964912d01..3e17fcc4a1762383d8725527365b21de7d4833e5 100644
--- a/app/views/listview/listview.html.haml
+++ b/app/views/listview/listview.html.haml
@@ -1,12 +1,12 @@
%h1= @name
-
- Product.list(@list_id).all.each do |product|
%p= product.name
- %input{type: "number", value: product.quantity, min: 0, onchange: "updateProductQuantity(this, " + String(product.id) + ")"}
+ %input{type: "number", value: product.quantity, min: 0, onchange: "updateProduct(this, " + String(product.id) + ", 'quantity')"}
+ %input{type: "checkbox", value: product.acquired, onchange: "updateProduct(this, " + String(product.id) + ", 'acquired')"}
= form_tag del_prod_path, remote: true, method: 'post' do
=hidden_field_tag :product_id, product.id
diff --git a/config/routes.rb b/config/routes.rb
index 6204cb4d98f196c124196e68bec4f4cdb390e3ad..fac329957d51459f59a8dd620605d0b4cc859781 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -11,7 +11,7 @@ Rails.application.routes.draw do
get 'listview', to: 'listview#listview'
post 'new_prod', to: 'listview#new_prod'
post 'del_prod', to: 'listview#del_prod'
- get 'change_quantity', to: 'listview#change_quantity'
+ get 'change_prods', to: 'listview#change_prods'
get 'contact', to: 'contact#contact'
post 'sendmessage', to: 'contact#sendmessage'