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
|
2019-11-26 10:55:38 +01:00
|
|
|
[cljs.spec.alpha :as s]
|
2019-07-24 00:48:24 +02:00
|
|
|
[rumext.alpha :as mf]
|
2019-07-01 19:40:01 +02:00
|
|
|
[uxbox.main.data.auth :refer [logout]]
|
2019-07-03 09:30:59 +02:00
|
|
|
[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]
|
2019-07-03 17:57:29 +02:00
|
|
|
[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]
|
2019-07-24 00:48:24 +02:00
|
|
|
[uxbox.util.storage :refer [storage]]
|
2019-07-01 19:40:01 +02:00
|
|
|
[uxbox.util.timers :as ts]))
|
|
|
|
|
|
|
|
;; --- i18n
|
|
|
|
|
|
|
|
(declare reinit)
|
2019-11-26 10:55:38 +01:00
|
|
|
(s/check-asserts true)
|
2019-06-19 16:44:54 +02:00
|
|
|
|
|
|
|
(i18n/update-locales! (fn [locales]
|
|
|
|
(-> locales
|
2019-07-24 00:48:24 +02:00
|
|
|
(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)]
|
2019-07-24 11:34:15 +02:00
|
|
|
(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))
|
|
|
|
|
|
|
|
(and (= path "") (not (:auth storage)))
|
|
|
|
(st/emit! (rt/nav :auth/login))
|
2019-07-01 19:40:01 +02:00
|
|
|
|
|
|
|
(nil? match)
|
2019-07-24 11:34:15 +02:00
|
|
|
(prn "TODO 404 main")
|
2019-07-01 19:40:01 +02:00
|
|
|
|
|
|
|
:else
|
|
|
|
(st/emit! #(assoc % :route match)))))
|
|
|
|
|
|
|
|
(defn init-ui
|
|
|
|
[]
|
2019-07-03 17:57:29 +02:00
|
|
|
(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))
|
|
|
|
|
2019-07-24 00:48:24 +02:00
|
|
|
(when (:auth storage)
|
2019-09-10 12:00:52 +02:00
|
|
|
(st/emit! udu/fetch-profile))
|
2019-07-03 09:30:59 +02:00
|
|
|
|
2019-08-21 21:03:29 +00:00
|
|
|
(mf/mount (mf/element ui/app) (dom/get-element "app"))
|
2019-07-24 00:48:24 +02:00
|
|
|
(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
|
|
|
|
2019-07-24 11:34:15 +02:00
|
|
|
(def app-sym (.for js/Symbol "uxbox.app"))
|
|
|
|
|
2016-06-14 17:39:44 +03:00
|
|
|
(defn ^:export init
|
2016-06-12 13:24:55 +03:00
|
|
|
[]
|
2019-07-24 11:34:15 +02:00
|
|
|
(unchecked-set js/window app-sym "main")
|
2017-01-13 22:01:13 +01:00
|
|
|
(st/init)
|
2019-07-01 19:40:01 +02:00
|
|
|
(init-ui))
|
|
|
|
|
|
|
|
(defn reinit
|
|
|
|
[]
|
|
|
|
(remove-watch html-history/path ::main)
|
2019-07-24 00:48:24 +02:00
|
|
|
(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
|
|
|
|
[]
|
2019-07-24 11:34:15 +02:00
|
|
|
(when (= "main" (unchecked-get js/window app-sym))
|
|
|
|
(reinit)))
|