Fork me on GitHub
#re-frame
<
2018-07-07
>
p-himik01:07:11

@lwhorton That's because subscription handlers receive more than one argument. Your way of doing things actually should've been producing this result at all times when db doesn't have the required key. Consider (:a {} 7) - it returns 7. The value after the map is the default value, just like in get.

👍 4
Bravi20:07:26

hi everyone. i have a sortable component, that looks like this

(defn render
  "this function renders a sortable list.
  it will pass down draggable props to
  the component as a first argument."
  [{order :order}]
  (let [state (r/atom {:order order})]
    (fn [{:keys [items order on-reorder item-renderer container-class is-updating?]}]
      (let [items (vec items)]
        (into [:div.sortable-list {:class container-class}
               (when is-updating? [loading])]
              (for [[id position] (map vector (:order @state) (range))]
                (when-let [item (first (filter #(= id (:id %)) items))]
                  [item-renderer
                   {:key           id
                    :class         [(sortable-list__item)
                                    (when is-updating?
                                      (sortable-list__item-updating))
                                    (when (= id (:drag-index @state))
                                      (sortable-list__item-dragging))]
                    :draggable     true
                    :on-drag-start #(swap! state assoc :drag-index id)
                    :on-drag-over  (fn [e]
                                     (.preventDefault e)
                                     (swap! state assoc
                                            :drag-over position)

                                     (swap! state update
                                            :order change-position
                                            (:drag-over @state)
                                            (:drag-index @state)))
                    :on-drag-leave #(swap! state assoc :over :empty)
                    :on-drag-end   (fn []
                                     (swap! state dissoc :drag-index :drag-over)
                                     (when on-reorder (on-reorder (:order @state))))}
                   item])))))))
basically it receives a map that has an order. and then it saves it in a local state. and once the re-order happens, on-reorder function is called that saves the new order on the BE. it all works fine until I add a new item to this list. the problem is that because this is a second type reagent component and order is saved outside, it doesn’t get updated. what could be a solution to such case?