Based on an earlier conversation, I am refactoring an app with lots of inputs to use subscriptions. I could use some advice but I’ll put the details in 🧵 to save space.
My db is a nested data structure like this. NOTE: I do not want to refactor the app-db at this point. I’m stuck with it.
{:values {:user {:first-name "Luke"
:last-name "Johnson"}
:car {:make "Subaru"
:year "2017"}}
:errors {:user nil
:car {:year "Should be a number!"}}}
I currently have subs for each domain (e.g. user, car) values and errors. E.g.
(rf/reg-sub
::user
(fn [db]
(get-in db [:errors :user])))
I have individual subscriptions for each field like:
(rf/reg-sub
::last-name
:<- [::values/user]
:<- [::errors/user]
(fn [[users errors] [_sub-id overrides]]
(merge {:id :user-last-name
:label "Last Name"
:input-type :text
:path [:user :last-name]
:value (:last-name users)
:error (:last-name errors)}
overrides)))Is this sufficient? Reading the re-frame docs, it sounds like I should actually have separate subscriptions for each value and error which would be SO many more subs.
(rf/reg-sub
::last-name
:<- [::values/user-last-name]
:<- [::errors/user-last-name]
(fn [[value error] [_sub-id overrides]]
(merge {:id :user-last-name
:label "Last Name"
:input-type :text
:path [:user :last-name]
:value value
:error error}
overrides)))
I would probably have 1 sub for the user and 1 sub for the car and 1 sub for the errors