Matt's Roof Garden

Powered by 🌱Roam Garden

Shopping list sum price August 30th, 2021

searches all children for prices and sums everything up


could also check for DONE and skip those




(ns shopping-sum-v2
   [reagent.core :as r]
   [datascript.core :as d]
   [roam.datascript.reactive :as dr]
   [clojure.pprint :as pp]))

(defn flatten-block 
  "Flattens a blocks children into a flat list"
  [acc block]
  (reduce flatten-block
          (conj acc (dissoc block :block/children))
          (:block/children block)))

(defn find-child-refs
  "Returns all _refs for children blocks given a parent block uid"
  (flatten-block []
             @(dr/q '[:find (pull ?e [:block/string :block/uid {:block/children ...}]) .
                      :in $ ?uid
                      [?e :block/uid ?uid]

(defn filter-content [content]
  "finds blocks that contain a price eg. $40. Not acutally used just useful"
    #(re-seq #".*\$(\d+)(?:\.\d+)?" (:block/string %))
    (filter #(re-seq #".*\$(\d+)(?:\.\d+)?" (:block/string %)) content))


(def log (.-log js/console))

(defn sum-string [content]
  (->> content
    (re-seq #"\$\d+(?:\.\d+)?" )
    (map #(subs % 1) )
    (map int )
    (reduce +)

(defn main [{:keys [block-uid]} & args]
  [:div {:style{:margin-left "10px"}} "Total Spent: " 
   [:div {:style{
                 :display "inline-block"
                :border "2px solid #CD3838"
                  :padding "2px 4px"
                :color "#CD3838"
                :font-weight "bold"
    "$"(reduce + (map #(sum-string (:block/string %)) (find-child-refs block-uid)))

Referenced in