Fork me on GitHub
#re-frame
<
2018-07-08
>
p-himik04:07:41

@bravilogy This particular implementation doesn't use re-frame in any way. If you want to create a re-frame component, you should get order from a subscription and change it via a call to dispatch. So, the order will not be stored in a local state, but outside where you could change it when you add a new item. If you want a reagent component, you may want to do roughly the same - store order only outside, do not store it in state, call from the component some functions that change this atom. Also, you appear to be using :drag-over and :over in state - a typo?

Bravi08:07:35

@p-himik yes, typo in code thanks! haha i tried it with re-frame as well but something goes wrong between on-drag-over and on-drag-leave. I’m wondering if there could be a sync problem. basically the order array is being appended by a bunch of nulls. I might try using dispatch-sync to make sure it is updated

p-himik08:07:29

@bravilogy dispatch only from :on-drag-end - it's the only place that actually changes order. In :on-drag-over you can use some local state - just make sure to propagate this state to the global one when the reordering is over.

Bravi09:07:52

I divided it into two parts. local state will now save drag-index and drag-over, and the order will be outside of this component

Bravi09:07:01

will see how it goes now 🙂

Bravi09:07:03

thank you

kennytilton10:07:48

@bravilogy Hey, this is OT but I noticed (first (filter #(= id (:id %)) items)…would some work there, stopping as soon as it found a match?

Bravi10:07:16

but doesn’t some return boolean? I need to get the actual item there..

p-himik11:07:05

You could change to some by changing the predicate to something like #(when (= id (:id %)) %), but that's hardly worth it.

valtteri11:07:44

My personal preference is to avoid (first (filter ... coll)) by transforming the list into a map where key is the id or whichever attribute you want to use for lookup and item is the value.

(defn index-by [idx-fn coll]
  (into {} (map (juxt idx-fn identity)) coll))

(def items [{:id 1 :name "cat"}
            {:id 2 :name "dog"}])

(index-by :id items)
=> {1 {:id 1, :name "cat"}, 2 {:id 2, :name "dog"}}

(index-by :name items)
=> {"cat" {:id 1, :name "cat"}, "dog" {:id 2, :name "dog"}}
I think I stole the “indexed entity” idea from here https://purelyfunctional.tv/guide/database-structure-in-re-frame/

p-himik11:07:58

Heh, yeah, I use it too, and most likely started to use it after the same article.

p-himik11:07:29

Oh maybe earlier - O(n) is always hard to look at when there's an obvious O(1).

kennytilton12:07:59

@bravilogy pwuahhaa--my native language is Lisp, where some indeed returns the first non-nil. My bad, but I still don’t like filtering the whole list. @p-himik’s idea might not be so unpalatable if it were part of one’s toolkit. It falls in the if-let/when-let category — “Don’t just decide my conditionial, bind me the truthy value while yer at it”.

kennytilton12:07:14

A good name might be something. 🙂 Wait… somewhat!