Fork me on GitHub
#beginners
<
2023-01-20
>
Uli09:01:44

I'm using reagent for a small app. I'd like to persist parts of the "html" / hiccup in the database, since it may change. But I have no idea, how to persist it to a dabase and get it back rendered as valid hiccup. Do you see a way, how to persist "layouted text" in the database?

Daniel Craig15:01:50

are you using re-frame? just out of curiousity

Uli16:01:52

no, not even sure what re-frame is all about...

Uli16:01:43

@USDPTD3FY currently looking into it. looks very interesting, makes fun reading documentation, this one catched me:

Sure, right now, you're thinking "lazy sod - make a proper Computer Science-y diagram". But, no. Joe Armstrong says "don't break the laws of physics" - I'm sure you've seen the videos - and if he says to do something, you do it (unless Rich Hickey disagrees, and says to do something else).

Daniel Craig16:01:13

Reframe is nice 🙂. but since you aren't using it don't feel pressured to dive into it right now

Daniel Craig16:01:29

The reason I ask is that re-frame's philosophy is that rendered views i.e. hiccup should always be derived from data, and that the data should flow into the view functions and not the other way around

Daniel Craig16:01:05

so storing layouted text in the db would be against the recommendations of that particular tool

Daniel Craig16:01:36

but as you are not using re-frame, there is no need to obey this

escherize16:01:37

Are you using a single atom for your app-state?

Uli16:01:39

Oh, I dont feel pressure. It's really fun for me playing around in that domain and exploring all that stuff!

escherize16:01:50

or at least the piece that you want to persist

escherize16:01:25

then, this is a simple approach: you can pr-str the atom into local storage, and edn/read-string it back into data.

escherize16:01:02

does that make sense

Uli16:01:43

@U051GFP2V yes it does! Im gonna give that one a try.

escherize16:01:18

storage-atom persists its own state for you

Uli16:01:28

makes sense....

escherize16:01:41

keys and values have to be strings — I’d writ it like this with the string encoding/decoding

(ns localstorage
  (:require [clojure.edn :as edn]))

(defn set-item!
  "Set `key' in browser's localStorage to `val`."
  [key val]
  (.setItem (.-localStorage js/window) (pr-str key) (pr-str val)))

(defn get-item
  "Returns value of `key' from browser's localStorage."
  [key]
  (edn/read-string (.getItem (.-localStorage js/window) (pr-str key))))

(defn remove-item!
  "Remove the browser's localStorage value for the given `key`"
  [key]
  (.removeItem (.-localStorage js/window) (edn/read-string key)))

escherize16:01:10

then you can use arbitrary clojure data (but not funcitons / objects).

Andre Ulle19:01:37

I need to check if a context is true, and the way I know how to do it is through the clojure.core/match function, but I don't need any action, I just want to know if the context fits my conditions. I'm doing something like:

(defn fit-context? [event-name input]
  (match [event-name input]
         [: ({:context {:context/reason (_ :guard is-bla?)}
                                        :person             {:type :nice-person}}
                                       :guard [not-that?
                                               and-that?])]
         true
         :else false))
how could i do the same, without needing to explicitly return true or false

Andre Ulle21:01:23

I was adding unnecessary complexity for something very simple rsrs I use the and function for that