                :onClick #(swap! state merge
                           {:show-import? false}
                           (when (react/call :did-load-data? (@refs "data-import"))
-                            {:entities-loaded? false}))}
+                            {:entity-map false}))}
            "< Back to Data List"]]
          [import-data/Page {:ref "data-import" :workspace-id (:workspace-id props)}]]
-        (:entities-loaded? @state) [EntitiesList {:entities (:entities @state)}]
-        (:error @state) (style/create-server-error-message (get-in @state [:error :message]))
+        (:entity-map @state) [EntitiesList {:entity-map (:entity-map @state)}]
+        (:error @state) (style/create-server-error-message (:error @state))
         :else [:div {:style {:textAlign "center"}} [comps/Spinner {:text "Loading entities..."}]])
       (when-not (:show-import? @state)
         [:div {:style {:margin "1em 0 0 1em"}}

     (react/call :load-entities this))
   :component-did-update
   (fn [{:keys [this state]}]
     (when-not (or (:entities-loaded? @state) (:error @state))
       (react/call :load-entities this)))
   :load-entities
   (fn [{:keys [state props]}]
     (utils/call-ajax-orch
       (list-all-entities-path (:workspace-id props) "sample")
       {:on-success (fn [{:keys [parsed-response]}]
                      (swap! state assoc :entities-loaded? true :entities parsed-response))
        :on-failure (fn [{:keys [status-text]}]
                      (swap! state assoc :error {:message status-text}))
        :mock-data (create-mock-entities)}))})

(defn render [workspace]

    [org.broadinstitute.firecloud-ui.common.table :as table]
    [org.broadinstitute.firecloud-ui.common.style :as style]
    [org.broadinstitute.firecloud-ui.page.import-data :as import-data]
    [org.broadinstitute.firecloud-ui.paths :refer [get-entities-by-type-path]]
    [org.broadinstitute.firecloud-ui.utils :as utils]))


(defn- create-mock-entities []
  (map
    (fn [i]
      {:entityType (rand-nth ["sample" "participant"])
       :name (str "entity" (inc i))
       :attributes {}})
    (range (rand-int 20))))

(react/defc EntitiesList
  {:get-initial-state
   (fn [{:keys [props]}]
     {:entities (get (:entity-map props) (first (keys (:entity-map props))))})
   :render
   (fn [{:keys [props state refs]}]
     [:div {}
      [:div {:style {:padding "0 0 0.5em 1em"}}
       (style/create-form-label "Select Entity Type")
       (style/create-select
         {:style {:width "50%" :minWidth 50 :maxWidth 200} :ref "filter"
          :onChange #(let [value (-> (@refs "filter") .getDOMNode .-value)
                           entities (get-in props [:entity-map value])]
                      (swap! state assoc :entities entities :entity-type value))}
         (keys (:entity-map props)))]
      (if (zero? (count (:entities @state)))
        (style/create-message-well "No entities to display.")
        (let [attribute-keys (apply union (map (fn [e] (set (keys (e "attributes")))) (:entities @state)))]
          [table/Table
           {:key (:entity-type @state)
            :columns (concat
                       [{:header "Entity Type" :starting-width 100 :sort-by :value}
                        {:header "Entity Name" :starting-width 100 :sort-by :value}]
                       (map (fn [k] {:header k :starting-width 100 :sort-by :value}) attribute-keys))
            :data (map (fn [m]
                         (concat
                           [(m "entityType")
                            (m "name")]
                           (map (fn [k] (get-in m ["attributes" k])) attribute-keys)))
                    (:entities @state))}]))])})


(react/defc WorkspaceData
