Created
November 4, 2019 23:42
-
-
Save papachan/6130212a073014daf8d8e1aef5331e00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns demo.core | |
(:require | |
[reagent.core :as reagent :refer [atom]] | |
[reagent.session :as session] | |
[reitit.frontend :as reitit] | |
[clerk.core :as clerk] | |
[accountant.core :as accountant])) | |
;; ------------------------- | |
;; Routes | |
(def router | |
(reitit/router | |
[["/" :index] | |
["/items" | |
["" :items] | |
["/:item-id" :item]] | |
["/about" :about]])) | |
(defn path-for [route & [params]] | |
(if params | |
(:path (reitit/match-by-name router route params)) | |
(:path (reitit/match-by-name router route)))) | |
(path-for :about) | |
;; ------------------------- | |
;; Page components | |
(defn home-page [] | |
(fn [] | |
[:span.main | |
[:h1 "app is running!"] | |
[:ul | |
[:li [:a {:href (path-for :items)} "Items of Demo"]] | |
[:li [:a {:href "/broken/link"} "Broken link"]]]])) | |
(defn items-page [] | |
(fn [] | |
[:span.main | |
[:h1 "The items of "] | |
[:ul (map (fn [item-id] | |
[:li {:name (str "item-" item-id) :key (str "item-" item-id)} | |
[:a {:href (path-for :item {:item-id item-id})} "Item: " item-id]]) | |
(range 1 60))]])) | |
(defn item-page [] | |
(fn [] | |
(let [routing-data (session/get :route) | |
item (get-in routing-data [:route-params :item-id])] | |
[:span.main | |
[:h1 (str "Item " item " of Demo")] | |
[:p [:a {:href (path-for :items)} "Back to the list of items"]]]))) | |
(defn about-page [] | |
(fn [] [:span.main | |
[:h1 "About "]])) | |
;; ------------------------- | |
;; Translate routes -> page components | |
(defn page-for [route] | |
(case route | |
:index #'home-page | |
:about #'about-page | |
:items #'items-page | |
:item #'item-page)) | |
;; ------------------------- | |
;; Page mounting component | |
(defn current-page [] | |
(fn [] | |
(let [page (:current-page (session/get :route))] | |
[:div | |
[:header | |
[:p [:a {:href (path-for :index)} "Home"] " | " | |
[:a {:href (path-for :about)} "About "]]] | |
[page] | |
[:footer | |
[:p " was generated by the " | |
[:a {:href "https://github.com/reagent-project/reagent-template"} "Reagent Template"] "."]]]))) | |
;; ------------------------- | |
;; Initialize app | |
(defn mount-root [] | |
(reagent/render [current-page] (.getElementById js/document "app"))) | |
(defn init! [] | |
(clerk/initialize!) | |
(accountant/configure-navigation! | |
{:nav-handler | |
(fn [path] | |
(let [match (reitit/match-by-path router path) | |
current-page (:name (:data match)) | |
route-params (:path-params match)] | |
(reagent/after-render clerk/after-render!) | |
(session/put! :route {:current-page (page-for current-page) | |
:route-params route-params}) | |
(clerk/navigate-page! path) | |
)) | |
:path-exists? | |
(fn [path] | |
(boolean (reitit/match-by-path router path)))}) | |
(accountant/dispatch-current!) | |
(mount-root)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment