0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-14 08:41:48 -05:00

Improved routing (now using volatile instead direct mutation).

This commit is contained in:
Andrey Antukh 2016-02-24 17:18:55 +02:00
parent 94bdf67c17
commit b3f3d51dae

View file

@ -2,11 +2,17 @@
(:require [bidi.router] (:require [bidi.router]
[bidi.bidi :as bidi] [bidi.bidi :as bidi]
[goog.events :as events] [goog.events :as events]
[cats.labs.lens :as l]
[uxbox.state :as s]
[uxbox.rstore :as rs])) [uxbox.rstore :as rs]))
(enable-console-print!) (enable-console-print!)
(def +router+ nil) (defonce +router+ (volatile! nil))
(def ^:const route-l
(as-> (l/in [:route]) $
(l/focus-atom $ s/state)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Events ;; Events
@ -17,22 +23,22 @@
(reify (reify
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(merge state (let [route (merge {:id handler}
{:location handler} (when route-params
(when route-params {:params route-params}))]
{:location-params route-params}))))) (assoc state :route route)))))
(defn navigate (defn navigate
([name] (navigate name nil)) ([id] (navigate id nil))
([name params] ([id params]
{:pre [(keyword? name)]} {:pre [(keyword? id)]}
(reify (reify
rs/EffectEvent rs/EffectEvent
(-apply-effect [_ state] (-apply-effect [_ state]
(let [loc (merge {:handler name} (let [loc (merge {:handler id}
(when params (when params
{:route-params params}))] {:route-params params}))]
(bidi.router/set-location! +router+ loc)))))) (bidi.router/set-location! @+router+ loc))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Router declaration ;; Router declaration
@ -41,22 +47,22 @@
(def ^:private page-route (def ^:private page-route
[[bidi/uuid :project-uuid] "/" [bidi/uuid :page-uuid]]) [[bidi/uuid :project-uuid] "/" [bidi/uuid :page-uuid]])
(def ^:static (def ^:const routes
routes ["/" [["auth/login" :auth/login] ["/" [["auth/login" :auth/login]
;; ["auth/register" :auth/register] ["auth/register" :auth/register]
;; ["auth/recover" :auth/recover-password] ["auth/recover" :auth/recover-password]
["dashboard/" [["projects" :dashboard/projects] ["dashboard/" [["projects" :dashboard/projects]
["elements" :dashboard/elements] ["elements" :dashboard/elements]
["icons" :dashboard/icons] ["icons" :dashboard/icons]
["colors" :dashboard/colors]]] ["colors" :dashboard/colors]]]
["workspace/" [[page-route :workspace/page]]]]]) ["workspace/" [[page-route :workspace/page]]]]])
(defn init (defn init
[] []
(let [opts {:on-navigate #(rs/emit! (update-location %)) (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)] router (bidi.router/start-router! routes opts)]
(set! +router+ router))) (vreset! +router+ router)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api ;; Public Api
@ -64,15 +70,13 @@
(defn go (defn go
"Redirect the user to other url." "Redirect the user to other url."
([name] (go name nil)) ([id] (go id nil))
([name params] (rs/emit! (navigate name params)))) ([id params] (rs/emit! (navigate id params))))
(defn route-for (defn route-for
"Given a location handler and optional parameter map, return the URI "Given a location handler and optional parameter map, return the URI
for such handler and parameters." for such handler and parameters."
([location] ([id]
(bidi/path-for routes location)) (bidi/path-for routes id))
([location params] ([id params]
(apply bidi/path-for routes location (into [] (apply bidi/path-for routes id (into [] (mapcat (fn [[k v]] [k v])) params))))
(mapcat (fn [[k v]] [k v]))
params))))