Skip to content

Commit

Permalink
Apply query parameters encoding on rfe/set-query
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Jan 22, 2025
1 parent 4de3b6a commit 910569e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions modules/reitit-frontend/src/reitit/frontend/history.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,18 @@
New query params can be given as a map, or a function taking
the old params and returning the new modified params.
Note: The query parameter values aren't coereced, so the
update fn will see string values for all query params."
The current path is matched against the routing tree, and the match data
(schema, coercion) is used to encode the query parameters."
([history new-query-or-update-fn]
(set-query history new-query-or-update-fn nil))
([history new-query-or-update-fn {:keys [replace] :as opts}]
(let [current-path (-get-path history)
new-path (rf/set-query-params current-path new-query-or-update-fn)]
;; FIXME: What if there is no match?
match (rf/match-by-path (.-router history) current-path)
query-params (if (fn? new-query-or-update-fn)
(new-query-or-update-fn (:query (:parameters match)))
new-query-or-update-fn)
new-path (rf/match->path match query-params (:fragment (:parameters match)))]
(if replace
(.replaceState js/window.history nil "" (-href history new-path))
(.pushState js/window.history nil "" (-href history new-path)))
Expand Down
4 changes: 2 additions & 2 deletions test/cljs/reitit/frontend/easy_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
;; 2. /bar/2?a=1
6 (do (is (= "/bar/2?a=1" url)
"update-query with map")
(rfe/set-query #(assoc % :b "foo") {:replace true}))
(rfe/set-query #(assoc % :q "x") {:replace true}))
;; 0. /
;; 1. /bar/2
;; 2. /bar/2?a=1&b=foo
7 (do (is (= "/bar/2?a=1&b=foo" url)
7 (do (is (= "/bar/2?a=1&q=__x" url)
"update-query with fn")
(.go js/window.history -2))
;; 0. /
Expand Down

0 comments on commit 910569e

Please sign in to comment.