 (def app-state
   (r/atom {:cards {}}))
 
-(defn add-cards!
+(defn add-cards
   "Add several cards to the application state"
-  [cards]
+  [store cards]
   ; BUG - Keeping the :show-details at app-state means that rendering twice
   ; the component would lead to the GUI being updated at two places
-  ; => It cannot be in the component alone (if you want the expand all)
-  ;    But it should be in a state specific to the board (assoc container)
   (let [default-state {:show-details false}
         to-gui-card #(vector (:card-id %) (merge % default-state))]
-    (swap! app-state
-     #(update-in % [:cards] merge (map to-gui-card cards))
-  )))
+    (merge store (map to-gui-card cards))
+  ))
 
 (defn toggle-all-cards
   [cards]

  (fn []
    [:button.header-button
     {:type "button" :on-click #(dispatch :on-toggle-all)}
     "Expand all"]
  ))

(defn render-add-card

  ))

(defn render-app
  "[Stateful] Render the application" 
  [dispatch]
  (let [filter (r/atom "")
        card-renderer (render-card dispatch)
        column-rendered (render-column card-renderer dispatch)
        board-renderer (render-board column-rendered)]
    (fn [cards]
      [:div
       (render-filter filter)
       [render-add-card dispatch]
       [render-toggle-all dispatch]
       [board-renderer (filter-by-title @filter cards)]
      ])
    ))

(defn fetch-and-render-app
  "Render the app - adding a fetching of data when the DOM is mounted"
  []
  (let [cards (r/cursor app-state [:cards])
        card-store (store/reducer-store cards app-reducer)
        dispatch (store/dispatcher card-store)
        app-render (render-app dispatch)]
    (r/create-class
      {:component-did-mount #(fake/fetch-cards! (partial dispatch :initialize))
       :reagent-render (fn [] [app-render @cards])})
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

