mirror of
https://github.com/penpot/penpot.git
synced 2025-05-15 09:57:11 +02:00
feat(frontend): refactor router
This commit is contained in:
parent
076c29e004
commit
26cdebece4
23 changed files with 418 additions and 293 deletions
|
@ -2,14 +2,28 @@
|
|||
;; 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/.
|
||||
;;
|
||||
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
|
||||
;; Copyright (c) 2015-2019 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.main
|
||||
(:require [uxbox.main.store :as st]
|
||||
[uxbox.main.ui :as ui]
|
||||
[uxbox.main.locales.en :as en]
|
||||
[uxbox.main.locales.fr :as fr]
|
||||
[uxbox.util.i18n :as i18n]))
|
||||
(ns ^:figwheel-hooks uxbox.main
|
||||
(:require
|
||||
[rumext.core :as mx :include-macros true]
|
||||
[uxbox.main.data.auth :refer [logout]]
|
||||
[uxbox.main.locales.en :as en]
|
||||
[uxbox.main.locales.fr :as fr]
|
||||
[uxbox.main.store :as st]
|
||||
[uxbox.main.ui :refer [app]]
|
||||
[uxbox.main.ui.lightbox :refer [lightbox]]
|
||||
[uxbox.main.ui.loader :refer [loader]]
|
||||
[uxbox.util.dom :as dom]
|
||||
[uxbox.util.html-history :as html-history]
|
||||
[uxbox.util.i18n :as i18n :refer [tr]]
|
||||
[uxbox.util.messages :as uum]
|
||||
[uxbox.util.router :as rt]
|
||||
[uxbox.util.timers :as ts]))
|
||||
|
||||
;; --- i18n
|
||||
|
||||
(declare reinit)
|
||||
|
||||
(i18n/update-locales! (fn [locales]
|
||||
(-> locales
|
||||
|
@ -19,10 +33,101 @@
|
|||
(i18n/on-locale-change!
|
||||
(fn [new old]
|
||||
(println "Locale changed from" old " to " new)
|
||||
(ui/reinit)))
|
||||
(reinit)))
|
||||
|
||||
;; --- Error Handling
|
||||
|
||||
(defn- on-error
|
||||
"A default error handler."
|
||||
[{:keys [status] :as error}]
|
||||
(js/console.error "on-error:" (pr-str error))
|
||||
(js/console.error (.-stack error))
|
||||
(reset! st/loader false)
|
||||
(cond
|
||||
;; Unauthorized or Auth timeout
|
||||
(and (:status error)
|
||||
(or (= (:status error) 403)
|
||||
(= (:status error) 419)))
|
||||
(ts/schedule 100 #(st/emit! (logout)))
|
||||
|
||||
;; Conflict
|
||||
(= status 412)
|
||||
(ts/schedule 100 #(st/emit! (uum/error (tr "errors.conflict"))))
|
||||
|
||||
;; Network error
|
||||
(= (:status error) 0)
|
||||
(ts/schedule 100 #(st/emit! (uum/error (tr "errors.network"))))
|
||||
|
||||
;; Something else
|
||||
:else
|
||||
(ts/schedule 100 #(st/emit! (uum/error (tr "errors.generic"))))))
|
||||
|
||||
(set! st/*on-error* on-error)
|
||||
|
||||
(def routes
|
||||
[["/auth"
|
||||
["/login" :auth/login]
|
||||
["/register" :auth/register]
|
||||
["/recovery/request" :auth/recovery-request]
|
||||
["/recovery/token/:token" :auth/recovery]]
|
||||
["/settings"
|
||||
["/profile" :settings/profile]
|
||||
["/password" :settings/password]
|
||||
["/notifications" :settings/notifications]]
|
||||
["/dashboard"
|
||||
["/projects" :dashboard/projects]
|
||||
["/elements" :dashboard/elements]
|
||||
["/icons" :dashboard/icons]
|
||||
["/images" :dashboard/images]
|
||||
["/colors" :dashboard/colors]]
|
||||
["/workspace/:project/:page" :workspace/page]])
|
||||
|
||||
(defn- on-navigate
|
||||
[router path]
|
||||
(let [match (rt/match router path)]
|
||||
(prn "on-navigate" path match)
|
||||
(cond
|
||||
(and (= path "") (nil? match))
|
||||
(html-history/set-path! "/dashboard/projects")
|
||||
|
||||
(nil? match)
|
||||
(prn "TODO 404")
|
||||
|
||||
:else
|
||||
(st/emit! #(assoc % :route match)))))
|
||||
|
||||
(defn init-ui
|
||||
[]
|
||||
(let [router (rt/init routes)
|
||||
cpath (deref html-history/path)]
|
||||
|
||||
(st/emit! #(assoc % :router router))
|
||||
(add-watch html-history/path ::main #(on-navigate router %4))
|
||||
|
||||
(mx/mount (app) (dom/get-element "app"))
|
||||
(mx/mount (lightbox) (dom/get-element "lightbox"))
|
||||
(mx/mount (loader) (dom/get-element "loader"))
|
||||
|
||||
(on-navigate router cpath)))
|
||||
|
||||
(defn ^:export init
|
||||
[]
|
||||
(st/init)
|
||||
(ui/init-routes)
|
||||
(ui/init))
|
||||
(init-ui))
|
||||
|
||||
(defn reinit
|
||||
[]
|
||||
(remove-watch html-history/path ::main)
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "app"))
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "lightbox"))
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "loader"))
|
||||
(init-ui))
|
||||
|
||||
(defn ^:after-load after-load
|
||||
[]
|
||||
(reinit))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue