~bendersteed/wikisophy-fe

1f4130bcd94769fde659a10664ac45f88781d9ca — Dimakakos Dimos 3 years ago e0fb969
Add: implementation of simple routing
1 files changed, 36 insertions(+), 6 deletions(-)

M src/wikisophy/core.cljs
M src/wikisophy/core.cljs => src/wikisophy/core.cljs +36 -6
@@ 22,6 22,12 @@
        :handler #(reset! path (response-handler %))}))

;; -------------------------
;; Utils

(defn mount [component]
  (r/render component (.getElementById js/document "app")))

;; -------------------------
;; Components

(defn navbar []


@@ 32,8 38,9 @@
   [:input {:id "bmenub" :type "checkbox" :class "show"}]
   [:label.icon-g {:for "bmenub" :class "burger pseudo button"} "menu"]
   [:div.menu
    [:a.pseudo.button {:href "#"} "About"]
    [:a.pseudo.button {:href "#"} "Analytics"]]])
    [:a.button {:href ""} "Home"]
    [:a.pseudo.button {:href "#about"} "About"]
    [:a.pseudo.button {:href "#analytics"} "Analytics"]]])

(defn header []
  [:header


@@ 138,12 145,35 @@
   [input]
   [graph]])

(defn about-page []
  [:body [navbar]
   [:h1 "About Page"]])

(defn analytics-page []
  [:body [navbar]
   [:h1 "Analytics Page"]])

;; -------------------------
;; Initialize app
;; Routing

(def routes
  {"" front-page
   "#about" about-page
   "#analytics" analytics-page})

(defn handle-routes [routes event]
  (let [hash (.-location.hash js/window)
        page (get routes hash (get routes ""))]
    (.history.replaceState js/window {} nil hash)
    (mount page)))

(defn mount-root []
  (r/render [front-page] (.getElementById js/document "app")))
(defn setup-router [routes]
  (.addEventListener js/window "hashchange" #(handle-routes routes %))
  (handle-routes routes nil))

;; -------------------------
;; Initialize app

(defn init! []
  (mount-root))
  (setup-router routes))