re-frame

Luke Johnson 2024-06-21T23:44:24.057229Z

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.

Luke Johnson 2024-06-21T23:44:43.237319Z

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!"}}}

Luke Johnson 2024-06-21T23:45:03.721599Z

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]))) 

Luke Johnson 2024-06-21T23:45:16.876559Z

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)))

Luke Johnson 2024-06-21T23:47:25.779219Z

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)))

2024-06-24T20:57:55.946659Z

I would probably have 1 sub for the user and 1 sub for the car and 1 sub for the errors