This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-08
Channels
- # aws-lambda (3)
- # beginners (27)
- # boot (4)
- # cljs-dev (26)
- # clojure (7)
- # clojure-spec (8)
- # clojure-uk (17)
- # clojurescript (1)
- # core-typed (2)
- # data-science (53)
- # datomic (24)
- # emacs (1)
- # fulcro (7)
- # luminus (1)
- # off-topic (2)
- # onyx (3)
- # pedestal (5)
- # planck (2)
- # portkey (50)
- # re-frame (15)
- # reagent (5)
- # reitit (2)
- # shadow-cljs (19)
- # tools-deps (15)
- # vim (2)
@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
@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?
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 https://purelyfunctional.tv/guide/database-structure-in-re-frame/@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
!