summaryrefslogtreecommitdiff
path: root/templates.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'templates.fnl')
-rw-r--r--templates.fnl54
1 files changed, 32 insertions, 22 deletions
diff --git a/templates.fnl b/templates.fnl
index 53b5c0c..b80576c 100644
--- a/templates.fnl
+++ b/templates.fnl
@@ -110,7 +110,7 @@
[:NO-ESCAPE "(" (lib.format-price product.price-per)
"₽ за 1 грамм)"]]))]))
-(fn add-to-basket-form [product classes redirect-url]
+(fn add-to-basket-form [product basket classes redirect-url]
(fn quantity-steps [stock step]
(assert (< 0 step) "step must be greater than 0")
(var result [])
@@ -120,27 +120,37 @@
(set first (+ first step)))
result)
- (var quantity-options [])
- (var no-stock? false)
- (let [piece? (= product.packaging :piece)]
- (if (< 0 product.stock)
- (each [_ q (ipairs (quantity-steps product.stock (if piece? 1 50)))]
- (table.insert
- quantity-options
- (HTML
- [:option {:value (tostring q)}
- (.. (lib.format-price (* product.price-per q))
- "₽ за " q (if piece? " шт." " гр."))])))
- (do
- (table.insert quantity-options (HTML [:option {:value "0"} "Товар закончился"]))
- (set no-stock? true))))
-
- (HTML
- [:form {:method "POST" :action "/shop/cart/add" :class classes}
- [:input {:type "hidden" :name "name" :value product.name}]
- [:input {:type "hidden" :name "redirect-url" :value redirect-url}]
- [:select {:name "quantity"} (table.concat quantity-options)]
- (if no-stock? "" (HTML [:button {:type "submit"} "Добавить"]))]))
+ (var in-basket-quantity nil)
+ (each [_ basket-item (pairs basket) &until in-basket-quantity]
+ (when (= product.name basket-item.name)
+ (set in-basket-quantity basket-item.quantity)))
+
+ (var quantity-options [])
+ (var out-of-stock? false)
+ (let [piece? (= product.packaging :piece)]
+ (if (< 0 product.stock)
+ (each [_ q (ipairs (quantity-steps product.stock (if piece? 1 50)))]
+ (table.insert
+ quantity-options
+ (HTML
+ [:option (fn [] {:value q
+ :selected (= in-basket-quantity q)})
+ (.. (lib.format-price (* product.price-per q))
+ "₽ за " q (if piece? " шт." " гр."))])))
+ (do
+ (table.insert quantity-options (HTML [:option {:value "0"} "Товар закончился"]))
+ (set out-of-stock? true))))
+
+ (local disabled (or out-of-stock? in-basket-quantity))
+
+ (HTML
+ [:form {:method "POST" :action "/shop/cart/add" :class classes}
+ [:input {:type "hidden" :name "name" :value product.name}]
+ [:input {:type "hidden" :name "redirect-url" :value redirect-url}]
+ [:select (fn [] {:name "quantity" :disabled disabled})
+ (table.concat quantity-options)]
+ [:button (fn [] {:type "submit" :disabled disabled})
+ (if in-basket-quantity "В корзине" "Добавить")]]))
(fn order-lines [order-lines]
(HTML