mirror of
https://github.com/penpot/penpot.git
synced 2025-06-08 08:31:38 +02:00
feat(frontend): refactor router
This commit is contained in:
parent
076c29e004
commit
26cdebece4
23 changed files with 418 additions and 293 deletions
|
@ -5,11 +5,10 @@
|
|||
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
|
||||
;; Copyright (c) 2015-2017 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||
|
||||
(ns ^:figwheel-hooks uxbox.main.ui
|
||||
(ns uxbox.main.ui
|
||||
(:require [beicon.core :as rx]
|
||||
[lentes.core :as l]
|
||||
[cuerdas.core :as str]
|
||||
[bide.core :as bc]
|
||||
[potok.core :as ptk]
|
||||
[uxbox.builtins.icons :as i]
|
||||
[uxbox.main.store :as st]
|
||||
|
@ -24,6 +23,7 @@
|
|||
[uxbox.main.ui.workspace :refer [workspace]]
|
||||
[uxbox.main.ui.shapes]
|
||||
[uxbox.util.messages :as uum]
|
||||
[uxbox.util.html-history :as html-history]
|
||||
[uxbox.util.router :as rt]
|
||||
[uxbox.util.timers :as ts]
|
||||
[uxbox.util.i18n :refer [tr]]
|
||||
|
@ -33,48 +33,10 @@
|
|||
|
||||
;; --- Constants
|
||||
|
||||
(def +unrestricted+
|
||||
#{:auth/login
|
||||
:auth/register
|
||||
:auth/recovery-request
|
||||
:auth/recovery})
|
||||
|
||||
(def restricted?
|
||||
(complement +unrestricted+))
|
||||
|
||||
(def route-ref
|
||||
(-> (l/key :route)
|
||||
(l/derive st/state)))
|
||||
|
||||
;; --- 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)
|
||||
|
||||
;; --- Main App (Component)
|
||||
|
||||
(defn app-will-mount
|
||||
|
@ -88,95 +50,45 @@
|
|||
:mixins [mx/reactive]}
|
||||
[]
|
||||
(let [route (mx/react route-ref)
|
||||
auth (mx/react st/auth-ref)
|
||||
location (:id route)
|
||||
params (:params route)]
|
||||
(if (and (restricted? location) (not auth))
|
||||
(do (ts/schedule 0 #(st/emit! (rt/navigate :auth/login))) nil)
|
||||
(case location
|
||||
:auth/login (auth/login-page)
|
||||
:auth/register (auth/register-page)
|
||||
:auth/recovery-request (auth/recovery-request-page)
|
||||
:auth/recovery (auth/recovery-page (:token params))
|
||||
:dashboard/projects (dashboard/projects-page)
|
||||
;; :dashboard/elements (dashboard/elements-page)
|
||||
:dashboard/icons (let [{:keys [id type]} params
|
||||
type (when (str/alpha? type) (keyword type))
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)]
|
||||
(dashboard/icons-page type id))
|
||||
auth (mx/react st/auth-ref)]
|
||||
(prn "main$app" route)
|
||||
(case (get-in route [:data :name])
|
||||
:auth/login (auth/login-page)
|
||||
:auth/register (auth/register-page)
|
||||
:auth/recovery-request (auth/recovery-request-page)
|
||||
:auth/recovery (let [token (get-in route [:params :path :token])]
|
||||
(auth/recovery-page token))
|
||||
:dashboard/projects (dashboard/projects-page)
|
||||
;; ;; :dashboard/elements (dashboard/elements-page)
|
||||
|
||||
:dashboard/images (let [{:keys [id type]} params
|
||||
type (when (str/alpha? type) (keyword type))
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)]
|
||||
(dashboard/images-page type id))
|
||||
:dashboard/icons (let [{:keys [id type]} (get-in route [:params :query])
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)
|
||||
type (when (str/alpha? type) (keyword type))]
|
||||
(dashboard/icons-page type id))
|
||||
|
||||
:dashboard/colors (let [{:keys [id type]} params
|
||||
type (when (str/alpha? type) (keyword type))
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)]
|
||||
(dashboard/colors-page type id))
|
||||
:settings/profile (settings/profile-page)
|
||||
:settings/password (settings/password-page)
|
||||
:settings/notifications (settings/notifications-page)
|
||||
:workspace/page (let [projectid (uuid (:project params))
|
||||
pageid (uuid (:page params))]
|
||||
(workspace projectid pageid))
|
||||
nil
|
||||
))))
|
||||
:dashboard/images (let [{:keys [id type]} (get-in route [:params :query])
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)
|
||||
type (when (str/alpha? type) (keyword type))]
|
||||
(dashboard/images-page type id))
|
||||
|
||||
;; --- Routes
|
||||
|
||||
(def routes
|
||||
[["/auth/login" :auth/login]
|
||||
["/auth/register" :auth/register]
|
||||
["/auth/recovery/request" :auth/recovery-request]
|
||||
["/auth/recovery/token/:token" :auth/recovery]
|
||||
["/settings/profile" :settings/profile]
|
||||
["/settings/password" :settings/password]
|
||||
["/settings/notifications" :settings/notifications]
|
||||
["/dashboard/projects" :dashboard/projects]
|
||||
["/dashboard/elements" :dashboard/elements]
|
||||
|
||||
["/dashboard/icons" :dashboard/icons]
|
||||
["/dashboard/icons/:type/:id" :dashboard/icons]
|
||||
["/dashboard/icons/:type" :dashboard/icons]
|
||||
|
||||
["/dashboard/images" :dashboard/images]
|
||||
["/dashboard/images/:type/:id" :dashboard/images]
|
||||
["/dashboard/images/:type" :dashboard/images]
|
||||
|
||||
["/dashboard/colors" :dashboard/colors]
|
||||
["/dashboard/colors/:type/:id" :dashboard/colors]
|
||||
["/dashboard/colors/:type" :dashboard/colors]
|
||||
|
||||
["/workspace/:project/:page" :workspace/page]])
|
||||
|
||||
;; --- Main Entry Point
|
||||
|
||||
(defn init-routes
|
||||
[]
|
||||
(rt/init st/store routes {:default :auth/login}))
|
||||
|
||||
(defn ^:export init
|
||||
[]
|
||||
(mx/mount (app) (dom/get-element "app"))
|
||||
(mx/mount (lightbox) (dom/get-element "lightbox"))
|
||||
(mx/mount (loader) (dom/get-element "loader")))
|
||||
|
||||
(defn reinit
|
||||
[]
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "app"))
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "lightbox"))
|
||||
(.unmountComponentAtNode js/ReactDOM (dom/get-element "loader"))
|
||||
(init))
|
||||
|
||||
(defn ^:after-load my-after-reload-callback []
|
||||
(reinit))
|
||||
:dashboard/colors (let [{:keys [id type]} (get-in route [:params :query])
|
||||
type (when (str/alpha? type) (keyword type))
|
||||
id (cond
|
||||
(str/digits? id) (parse-int id)
|
||||
(uuid-str? id) (uuid id)
|
||||
:else nil)]
|
||||
(dashboard/colors-page type id))
|
||||
:settings/profile (settings/profile-page)
|
||||
:settings/password (settings/password-page)
|
||||
:settings/notifications (settings/notifications-page)
|
||||
:workspace/page (let [projectid (uuid (get-in route [:params :path :project]))
|
||||
pageid (uuid (get-in route [:params :path :page]))]
|
||||
(workspace projectid pageid))
|
||||
nil
|
||||
)))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue