From 1ba77a72675eeda51bfabadf2135763e6adf2aad Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 22 Jan 2025 11:10:39 +0200 Subject: [PATCH] Apply query parameters encoding on rfe/set-query --- .../reitit-frontend/src/reitit/frontend/history.cljs | 11 ++++++++--- test/cljs/reitit/frontend/easy_test.cljs | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 86043ddc..e8259894 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -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))) diff --git a/test/cljs/reitit/frontend/easy_test.cljs b/test/cljs/reitit/frontend/easy_test.cljs index 63e4ecb5..a82be64e 100644 --- a/test/cljs/reitit/frontend/easy_test.cljs +++ b/test/cljs/reitit/frontend/easy_test.cljs @@ -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. /