mirror of
https://github.com/penpot/penpot.git
synced 2025-05-09 18:36:37 +02:00
✨ Improve (and fix many bugs) routing handling.
This commit is contained in:
parent
1e6ed35c77
commit
d9c459e877
3 changed files with 32 additions and 26 deletions
|
@ -13,6 +13,7 @@
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[goog.object :as gobj]
|
[goog.object :as gobj]
|
||||||
[rumext.alpha :as mf]
|
[rumext.alpha :as mf]
|
||||||
|
[uxbox.common.uuid :as uuid]
|
||||||
[uxbox.main.data.auth :refer [logout]]
|
[uxbox.main.data.auth :refer [logout]]
|
||||||
[uxbox.main.data.users :as udu]
|
[uxbox.main.data.users :as udu]
|
||||||
[uxbox.main.store :as st]
|
[uxbox.main.store :as st]
|
||||||
|
@ -23,6 +24,7 @@
|
||||||
[uxbox.util.i18n :as i18n]
|
[uxbox.util.i18n :as i18n]
|
||||||
[uxbox.util.theme :as theme]
|
[uxbox.util.theme :as theme]
|
||||||
[uxbox.util.router :as rt]
|
[uxbox.util.router :as rt]
|
||||||
|
[uxbox.util.object :as obj]
|
||||||
[uxbox.util.storage :refer [storage]]
|
[uxbox.util.storage :refer [storage]]
|
||||||
[uxbox.util.timers :as ts]))
|
[uxbox.util.timers :as ts]))
|
||||||
|
|
||||||
|
@ -31,35 +33,38 @@
|
||||||
(defn on-navigate
|
(defn on-navigate
|
||||||
[router path]
|
[router path]
|
||||||
(let [match (rt/match router path)
|
(let [match (rt/match router path)
|
||||||
profile (:profile storage)]
|
profile (:profile storage)
|
||||||
|
authed? (and (not (nil? profile))
|
||||||
|
(not= (:id profile) uuid/zero))]
|
||||||
(cond
|
(cond
|
||||||
(and (= path "") (not profile))
|
(and (or (= path "")
|
||||||
(rt/nav :login)
|
(nil? match))
|
||||||
|
(not authed?))
|
||||||
|
(st/emit! (rt/nav :login))
|
||||||
|
|
||||||
|
(and (= path "") authed?)
|
||||||
|
(st/emit! (rt/nav :dashboard-team {:team-id (:default-team-id profile)}))
|
||||||
|
|
||||||
(and (= path "") profile)
|
|
||||||
(rt/nav :dashboard-team {:team-id (:default-team-id profile)})
|
|
||||||
|
|
||||||
(nil? match)
|
(nil? match)
|
||||||
(rt/nav :not-found)
|
(st/emit! (rt/nav :not-found))
|
||||||
|
|
||||||
:else
|
:else
|
||||||
#(assoc % :route match))))
|
(st/emit! #(assoc % :route match)))))
|
||||||
|
|
||||||
(defn init-ui
|
(defn init-ui
|
||||||
[]
|
[]
|
||||||
(st/emit! (rt/initialize-router ui/routes)
|
(st/emit! (rt/initialize-router ui/routes)
|
||||||
(rt/initialize-history on-navigate))
|
(rt/initialize-history on-navigate))
|
||||||
|
|
||||||
(when (:profile storage)
|
(st/emit! udu/fetch-profile)
|
||||||
(st/emit! udu/fetch-profile))
|
|
||||||
|
|
||||||
(mf/mount (mf/element ui/app) (dom/get-element "app"))
|
(mf/mount (mf/element ui/app) (dom/get-element "app"))
|
||||||
(mf/mount (mf/element modal) (dom/get-element "modal")))
|
(mf/mount (mf/element modal) (dom/get-element "modal")))
|
||||||
|
|
||||||
(defn ^:export init
|
(defn ^:export init
|
||||||
[]
|
[]
|
||||||
(let [translations (gobj/get goog.global "uxboxTranslations")
|
(let [translations (obj/get js/window "uxboxTranslations")
|
||||||
themes (gobj/get goog.global "uxboxThemes")]
|
themes (gobj/get js/window "uxboxThemes")]
|
||||||
(i18n/init! translations)
|
(i18n/init! translations)
|
||||||
(theme/init! themes)
|
(theme/init! themes)
|
||||||
(st/init)
|
(st/init)
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
|
|
||||||
(mf/defc profile-section
|
(mf/defc profile-section
|
||||||
[{:keys [profile] :as props}]
|
[{:keys [profile] :as props}]
|
||||||
(let [show (mf/use-state false)
|
(let [show (mf/use-state false)
|
||||||
photo (:photo-uri profile "")
|
photo (:photo-uri profile "")
|
||||||
photo (if (str/empty? photo)
|
photo (if (str/empty? photo)
|
||||||
"/images/avatar.jpg"
|
"/images/avatar.jpg"
|
||||||
|
|
|
@ -2,19 +2,22 @@
|
||||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2015-2019 Andrey Antukh <niwi@niwi.nz>
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
(ns uxbox.util.router
|
(ns uxbox.util.router
|
||||||
(:refer-clojure :exclude [resolve])
|
(:refer-clojure :exclude [resolve])
|
||||||
(:require
|
(:require
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[rumext.alpha :as mf]
|
|
||||||
[reitit.core :as r]
|
|
||||||
[goog.events :as e]
|
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
|
[goog.events :as e]
|
||||||
[potok.core :as ptk]
|
[potok.core :as ptk]
|
||||||
|
[reitit.core :as r]
|
||||||
|
[uxbox.common.data :as d]
|
||||||
[uxbox.util.browser-history :as bhistory]
|
[uxbox.util.browser-history :as bhistory]
|
||||||
[uxbox.common.data :as d])
|
[uxbox.util.timers :as ts])
|
||||||
(:import
|
(:import
|
||||||
goog.Uri
|
goog.Uri
|
||||||
goog.Uri.QueryData))
|
goog.Uri.QueryData))
|
||||||
|
@ -120,21 +123,19 @@
|
||||||
(bhistory/enable! history)
|
(bhistory/enable! history)
|
||||||
(assoc state :history history)))
|
(assoc state :history history)))
|
||||||
|
|
||||||
ptk/WatchEvent
|
ptk/EffectEvent
|
||||||
(watch [_ state stream]
|
(effect [_ state stream]
|
||||||
(let [stoper (rx/filter (ptk/type? ::initialize-history) stream)
|
(let [stoper (rx/filter (ptk/type? ::initialize-history) stream)
|
||||||
history (:history state)
|
history (:history state)
|
||||||
router (:router state)]
|
router (:router state)]
|
||||||
(rx/merge
|
(ts/schedule #(on-change router (.getToken history)))
|
||||||
(->> (rx/of (on-change router (.getToken history)))
|
(->> (rx/create (fn [sink]
|
||||||
(rx/observe-on :asap))
|
|
||||||
(->> (rx/create (fn [sink]
|
|
||||||
(let [key (e/listen history "navigate" #(sink (.-token %)))]
|
(let [key (e/listen history "navigate" #(sink (.-token %)))]
|
||||||
(fn []
|
(fn []
|
||||||
(bhistory/disable! history)
|
(bhistory/disable! history)
|
||||||
(e/unlistenByKey key)))))
|
(e/unlistenByKey key)))))
|
||||||
(rx/map #(on-change router %))
|
(rx/take-until stoper)
|
||||||
(rx/take-until stoper)))))))
|
(rx/subs #(on-change router %)))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue