Fork me on GitHub
#fulcro
<
2018-07-05
>
wilkerlucio00:07:28

ah, yeah, I was thinking on pmutate, but yes, for load its fine on post-mutation (still feels dirty)

tony.kay04:07:36

@wilkerlucio I agree...I don't like the chained stuff. But in certain circumstances it is unavoidable.

fatihict12:07:56

Thanks for the help. I missed the warning section in the docstring đŸ˜„, I managed to solve my case with the setTimeout trick

edward.scott14:07:08

Looking for a way to detect mouse clicks outside of a component so I can close a drop down. A react approach is described here https://www.jamestease.co.uk/blether/detect-clicks-outside-element-with-react-components. Unfortunately, I cannot see a way of passing the same function to both the addEventListener and removeEventListener in the defsc lifecycle methods and have the defsc this supplied to the function (so I can trigger a mutation). Any suggestions?

wilkerlucio14:07:07

@mandor2017 what I do on those cases is have some react components that can attach/dettach events on the DOM directly, so you can capture clicks in the body, something like this:

wilkerlucio14:07:10

(fp/defsc DomListener [this _]
  {
   :componentDidMount
   (fn []
     (let [{::keys [target event action]} (fp/props this)
           target (get-target target)]
       (assert event "You must provide an event to dom-listener")
       (gobj/set this "handler" action)
       (if target
         (.addEventListener target event action))))
   
   :componentWillUnmount
   (fn []
     (if-let [handler (gobj/get this "handler")]
       (let [{::keys [target event]} (fp/props this)
             target (get-target target)]
         (if target
           (.removeEventListener target event handler)))))}

  (dom/noscript nil))

(def dom-listener (fp/factory DomListener))

wilkerlucio14:07:01

so then in your component you cna do: (dom-listener {::target js/document.body ::event "click" ::action (fn [] (do-something))}

tony.kay14:07:06

@mandor2017 Another trick is to just have your app render a top-level div that covers the screen, and attach event handlers via React to that.

tony.kay14:07:34

But if you just want to make a method on the component, use the :protocols option to add a method:

(defsc Boo [t p]
  {:protocols [Object
               (handle-click [this] . ..)]}
  ...)

edward.scott15:07:43

@tony.kay Perfect. Thank you.

tony.kay15:07:54

you could also use componentDidMount (componentWillMount is being deprecated, so might as well get used to it) to put a function into the object

tony.kay15:07:12

that’s most often what I do when I need a shared saved function for the instance lifetime:

tony.kay15:07:30

(gobj/set this "handle-click" (fn [...] ...))

tony.kay15:07:52

that also lets you “close over” this, so you shouldn’t need any js bind stuff

wilkerlucio15:07:18

@tony.kay juts saying, componentWillMount is going off, but componentWillUnmount stays, so you can still have element handlers 🙂

currentoor20:07:43

should prim/integrate-ident have a :remove option? for removing an ident from a list

tony.kay21:07:09

@wilkerlucio actually you did say about what I said...didn't mean to sound contradictory 🙂

tony.kay21:07:26

@currentoor bad naming...I would be fine with a remove-ident helper

tony.kay21:07:03

and some GC support in general...say you want to name the tables to clean...`(clean-ident #{table-names} ident)`

tony.kay21:07:20

a recursive transform that just wipes them out

tony.kay21:07:58

probably both...one to clear them from a list, but another to just scan recursively and get rid of them

tony.kay21:07:18

perhaps the table names could be optional, in which case it cleans all of state

currentoor21:07:01

@tony.kay i'll make an issue and submit a PR later simple_smile

currentoor21:07:51

yeah for sure, thanks!

currentoor21:07:58

i guess that would need to be an optional thing right?

currentoor21:07:19

cuz we don't have a way to declare component entities like datomic

currentoor21:07:38

so we don't know if deletes should go across links?

currentoor21:07:12

now i'm thinking it might not be worth it, or we could have a third deep-clean-ident function

currentoor21:07:58

or deep-clean-entity sounds a little better, and it won't take in table names