Skip to content

Instantly share code, notes, and snippets.

@chrismurrph
Created June 13, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismurrph/4f1be8345b2d5312e81b530ae4efa40e to your computer and use it in GitHub Desktop.
Save chrismurrph/4f1be8345b2d5312e81b530ae4efa40e to your computer and use it in GitHub Desktop.
Machinery for loading and unloading tabs
(ns om-alarming.mutations.nav
(:require [untangled.client.mutations :as m]
[untangled.dom :refer [unique-key]]
[om-alarming.state :as st]))
(def tab-loadings [{:target :app/trending
:moves [{:key :grid/gas-query-grid :ident [:gas-query-grid/by-id st/GasQueryGrid]}
{:key :graph/trending-graph :ident [:trending-graph/by-id st/TrendingGraph]}]}
{:target :app/automatic
:moves [{:key :tube/automatic-panel :ident [:automatic-panel/by-id st/AutomaticPanel]}]}
{:target :app/manual
:moves [{:key :tube/manual-panel :ident [:manual-panel/by-id st/ManualPanel]}]}
{:target :app/logs
:moves [{:key :err/system-faults-panel :ident [:system-faults-panel/by-id st/SystemFaultsPanel]}]}])
(defn unload [state prev-target]
(let [data (first (filter #(= (:target %) prev-target) tab-loadings))]
(if data
(let [ident [prev-target :singleton]
dissoc-keys (mapv :key (:moves data))]
(apply update-in state ident dissoc dissoc-keys))
state)))
(defn load [state new-target]
(let [data (first (filter #(= (:target %) new-target) tab-loadings))]
(if data
(let [ident [new-target :singleton]
to-assoc (mapcat (fn [item] (vector (:key item) (:ident item))) (:moves data))]
(apply update-in state ident assoc to-assoc))
state)))
(comment (defn load-simple [state new-target]
(case new-target
:app/trending (-> state
(update-in [:app/trending :singleton] assoc
:grid/gas-query-grid [:gas-query-grid/by-id st/GasQueryGrid]
:graph/trending-graph [:trending-graph/by-id st/TrendingGraph]
))
state)))
(comment (defn unload-simple [state prev-target]
(case prev-target
:app/trending (-> state
(update-in [:app/trending :singleton] dissoc
:grid/gas-query-grid
:graph/trending-graph
))
state)))
;;
;; Only happens once so we could check. However we should probably do this as a post-load thing. Or another idea is
;; to move current out and new in. Thus will achieve not rendering tabs that are not visible
;;
(defn change-tab [old-st new-target]
(let [prev-target (first (get old-st :app/current-tab))
new-state (-> old-st
(unload prev-target)
(load new-target)
(assoc :app/current-tab [new-target :singleton]))]
new-state))
(defmethod m/mutate 'nav/load-tab [{:keys [state]} k {:keys [target]}]
{:action (fn [] (swap! state change-tab target))})
;;
;; What is a post mutate and why does it keep getting called?
;;
(defmethod m/post-mutate :default [{:keys [state]} _ _]
;(println "Nothing in default for post-mutate")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment