Fork me on GitHub

@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?


@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


@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.


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


will see how it goes now 🙂


thank you


@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?


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


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


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


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


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


@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”.


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