Skip to content

Commit 910569e

Browse files
committed
Apply query parameters encoding on rfe/set-query
1 parent 4de3b6a commit 910569e

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

modules/reitit-frontend/src/reitit/frontend/history.cljs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,18 @@
289289
New query params can be given as a map, or a function taking
290290
the old params and returning the new modified params.
291291
292-
Note: The query parameter values aren't coereced, so the
293-
update fn will see string values for all query params."
292+
The current path is matched against the routing tree, and the match data
293+
(schema, coercion) is used to encode the query parameters."
294294
([history new-query-or-update-fn]
295295
(set-query history new-query-or-update-fn nil))
296296
([history new-query-or-update-fn {:keys [replace] :as opts}]
297297
(let [current-path (-get-path history)
298-
new-path (rf/set-query-params current-path new-query-or-update-fn)]
298+
;; FIXME: What if there is no match?
299+
match (rf/match-by-path (.-router history) current-path)
300+
query-params (if (fn? new-query-or-update-fn)
301+
(new-query-or-update-fn (:query (:parameters match)))
302+
new-query-or-update-fn)
303+
new-path (rf/match->path match query-params (:fragment (:parameters match)))]
299304
(if replace
300305
(.replaceState js/window.history nil "" (-href history new-path))
301306
(.pushState js/window.history nil "" (-href history new-path)))

test/cljs/reitit/frontend/easy_test.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@
6969
;; 2. /bar/2?a=1
7070
6 (do (is (= "/bar/2?a=1" url)
7171
"update-query with map")
72-
(rfe/set-query #(assoc % :b "foo") {:replace true}))
72+
(rfe/set-query #(assoc % :q "x") {:replace true}))
7373
;; 0. /
7474
;; 1. /bar/2
7575
;; 2. /bar/2?a=1&b=foo
76-
7 (do (is (= "/bar/2?a=1&b=foo" url)
76+
7 (do (is (= "/bar/2?a=1&q=__x" url)
7777
"update-query with fn")
7878
(.go js/window.history -2))
7979
;; 0. /

0 commit comments

Comments
 (0)