Fork me on GitHub
#fulcro
<
2018-10-03
>
Twan07:10:51

Hi, is there a canonical way to fetch data (like data-fetch/load) only if it not already in local state? We want to use this to refer to (maybe) already loaded data entries

claudiu07:10:17

as far as I know, there is no magic cache stuff implemented in fulcro. You have to look at the state and decide if some stuff is already loaded and tell df/load to elide it from the query or not issue the df/load at all.

wilkerlucio12:10:01

@twan you can write one yourself using :update-query, since most of the time you will have the ident of the entity you are going to load from, you can use that to get the current data on the DB and prune those parts out of query

tony.kay13:10:16

I don't think he's asking about query pruning... @twan I think perhaps the answer to your question is: yes, write a mutation (i typically name it something like ensure-thing-loaded) which: 1. Looks in app state. If the thing is not found, then issues df/load-action. 2. Always calls df/remote-load in the remote side of the mutation (which is a noop if there was no load action)

tony.kay13:10:56

Such a mutation is then safe to put in places like react lifecycle methods, or combined with mutations that do routing

(defmutation ensure-dropdown-options-loaded [_]
  (action [{:keys [state] :as env}]
    (let [options (get-in @state [:path :where :options :live])]
      (when-not (seq options)
        (df/load-action env ...))))
  (remote [env] 
    (df/remote-load env)))

pvillegas1216:10:18

How can I access the form-fields given a reference to a component?

pvillegas1216:10:41

(Use case: adding a new model to a form in a generic way)

pvillegas1216:10:43

Right now the only way to do this is setting the initial-state with the prop names I need in order to set up an empty model by using (prim/props component)

tony.kay17:10:04

@pvillegas12 in both kinds of form support the “form fields” current values are just your component’s fields…nothing special. Access them as you would without form support

pvillegas1218:10:52

Given

(defsc PersonForm [this props]
  {:query [:db/id :person/name {:person/phone-numbers (prim/get-query PhoneForm)}
           fs/form-config-join]
   :ident [:person/by-id :db/id]
   :form-fields #{:person/name :person/phone-numbers}}
  ...)
how can I do something like (get-form-fields PersonForm) ; => #{:person/name ... }

pvillegas1218:10:33

I want to generically populate a form without having a hard wired map like

(def person-tree
  { :db/id (prim/tempid)
    :person/name "Joe"
    :person/phone-numbers [{:db/id (prim/tempid) :phone/number "555-1212"}]})

tony.kay18:10:09

“populate a form” from a load, a data generator, or what? The basic approach is as documented here http://book.fulcrologic.com/#_initializing_a_tree_of_data

tony.kay18:10:37

you have to put the props in app state, as you normally would no matter what, and also run a add-form-config on it.

tony.kay18:10:07

You can do that all in a single custom mutation with the helper versions of merge-component and add-form-config*

pvillegas1218:10:23

Yes, that’s what I have right now but for a specific model

tony.kay18:10:24

or if you’re doing a load, then you can do a post mutation that just runs the add-form-config* helper on it

pvillegas1218:10:43

I want to infer the attributes a form needs from a stateful component

pvillegas1218:10:03

and do something like (zipmap model-attributes (repeat nil))

tony.kay18:10:28

oh…form fields adds a protocol to the component

tony.kay18:10:41

so you can just call the protocol to get the declared fields, if that’s what you mean

pvillegas1218:10:51

yes, I think that’s what I need

pvillegas1218:10:13

what is the way to call it?

tony.kay18:10:19

(form-fields this) should do it…I think it is also a static protocol, so you can call it with the class too

tony.kay18:10:34

(fs/form-fields Person)

👍 4
pvillegas1218:10:45

ah, the prim/get-ident fn only works when props are given to a component? 😛

tony.kay18:10:09

get-ident works on instaces OR classes with props…needs props one way or the other to calculate ID 🙂

tony.kay17:10:26

or perhaps you’re asking something else…I think it is pretty well covered in the book, isn’t it?

currentoor20:10:25

@wilkerlucio can the inspector work with advanced compilation on?

wilkerlucio20:10:15

I think so, you just have to ensure the preload is setup on the adv build, usually the compiler takes those out (shadow or figwheel), so you might have to call the initialization yourself

currentoor20:10:19

i was thinking it might be useful to have an alternative URL in prod that works with the inspector for debugging certain kinds of issues, i don’t care if that build gets bloated

wilkerlucio20:10:09

and make sure you call it before any apps are initialized

wilkerlucio20:10:21

I think it's not going to bloat, the client is very thin these days (most heavy things were move to the extension)

wilkerlucio20:10:11

the main reason to don't do it is because of the overhead you have to encode states to send to inspect, but bundle size addition is probably negligible

currentoor21:10:38

yeah makes sense

currentoor21:10:10

either way, it’s fine that my debug build is a little slow or worse in some types of performance