Skip to content

Instantly share code, notes, and snippets.

@holyjak
Last active February 10, 2020 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/eeb4341f85e824c040ee2b3a7af44705 to your computer and use it in GitHub Desktop.
Save holyjak/eeb4341f85e824c040ee2b3a7af44705 to your computer and use it in GitHub Desktop.
Fulcro mystery 2 - why isn't the initial state normalized in the DB?
(defsc Person [this {:person/keys [name] :as props}]
{:query [:ui/modified? :person/id :person/name :address/city :address/state]
:ident :person/id
:initial-state (fn [{:keys [id name city state]}] {:person/id id
:person/name name
:address/city city
:address/state state})}
(dom/li
(dom/h5 (pr-str props))))
(def ui-person (comp/factory Person {:keyfn :person/id}))
(defsc Root [this {people :people}]
{:query [:people [(comp/get-query Person)]]
#_#_:initial-state {:people [{:id 0 :name "Sally" :city "Nowhere" :state "GA"}
{:id 1 :name "Tom" :city "There" :state "OH"}]}
:initial-state (fn [_]
{:people [(comp/get-initial-state Person {:id 0 :name "Sally" :city "Nowhere" :state "GA"})
(comp/get-initial-state Person {:id 1 :name "Tom" :city "There" :state "OH"})]})}
(dom/section
(dom/h3 "Peeps")
(dom/ul
(map ui-person people))))
;;;; DB is stil not normalized:
;;{:people
;; [{:person/id 0,
;; :person/name "Sally",
;; :address/city "Nowhere",
;; :address/state "GA"}
;; {:person/id 1,
;; :person/name "Tom",
;; :address/city "There",
;; :address/state "OH"}],
;; :fulcro.inspect.core/app-id "app.ui.tmp-people/Root",
;; :fulcro.inspect.core/app-uuid
;; #uuid "10ea403a-5d18-4970-971c-a4ee64040cad",
;; :ui/routing-ready? true,
;; :com.fulcrologic.fulcro.application/active-remotes #{}}
;;
;; Expected: :eprson/id {0.., 1 ..} table and :people just a vec of idents

Solution

L14 is wrong, it should have been wrapped in {..} and thus a join: :query [{:people [(comp/get-query Person)]}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment