mirror of
https://github.com/penpot/penpot.git
synced 2025-03-13 16:21:57 -05:00
Improved routing (now using volatile instead direct mutation).
This commit is contained in:
parent
94bdf67c17
commit
b3f3d51dae
1 changed files with 33 additions and 29 deletions
|
@ -2,11 +2,17 @@
|
|||
(:require [bidi.router]
|
||||
[bidi.bidi :as bidi]
|
||||
[goog.events :as events]
|
||||
[cats.labs.lens :as l]
|
||||
[uxbox.state :as s]
|
||||
[uxbox.rstore :as rs]))
|
||||
|
||||
(enable-console-print!)
|
||||
|
||||
(def +router+ nil)
|
||||
(defonce +router+ (volatile! nil))
|
||||
|
||||
(def ^:const route-l
|
||||
(as-> (l/in [:route]) $
|
||||
(l/focus-atom $ s/state)))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Events
|
||||
|
@ -17,22 +23,22 @@
|
|||
(reify
|
||||
rs/UpdateEvent
|
||||
(-apply-update [_ state]
|
||||
(merge state
|
||||
{:location handler}
|
||||
(when route-params
|
||||
{:location-params route-params})))))
|
||||
(let [route (merge {:id handler}
|
||||
(when route-params
|
||||
{:params route-params}))]
|
||||
(assoc state :route route)))))
|
||||
|
||||
(defn navigate
|
||||
([name] (navigate name nil))
|
||||
([name params]
|
||||
{:pre [(keyword? name)]}
|
||||
([id] (navigate id nil))
|
||||
([id params]
|
||||
{:pre [(keyword? id)]}
|
||||
(reify
|
||||
rs/EffectEvent
|
||||
(-apply-effect [_ state]
|
||||
(let [loc (merge {:handler name}
|
||||
(let [loc (merge {:handler id}
|
||||
(when params
|
||||
{:route-params params}))]
|
||||
(bidi.router/set-location! +router+ loc))))))
|
||||
(bidi.router/set-location! @+router+ loc))))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Router declaration
|
||||
|
@ -41,22 +47,22 @@
|
|||
(def ^:private page-route
|
||||
[[bidi/uuid :project-uuid] "/" [bidi/uuid :page-uuid]])
|
||||
|
||||
(def ^:static
|
||||
routes ["/" [["auth/login" :auth/login]
|
||||
;; ["auth/register" :auth/register]
|
||||
;; ["auth/recover" :auth/recover-password]
|
||||
["dashboard/" [["projects" :dashboard/projects]
|
||||
["elements" :dashboard/elements]
|
||||
["icons" :dashboard/icons]
|
||||
["colors" :dashboard/colors]]]
|
||||
["workspace/" [[page-route :workspace/page]]]]])
|
||||
(def ^:const routes
|
||||
["/" [["auth/login" :auth/login]
|
||||
["auth/register" :auth/register]
|
||||
["auth/recover" :auth/recover-password]
|
||||
["dashboard/" [["projects" :dashboard/projects]
|
||||
["elements" :dashboard/elements]
|
||||
["icons" :dashboard/icons]
|
||||
["colors" :dashboard/colors]]]
|
||||
["workspace/" [[page-route :workspace/page]]]]])
|
||||
|
||||
(defn init
|
||||
[]
|
||||
(let [opts {:on-navigate #(rs/emit! (update-location %))
|
||||
:default-location {:handler :dashboard/projects}}
|
||||
:default-location {:handler :auth/login}}
|
||||
router (bidi.router/start-router! routes opts)]
|
||||
(set! +router+ router)))
|
||||
(vreset! +router+ router)))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Public Api
|
||||
|
@ -64,15 +70,13 @@
|
|||
|
||||
(defn go
|
||||
"Redirect the user to other url."
|
||||
([name] (go name nil))
|
||||
([name params] (rs/emit! (navigate name params))))
|
||||
([id] (go id nil))
|
||||
([id params] (rs/emit! (navigate id params))))
|
||||
|
||||
(defn route-for
|
||||
"Given a location handler and optional parameter map, return the URI
|
||||
for such handler and parameters."
|
||||
([location]
|
||||
(bidi/path-for routes location))
|
||||
([location params]
|
||||
(apply bidi/path-for routes location (into []
|
||||
(mapcat (fn [[k v]] [k v]))
|
||||
params))))
|
||||
([id]
|
||||
(bidi/path-for routes id))
|
||||
([id params]
|
||||
(apply bidi/path-for routes id (into [] (mapcat (fn [[k v]] [k v])) params))))
|
||||
|
|
Loading…
Add table
Reference in a new issue