Add path and query params to frontend example

This commit is contained in:
Juho Teperi 2018-07-11 10:26:53 +03:00
parent dff9206210
commit aeadfad880
3 changed files with 39 additions and 15 deletions

View file

@ -13,7 +13,9 @@
[org.clojure/clojurescript "1.10.339" :scope "provided"] [org.clojure/clojurescript "1.10.339" :scope "provided"]
[metosin/reitit "0.1.4-SNAPSHOT"] [metosin/reitit "0.1.4-SNAPSHOT"]
[metosin/reitit-schema "0.1.4-SNAPSHOT"] [metosin/reitit-schema "0.1.4-SNAPSHOT"]
[metosin/reitit-frontend "0.1.4-SNAPSHOT"]] [metosin/reitit-frontend "0.1.4-SNAPSHOT"]
;; Just for pretty printting the match
[fipp "0.6.12"]]
:plugins [[lein-cljsbuild "1.1.7"]] :plugins [[lein-cljsbuild "1.1.7"]]

View file

@ -1,40 +1,62 @@
(ns frontend.core (ns frontend.core
(:require [reagent.core :as r] (:require [reagent.core :as r]
[reitit.core :as rc] [reitit.core :as re]
[reitit.frontend :as rf] [reitit.frontend :as rf]
[reitit.frontend.history :as rfh] [reitit.frontend.history :as rfh]
[reitit.coercion.schema :as rs])) [reitit.coercion :as rc]
[reitit.coercion.schema :as rsc]
[schema.core :as s]
[fipp.edn :as fedn]))
(def router (atom nil)) (def router (atom nil))
(defn home-page [] (defn home-page []
[:div [:div
[:h2 "Welcome to frontend"] [:h2 "Welcome to frontend"]])
[:div [:a {:href (rfh/href @router ::about)} "go to about page"]]])
(defn about-page [] (defn about-page []
[:div [:div
[:h2 "About frontend"] [:h2 "About frontend"]
[:a {:href "http://google.com"} "external link"] [:a {:href "http://google.com"} "external link"]])
[:div [:a {:href (rfh/href @router ::frontpage)} "go to the home page"]]])
(defn item-page [match]
(let [{:keys [path query]} (:parameters match)
{:keys [id]} path]
[:div
[:h2 "Selected item " id]
(if (:foo query)
[:p "Optional foo query param: " (:foo query)])]))
(defonce match (r/atom nil)) (defonce match (r/atom nil))
(defn current-page [] (defn current-page []
[:div [:div
[:ul
[:li [:a {:href (rfh/href @router ::frontpage)} "Frontpage"]]
[:li [:a {:href (rfh/href @router ::about)} "About"]]
[:li [:a {:href (rfh/href @router ::item {:id 1})} "Item 1"]]
[:li [:a {:href (rfh/href @router ::item {:id 2} {:foo "bar"})} "Item 2"]]]
(if @match (if @match
[:div [(:view (:data @match))]]) (let [view (:view (:data @match))]
(pr-str @match)]) [view @match]))
[:pre (with-out-str (fedn/pprint @match))]])
(def routes (def routes
(rc/router (re/router
["/" ["/"
["" [""
{:name ::frontpage {:name ::frontpage
:view home-page}] :view home-page}]
["about" ["about"
{:name ::about {:name ::about
:view about-page}]])) :view about-page}]
["item/:id"
{:name ::item
:view item-page
:parameters {:path {:id s/Int}
:query {(s/optional-key :foo) s/Keyword}}}]]
{:compile rc/compile-request-coercers
:data {:coercion rsc/coercion}}))
(defn init! [] (defn init! []
(reset! router (rfh/start! routes (reset! router (rfh/start! routes

View file

@ -2,9 +2,9 @@
(:require [clojure.test :refer [deftest testing is are]] (:require [clojure.test :refer [deftest testing is are]]
[reitit.core :as r] [reitit.core :as r]
[reitit.frontend :as rf] [reitit.frontend :as rf]
[reitit.coercion :as coercion] [reitit.coercion :as rc]
[schema.core :as s] [schema.core :as s]
[reitit.coercion.schema :as schema-coercion])) [reitit.coercion.schema :as rsc]))
(defn m [x] (defn m [x]
(assoc x :data nil :result nil)) (assoc x :data nil :result nil))
@ -45,8 +45,8 @@
[":id" {:name ::foo [":id" {:name ::foo
:parameters {:path {:id s/Int} :parameters {:path {:id s/Int}
:query {(s/optional-key :mode) s/Keyword}}}]] :query {(s/optional-key :mode) s/Keyword}}}]]
{:compile coercion/compile-request-coercers {:compile rc/compile-request-coercers
:data {:coercion schema-coercion/coercion}})] :data {:coercion rsc/coercion}})]
(is (= (r/map->Match (is (= (r/map->Match
{:template "/:id" {:template "/:id"
:path-params {:id "5"} :path-params {:id "5"}