0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-27 09:16:17 -05:00
penpot/frontend/src/uxbox/main.cljs

101 lines
2.6 KiB
Text
Raw Normal View History

2016-03-01 20:18:42 +02:00
;; This Source Code Form is subject to the terms of the Mozilla Public
;; 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/.
;;
2019-07-01 19:40:01 +02:00
;; Copyright (c) 2015-2019 Andrey Antukh <niwi@niwi.nz>
2016-03-01 20:18:42 +02:00
2019-07-01 19:40:01 +02:00
(ns ^:figwheel-hooks uxbox.main
(:require
[cljs.spec.alpha :as s]
[rumext.alpha :as mf]
2019-07-01 19:40:01 +02:00
[uxbox.main.data.auth :refer [logout]]
[uxbox.main.data.users :as udu]
2019-07-01 19:40:01 +02:00
[uxbox.main.locales.en :as en]
[uxbox.main.locales.fr :as fr]
[uxbox.main.store :as st]
[uxbox.main.ui :as ui]
2019-07-01 19:40:01 +02:00
[uxbox.main.ui.lightbox :refer [lightbox]]
2019-08-02 20:18:05 +02:00
[uxbox.main.ui.modal :refer [modal]]
2019-07-01 19:40:01 +02:00
[uxbox.main.ui.loader :refer [loader]]
[uxbox.util.dom :as dom]
2019-07-09 13:39:49 +02:00
[uxbox.util.html.history :as html-history]
2019-07-01 19:40:01 +02:00
[uxbox.util.i18n :as i18n :refer [tr]]
[uxbox.util.messages :as uum]
[uxbox.util.router :as rt]
[uxbox.util.storage :refer [storage]]
2019-07-01 19:40:01 +02:00
[uxbox.util.timers :as ts]))
;; --- i18n
(declare reinit)
(s/check-asserts true)
2019-06-19 16:44:54 +02:00
(i18n/update-locales! (fn [locales]
(-> locales
(assoc "en" en/locales)
(assoc "fr" fr/locales))))
2019-06-19 16:44:54 +02:00
(i18n/on-locale-change!
(fn [new old]
(println "Locale changed from" old " to " new)
2019-07-01 19:40:01 +02:00
(reinit)))
;; --- Error Handling
(defn- on-navigate
[router path]
(let [match (rt/match router path)]
(prn "main$on-navigate" path)
2019-07-24 13:29:07 +02:00
2019-07-01 19:40:01 +02:00
(cond
2019-07-24 13:29:07 +02:00
(and (= path "") (:auth storage))
(st/emit! (rt/nav :dashboard-projects))
2019-07-24 13:29:07 +02:00
(and (= path "") (not (:auth storage)))
(st/emit! (rt/nav :auth/login))
2019-07-01 19:40:01 +02:00
(nil? match)
(prn "TODO 404 main")
2019-07-01 19:40:01 +02:00
:else
(st/emit! #(assoc % :route match)))))
(defn init-ui
[]
(let [router (rt/init ui/routes)
2019-07-01 19:40:01 +02:00
cpath (deref html-history/path)]
(st/emit! #(assoc % :router router))
(add-watch html-history/path ::main #(on-navigate router %4))
(when (:auth storage)
(st/emit! udu/fetch-profile))
2019-08-21 21:03:29 +00:00
(mf/mount (mf/element ui/app) (dom/get-element "app"))
(mf/mount (lightbox) (dom/get-element "lightbox"))
2019-08-02 20:18:05 +02:00
(mf/mount (mf/element modal) (dom/get-element "modal"))
2019-08-22 20:00:33 +02:00
(mf/mount (mf/element loader) (dom/get-element "loader"))
2019-07-01 19:40:01 +02:00
(on-navigate router cpath)))
2015-06-18 19:35:50 +02:00
(def app-sym (.for js/Symbol "uxbox.app"))
(defn ^:export init
[]
(unchecked-set js/window app-sym "main")
(st/init)
2019-07-01 19:40:01 +02:00
(init-ui))
(defn reinit
[]
(remove-watch html-history/path ::main)
(mf/unmount (dom/get-element "app"))
(mf/unmount (dom/get-element "lightbox"))
(mf/unmount (dom/get-element "loader"))
2019-07-01 19:40:01 +02:00
(init-ui))
(defn ^:after-load after-load
[]
(when (= "main" (unchecked-get js/window app-sym))
(reinit)))