Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns cards.union-follow-on-reads
(:require [devcards.core :as dc :refer-macros [defcard dom-node]]
[om.dom :as dom]
[om.next :as om :refer [defui]]
[om.util :as util]))
(defui ^:once ActiveUsersTab
static om/IQuery
(query [this] [:which-tab])
static om/Ident
(ident [this props]
[(:which-tab props) :tab])
Object
(render [this]
(println (om/path this))
(println (om/full-query this))
(dom/h3 nil (str "ActiveUser path: " (om/path this)))))
(defui ^:once HighScoreTab
static om/IQuery
(query [this] [:which-tab])
static om/Ident
(ident [this props]
[(:which-tab props) :tab])
Object
(render [this]
(println (om/path this))
(println (om/full-query this))
(dom/h3 nil (str "HighScore path: " (om/path this)))))
(def ui-active-users-tab (om/factory ActiveUsersTab))
(def ui-high-score-tab (om/factory HighScoreTab))
(defui ^:once SettingsViewerUnion
static om/IQuery
(query [this]
{:active-users (om/get-query ActiveUsersTab)
:high-score (om/get-query HighScoreTab)})
static om/Ident
(ident [this props]
[(:which-tab props) :tab])
Object
(componentDidMount [this]
(let [{:keys [switch-settings-tab!]} (om/get-computed this)]
(switch-settings-tab! [:high-score :tab])))
(render [this]
(let [{:keys [switch-settings-tab!]} (om/get-computed this)]
(dom/div
nil
(case (:which-tab (om/props this))
:active-users (ui-active-users-tab (om/props this))
:high-score (ui-high-score-tab (om/props this)))))))
(def ui-settings-viewer (om/factory SettingsViewerUnion))
(defui ^:once SettingsTab
static om/IQuery
(query [this] [:which-tab
{:settings-viewer-tab (om/get-query SettingsViewerUnion)}])
static om/Ident
(ident [this props]
[(:which-tab props) :tab])
Object
(render [this]
(let [{:keys [settings-chooser settings-viewer-tab]} (om/props this)
switch-settings-tab! (fn [tab] (om/transact! this `[(app/choose-setting-tab {:setting ~tab})]))]
(dom/div
nil
(ui-settings-viewer (om/computed settings-viewer-tab {:switch-settings-tab! switch-settings-tab!}))))))
(def ui-settings-tab (om/factory SettingsTab))
(defui ^:once TabUnion
static om/IQuery
(query [this]
{:settings (om/get-query SettingsTab)})
static om/Ident
(ident [this props]
[(:which-tab props) :tab])
Object
(render [this]
(let [{:keys [which-tab] :as props} (om/props this)]
(dom/div nil
(ui-settings-tab props)))))
(def ui-tabs (om/factory TabUnion))
(defui ^:once Root
static om/IQuery
(query [this] [{:current-tab (om/get-query TabUnion)}])
Object
(render [this]
(let [{:keys [ui/react-key current-tab] :as props} (om/props this)]
(dom/div nil (ui-tabs current-tab)))))
(defmulti mutate om/dispatch)
(defmethod mutate 'app/choose-setting-tab
[{:keys [state ref]} k {:keys [setting]}]
{:action (fn [] (swap! state assoc-in [:settings :tab :settings-viewer-tab] setting))})
(defn read-local
[{:keys [query target state ast]} dkey _]
(when (not target)
(case dkey
(let [top-level-prop (nil? query)
key (or (:key ast) dkey)
by-ident? (util/ident? key)
union? (map? query)
data (if by-ident? (get-in @state key) (get @state key))]
{:value
(cond
union? (get (om/db->tree [{key query}] @state @state) key)
top-level-prop data
:else (om/db->tree query data @state))}))))
(defonce state (atom {:active-users {:tab {:which-tab :active-users}},
:high-score {:tab {:which-tab :high-score}},
:settings {:tab {:id :tab, :which-tab :settings,
:settings-viewer-tab [:active-users :tab]}},
:current-tab [:settings :tab],
:main {:tab {:id :tab, :which-tab :main}}}))
(defonce reconciler
(atom (om/reconciler
{:state state
:normalize true
:pathopt true
:parser (om/parser {:mutate mutate
:read read-local})})))
(defcard VanillaOm
""
(dom-node
(fn [_ node]
(om/add-root! @reconciler Root node))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment