Skip to content

Commit

Permalink
Fix push+secretary nav
Browse files Browse the repository at this point in the history
I guess updating clojurescript started warning about the use of private
functions from secretary. Not sure why it wasn't warning before, but
it's probably best to pull them in anyway.
  • Loading branch information
dhleong committed Nov 12, 2018
1 parent af2c527 commit 4716b56
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/cljs/wish/util/nav.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns ^{:author "Daniel Leong"
:doc "Navigation util"}
wish.util.nav
(:require [clojure.string :as string]
(:require [clojure.string :as str]
[reagent.dom :as reagent-dom]
[re-frame.core :refer [dispatch-sync]]
[secretary.core :as secretary]
Expand All @@ -19,11 +19,22 @@
(pushy/supported?)))

(def ^:private pushy-prefix "/wish")
(def ^:private secretary-prefix (if pushy-supported?
(str pushy-prefix "/")
"#"))

(defn init! []
(secretary/set-config! :prefix (if pushy-supported?
(str pushy-prefix "/")
"#")))
(secretary/set-config! :prefix secretary-prefix))

;; from secretary
(defn- uri-without-prefix [uri]
(str/replace uri (re-pattern (str "^" secretary-prefix)) ""))
(defn- uri-with-leading-slash
"Ensures that the uri has a leading slash"
[uri]
(if (= "/" (first uri))
uri
(str "/" uri)))

;; must be called after routes have been defined
(defn hook-browser-navigation! []
Expand All @@ -32,8 +43,11 @@
(let [history (pushy/pushy
secretary/dispatch!
(fn [x]
(when (secretary/locate-route x)
x)))]
(let [[uri-path query-string]
(str/split (uri-without-prefix x) #"\?")
uri-path (uri-with-leading-slash uri-path)]
(when (secretary/locate-route uri-path)
x))))]
(pushy/start! history))

; #-based navigation
Expand Down

0 comments on commit 4716b56

Please sign in to comment.