Add more adaptations to make app run in a prefixed path.

This commit is contained in:
Andrey Antukh 2021-04-19 18:35:36 +02:00 committed by Andrés Moya
parent 2828ccda7f
commit 55ea84a056
14 changed files with 115 additions and 98 deletions

View file

@ -9,37 +9,17 @@
(:require
[app.common.data :as d]
[app.config :as cfg]
[app.common.uri :as u]
[app.util.browser-history :as bhistory]
[app.util.timers :as ts]
[beicon.core :as rx]
[cuerdas.core :as str]
[goog.events :as e]
[potok.core :as ptk]
[reitit.core :as r])
(:import
goog.Uri
goog.Uri.QueryData))
[reitit.core :as r]))
;; --- Router API
(defn- parse-query-data
[^QueryData qdata]
(persistent!
(reduce (fn [acc key]
(let [values (.getValues qdata key)
rkey (str/keyword key)]
(cond
(> (alength values) 1)
(assoc! acc rkey (into [] values))
(= (alength values) 1)
(assoc! acc rkey (aget values 0))
:else
acc)))
(transient {})
(.getKeys qdata))))
(defn resolve
([router id] (resolve router id {} {}))
([router id params] (resolve router id params {}))
@ -47,12 +27,10 @@
(when-let [match (r/match-by-name router id params)]
(if (empty? qparams)
(r/match->path match)
(let [uri (.parse goog.Uri (r/match->path match))
qdt (.createFromMap QueryData (-> qparams
(d/without-nils)
(clj->js)))]
(.setQueryData ^js uri qdt)
(.toString ^js uri))))))
(let [query (u/map->query-string qparams)]
(-> (u/uri (r/match->path match))
(assoc :query query)
(str)))))))
(defn create
[routes]
@ -65,26 +43,18 @@
(update [_ state]
(assoc state :router (create routes)))))
(defn query-params
"Given goog.Uri, read query parameters into Clojure map."
[^goog.Uri uri]
(let [^js q (.getQueryData uri)]
(->> q
(.getKeys)
(map (juxt keyword #(.get q %)))
(into {}))))
(defn match
"Given routing tree and current path, return match with possibly
coerced parameters. Return nil if no match found."
[router path]
(let [uri (.parse ^js Uri path)]
(when-let [match (r/match-by-path router (.getPath ^js uri))]
(let [qparams (parse-query-data (.getQueryData ^js uri))
params {:path (:path-params match) :query qparams}]
(assoc match
:params params
:query-params qparams)))))
(let [uri (u/uri path)]
(when-let [match (r/match-by-path router (:path uri))]
(let [qparams (u/query-string->map (:query uri))
params {:path (:path-params match)
:query qparams}]
(-> match
(assoc :params params)
(assoc :query-params qparams))))))
;; --- Navigate (Event)
@ -119,8 +89,9 @@
(effect [_ state stream]
(let [router (:router state)
path (resolve router id params qparams)
uri (str cfg/public-uri "/#" path)]
(js/window.open uri "_blank"))))
uri (-> (u/uri cfg/public-uri)
(assoc :fragment path))]
(js/window.open (str uri) "_blank"))))
(defn nav-new-window
([id] (nav-new-window id nil nil))