summaryrefslogtreecommitdiff
path: root/pages/shop/_product/index.fnl
blob: c3c296866ad1caeb472417d78a1333f7a6ebf11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(import-macros {:compile-html HTML} :macros)
(local templates (require :templates))
(local dicts (require :dicts))
(local lib (require :lib))

(fn text->html [text]
  (assert (= (type text) "string"))
  (var result "")
  (var from 1)
  (var to (text:find "\n%s*\n%s*" from))
  (while to
    (set result (.. result "<p>" (text:sub from (- to 1)) "</p>\n"))
    (set from (+ to 2))
    (set to (text:find "\n%s*\n%s*" from)))
  (.. result "<p>" (text:sub from) "</p>"))

(fn find-product [db name]
  (.
   (_G.must
    (luna.db.query-assoc
      db
      "SELECT products.name,
              products.title,
              products.description,
              products.price_per AS \"price-per\",
              products.type,
              products.stock,
              products.packaging,
              products.published,
              products.image1,
              products.image2,
              products.image3,
              products.image4,
              products.image5
        FROM products
        WHERE products.name = ?"
      [name]))
   1))

(fn content [product authenticated?]
  (local images [])
  (for [i 1 5]
    (table.insert images (. product (.. "image" i))))

  [(HTML
    [:div {:class "side"}
     (templates.header "/shop" authenticated?)])
   (HTML
    [:div {:class "content"}
     [:div {:class "mb-1"} [:a {:href "/shop"} "⟵ Обратно к списку"]]

     [:div {:class "product-page-layout"}
      [:div {}
        [:h2 {:class "product-page-title"}
          product.title
          (if authenticated?
            (HTML [:a {:style "font-size: 1rem; margin-left: 0.75rem;"
                       :href (.. "/shop/" product.name "/edit")}
                      "% Редактировать"])
            "")]
        [:section {:class "mb-2"}
          [:div {:class "mb-0-5" :style "font-style: italic;"}
            (or (dicts.label dicts.tea-type product.type) product.type) ", "
            (if (= product.packaging "piece")
              (HTML [:strong {} product.price-per "₽ за 1 шт. "])
              (HTML [:span {}
                      [:strong {} (* 50 product.price-per) "₽ за 50 грамм "]
                      "(" product.price-per "₽ за 1 грамм)"]))]]
        (let [link (.. "/static/files/" product.image1)]
          (HTML [:a {:href link :target "_blank"}
                 [:img {:class "product-page-img-mobile mb-2"
                        :src (.. link "-thumbnail.jpg")}]]))
        [:div {:class "mb-2"} "~~~"]
        [:NO-ESCAPE (text->html product.description)]]
      [:div {:class "product-page-imgs"}
        (table.concat
         (icollect [idx image (ipairs images)]
           (let [link (.. "/static/files/" image)]
             (HTML [:a {:href link :target "_blank"}
                    [:img {:class "product-page-img" :src (.. link "-thumbnail.jpg")}]]))))]]])])

(fn render [request db authenticated?]
 (let [product (find-product db request.params._product)]
   (if (and product (or product.published authenticated?))
    (values 200 {} (templates.base (content product authenticated?)))
    (values 404 {} "not found"))))

{: render}