Skip to content

Instantly share code, notes, and snippets.

@jeans11

jeans11/core.clj Secret

Last active January 24, 2022 08:17
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 jeans11/41951eaff256d8aecc228e717c9000b3 to your computer and use it in GitHub Desktop.
Save jeans11/41951eaff256d8aecc228e717c9000b3 to your computer and use it in GitHub Desktop.
Routing
(ns demo.fulcro
(:require [accountant.core :as history]))
;; Fulcro application state
(defonce app (app/fulcro-app))
(defn start-routing! []
(history/configure-navigation!
{:nav-handler (fn [path]
(let [route-segment (vec (rest (str/split path #"/")))]
(dr/change-route! app (if (seq route-segment) route-segment [""]))))})
(history/dispatch-current!))
(defn set-root!
[app root-component]
(swap! (::app/state-atom app) #(merge {:fulcro.inspect.core/app-id (comp/component-name root-component)} %))
(app/set-root! app root-component {:initialize-state? true}))
(defn -mount-app! []
(set-root! app entry/Root)
(rn/register-root-component! app-name entry/Root app)
(start-routing!)
(rn/run-application! app-name "app"))
(def ui-layout (comp/factory Layout))
(defsc Home [_this _props]
{:ident (fn [] [:component/id ::home])
:query []
:route-segment ["home"]
:initial-state {}}
(rn/ui-view
(rn/ui-text "Home")))
(defrouter Router [_this {:keys [current-state]}]
{:router-targets [Home]}
(prn current-state)
(case current-state
:initial (rn/ui-text "Show something")
:pending (rn/ui-text "Loading...")
(rn/ui-text "No route selected")))
(def ui-router (comp/factory Router))
(defsc Root [this {:root/keys [router]}]
{:query [{:root/router (comp/get-query Router)}]
:componentDidMount (fn [this] (mroot/register-root! this {:app app}))
:componentWillUnmount (fn [this] (mroot/deregister-root! this {:app app}))
:initial-state {:root/router {}}}
(ui-layout {} (ui-router router)))
(-mount-app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment