Fork me on GitHub
#fulcro
<
2021-02-13
>
Alex17:02:03

Am I doing something wrong? I'm unable to get fs/dirty? to print true when I make changes to the form. Or when I try to get fs/dirty-fields I get an empty map as if nothing changed.

(defsc AccountItemForm
  "User Account Edit Form"
  [this {:account/keys [id name email active? editing?] :as props}]
  {:query [:account/editing? :account/id :account/name :account/email :account/active? fs/form-config-join]
   :ident :account/id
   :form-fields #{:account/name :account/email :account/active?}}
  (let [form-dirty? (fs/dirty? (prim/props this))]
    (println "Form DIrty?" form-dirty? (fs/dirty? props))
    (dom/tr :.ui.form {:classes [(when form-dirty? "warning")]}
            (dom/td (dom/input {:value name
                                :name :account/name
                                :onChange #(m/set-string! this :account/name :event %)}))
            (dom/td (dom/input {:value email
                                :name :account/email
                                :type "email"
                                :onChange #(m/set-string! this :account/email :event %)}))
            (dom/td (dom/input {:checked active? :name :account/active? :type "checkbox"}))
            (dom/td (dom/div
                     (dom/button {:onClick
                                  #(prim/transact! this `[(app.model.account/submit-account-changes {:account-id ~id :delta ~(fs/dirty-fields props true)})])}
                                 "Save")
                     (dom/button {:onClick
                                  #(prim/transact! this `[(app.model.account/cancel-account-edit {:account-id ~id})])}
                                 "Cancel"))))))

Jakub Holý (HolyJak)20:02:31

I think you must first mark the changed fields as "done". Or perhaps this only applies to validation checks?

Jakub Holý (HolyJak)20:02:57

Remember UI = f(data) so look at what data and form data is in the DB. Also, is the initial form state added correctly when adding account data to the DB?

henrik22:02:33

If you use this component to load data, try adding

{:pre-merge (fn [{:keys [data-tree current-normalized]}]
              (merge current-normalized (fs/add-form-config AccountItemForm data-tree)))}

henrik22:02:24

Other than potentially that, I can't see anything that sticks out.

Alex06:02:46

Thanks for the suggestions guys, I'll keep on digging.

Alex06:02:16

@U0522TWDA Yes the db contains the pristine data of what account I'm trying to edit. Initially they both start as the same state. When I start editing the record in my db, not the form one, changes to w/e I'm typing.

Jakub Holý (HolyJak)11:02:39

? Huh? Have you 2 different things you are trying to edit? I'm more interested in the form state data than the entity's own data because that is used in fs/dirty

Alex15:02:38

Okay I think I explained it badly, here's a screenshot of my entity and the entry that's made in forms-by-ident.

Alex15:02:36

I made edits and they show up on the entity while the form pristine state remains the same. I looked at the fulcro demo for the phone numbers and that one also happens to do that.

Jakub Holý (HolyJak)17:02:18

I see. Perhaps look at how the dirty function works to see what data it looks at and why it might not be doing what you expect? Perhaps also try to call it from the REPL instead of just on render in the body?

Jakub Holý (HolyJak)17:02:50

Does transacting (fs/mark-complete! ...) has any effect on the output of dirty?

Alex03:02:19

@U0522TWDA thank you for the suggestions and sorry for the late reply. I'll be trying this out on the coming days.