Fix #600: Add frontend function to update query-params for current path

This commit is contained in:
Juho Teperi 2023-03-23 15:13:27 +02:00
parent fc12af4ea8
commit dd724f0d0e
2 changed files with 23 additions and 1 deletions

View file

@ -88,3 +88,14 @@
match)
(do (js/console.warn "missing route" name)
nil))))
(defn update-path-query-params
"Given Reitit-frontend path, update the query params
with given function and arguments.
NOTE: coercion is not applied to the query params"
[path f & args]
(let [^goog.Uri uri (Uri/parse path)
new-query (apply f (query-params uri) args)]
(.setQueryData uri (QueryData/createFromMap (clj->js new-query)))
(.toString uri)))

View file

@ -2,7 +2,8 @@
"Easy wrapper over reitit.frontend.history,
handling the state. Only one router can be active
at a time."
(:require [reitit.frontend.history :as rfh]))
(:require [reitit.frontend.history :as rfh]
[reitit.frontend :as rf]))
(defonce history (atom nil))
@ -101,3 +102,13 @@
(rfh/replace-state @history name path-params nil))
([name path-params query-params]
(rfh/replace-state @history name path-params query-params)))
(defn update-query
"Takes the current location and updates the query params
with given fn and arguments."
[f & args]
;; TODO: rfh version?
(let [current-path (rfh/-get-path @history)
new-path (apply rf/update-path-query-params current-path f args)]
(.pushState js/window.history nil "" new-path)
(rfh/-on-navigate @history new-path)))