Use query-string fn in core

This commit is contained in:
Juho Teperi 2018-07-11 09:52:35 +03:00
parent 434bc25cd6
commit 245902bd4d
3 changed files with 28 additions and 30 deletions

View file

@ -16,19 +16,6 @@
(map (juxt keyword #(.get q %)))
(into {}))))
(defn query-string
"Given map, creates "
[m]
(str/join "&" (map (fn [[k v]]
(str (js/encodeURIComponent (name k))
"="
;; FIXME: create protocol to handle how types are converted to string
;; FIXME: array to multiple params
(if (coll? v)
(str/join "," (map #(js/encodeURIComponent %) v))
(js/encodeURIComponent v))))
m)))
(defn match-by-path
"Given routing tree and current path, return match with possibly
coerced parameters. Return nil if no match found."
@ -47,12 +34,7 @@
(assoc match :parameters parameters)))))
(defn match-by-name
[router name params]
;; FIXME: move router not initialized to re-frame integration?
(if router
(or (reitit/match-by-name router name params)
;; FIXME: return nil?
(do
(js/console.error "Can't create URL for route " (pr-str name) (pr-str params))
nil))
::not-initialized))
([router name]
(match-by-name router name {}))
([router name path-params]
(reitit/match-by-name router name path-params)))

View file

@ -4,6 +4,7 @@
[clojure.string :as string]
[goog.events :as e]
[goog.dom :as dom]
[reitit.core :as r]
[reitit.frontend :as rf])
(:import goog.history.Html5History
goog.Uri))
@ -100,11 +101,8 @@
(close-fn)))
(defn- match->token [history match k params query]
;; FIXME: query string
(if-let [path (:path match)]
(str (path->token history path)
(if query
(str "?" (rf/query-string query))))))
(some->> (r/match->path match query)
(path->token history)))
(defn href
([state k]

View file

@ -6,6 +6,9 @@
[schema.core :as s]
[reitit.coercion.schema :as schema-coercion]))
(defn m [x]
(assoc x :data nil :result nil))
(deftest match-by-path-test
(testing "simple"
(let [router (r/router ["/"
@ -20,6 +23,10 @@
:parameters {:query {}
:path {}}})
(rf/match-by-path router "/")))
(is (= "/"
(r/match->path (rf/match-by-name router ::frontpage))))
(is (= (r/map->Match
{:template "/foo"
:data {:name ::foo}
@ -27,7 +34,11 @@
:path "/foo"
:parameters {:query {}
:path {}}})
(rf/match-by-path router "/foo")))))
(rf/match-by-path router "/foo")))
(is (= "/foo"
(r/match->path (rf/match-by-name router ::foo))))))
(testing "schema coercion"
(let [router (r/router ["/"
@ -42,7 +53,11 @@
:path "/5"
:parameters {:query {}
:path {:id 5}}})
(assoc (rf/match-by-path router "/5") :data nil :result nil)))
(m (rf/match-by-path router "/5"))))
(is (= "/5"
(r/match->path (rf/match-by-name router ::foo {:id 5}))))
(is (= (r/map->Match
{:template "/:id"
:path-params {:id "5"}
@ -50,4 +65,7 @@
:path "/5"
:parameters {:path {:id 5}
:query {:mode :foo}}})
(assoc (rf/match-by-path router "/5?mode=foo") :data nil :result nil))))))
(m (rf/match-by-path router "/5?mode=foo"))))
(is (= "/5?mode=foo"
(r/match->path (rf/match-by-name router ::foo {:id 5}) {:mode :foo}))))))