                           (map om/value))
                     page-ids)]
     ;; TODO: Assert?
-    (when-not (<= 0 target-idx (dec (count existing-pages)))
+    ;; NOTE: We need to handle (= idx page-count) to be able to insert pages at the end
+    (when-not (<= 0 target-idx (count existing-pages))
       (throw (ex-info (str "Got invalid target-idx:" target-idx)
                       {:idx target-idx
                        :column-pages existing-pages

                              :store-idx! (fn [idx]
                                            (om/set-state! owner :drop-idx idx))}
                       :fn (fn [page]
                             (mark-page-selected page (:selected selection)))}))]]))

(defn ^:private inbox-handle-drop! [state owner page-cache target source page-ids target-idx]
  (let [columns (om/get-state owner :columns)

    (assert (number? document-id))
    (api/fetch-documents! [document-id])))

(defn- inbox-page-drag-over [page owner store-idx! e]
  ;; Calculcate the center position for this image and set idx to the
  ;; current page or the page after, respectively
  (let [rect (.getBoundingClientRect (.-currentTarget e))
        top (.-top rect)
        height (.-height rect)
        local (Math/abs (- (.-clientY e) top))
        idx (if (>= (/ height 2) local)
              (:idx page)
              (inc (:idx page)))]
    (store-idx! idx)))

;;; NOTE: We need on-drag-start in `inbox-column' and in
;;; `inbox-column-page' (the latter to handle selection-updates when
;;; dragging). We bubble it from `inbox-column-page', adding its own
;;; `:id' and use that id to update the selection information.
(ui/defcomponent inbox-column-page [page owner {:keys [page-click! store-idx!]}]
  (render [_]
    [:li.page {:draggable true
               :class (let [selected? (:selected? page)]
                        [(when selected? "selected")
                         (when (and (not selected?) (:dragover? page)) "dragover")])
               :on-drag-start (fn [e]
                                (println "inbox-column-page")
                                (.setData e.dataTransfer "application/x-pepa-page" (:id page)))
               :on-drag-over (partial inbox-page-drag-over page owner store-idx!)
               :on-click (fn [e]
                           (page-click!
                            (selection/event->click (:id page) e))
