diff --git a/src/uxbox/core.cljs b/src/uxbox/core.cljs index 1deef2cb77..436c279abc 100644 --- a/src/uxbox/core.cljs +++ b/src/uxbox/core.cljs @@ -7,7 +7,8 @@ (ns uxbox.core (:require-macros [uxbox.util.syntax :refer [define-once]]) - (:require [uxbox.state :as st] + (:require [beicon.core :as rx] + [uxbox.state :as st] [uxbox.router :as rt] [uxbox.rstore :as rs] [uxbox.ui :as ui])) diff --git a/src/uxbox/router.cljs b/src/uxbox/router.cljs index 5c3c79844f..4774af3cb2 100644 --- a/src/uxbox/router.cljs +++ b/src/uxbox/router.cljs @@ -21,33 +21,40 @@ (as-> (l/in [:route]) $ (l/focus-atom $ s/state))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Events -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; --- Update Location (Event) + +(defrecord UpdateLocation [id params] + rs/UpdateEvent + (-apply-update [_ state] + (let [route (merge {:id id} + (when params + {:params params}))] + (assoc state :route route)))) + +(defn update-location? + [v] + (instance? UpdateLocation v)) (defn update-location [{:keys [handler route-params] :as params}] - (reify - rs/UpdateEvent - (-apply-update [_ state] - ;; (println "update-location" handler route-params) - (let [route (merge {:id handler} - (when route-params - {:params route-params}))] - (assoc state :route route))))) + (UpdateLocation. handler route-params)) + +;; --- Navigate (Event) + +(defrecord Navigate [id params] + rs/EffectEvent + (-apply-effect [_ state] + ;; (println "navigate" id params) + (let [loc (merge {:handler id} + (when params + {:route-params params}))] + (bidi.router/set-location! @+router+ loc)))) (defn navigate ([id] (navigate id nil)) ([id params] {:pre [(keyword? id)]} - (reify - rs/EffectEvent - (-apply-effect [_ state] - ;; (println "navigate" id params) - (let [loc (merge {:handler id} - (when params - {:route-params params}))] - (bidi.router/set-location! @+router+ loc)))))) + (Navigate. id params))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Router declaration diff --git a/src/uxbox/state.cljs b/src/uxbox/state.cljs index 95658dd2d6..1382a1e0ea 100644 --- a/src/uxbox/state.cljs +++ b/src/uxbox/state.cljs @@ -13,6 +13,14 @@ (defonce state (atom {})) +(def ^:const auth-l + (-> (l/key :auth) + (l/focus-atom state))) + +(def ^:const loader-l + (-> (l/key :loader) + (l/focus-atom state))) + (defn get-initial-state [] {:dashboard {:project-order :name diff --git a/src/uxbox/ui.cljs b/src/uxbox/ui.cljs index 2a38375a9c..c0e67633a2 100644 --- a/src/uxbox/ui.cljs +++ b/src/uxbox/ui.cljs @@ -11,7 +11,7 @@ [goog.dom :as gdom] [rum.core :as rum] [lentes.core :as l] - [uxbox.state :as s] + [uxbox.state :as st] [uxbox.router :as r] [uxbox.rstore :as rs] [uxbox.data.projects :as dp] @@ -25,16 +25,6 @@ [uxbox.ui.mixins :as mx] [uxbox.ui.shapes])) -;; --- Lentes - -(def ^:const auth-data-l - (-> (l/key :auth) - (l/focus-atom s/state))) - -(def ^:const loader-l - (-> (l/key :loader) - (l/focus-atom s/state))) - ;; --- Constants (def ^:const +unrestricted+ #{:auth/login}) @@ -45,7 +35,7 @@ (defn app-render [own] (let [route (rum/react r/route-l) - auth (rum/react auth-data-l) + auth (rum/react st/auth-l) location (:id route) params (:params route)] (if (and (restricted? location) (not auth)) @@ -67,7 +57,7 @@ (defn app-will-mount [own] - (when @auth-data-l + (when @st/auth-l (rs/emit! (udu/fetch-profile))) own) @@ -82,7 +72,7 @@ (defn loader-render [own] - (when (rum/react loader-l) + (when (rum/react st/loader-l) (html [:div.loader-content i/loader]))) diff --git a/src/uxbox/ui/auth.cljs b/src/uxbox/ui/auth.cljs index 8ca73950de..c3643551da 100644 --- a/src/uxbox/ui/auth.cljs +++ b/src/uxbox/ui/auth.cljs @@ -3,8 +3,8 @@ [lentes.core :as l] [cuerdas.core :as str] [rum.core :as rum] - [uxbox.router :as r] - [uxbox.state :as s] + [uxbox.router :as rt] + [uxbox.state :as st] [uxbox.rstore :as rs] [uxbox.data.auth :as da] [uxbox.data.messages :as udm] @@ -67,11 +67,18 @@ :value "Continue" :type "submit"}] [:div.login-links - [:a {:on-click #(r/go :auth/recover-password)} "Forgot your password?"] - [:a {:on-click #(r/go :auth/register)} "Don't have an account?"]]]]]]))) + [:a {:on-click #(rt/go :auth/recover-password)} "Forgot your password?"] + [:a {:on-click #(rt/go :auth/register)} "Don't have an account?"]]]]]]))) + +(defn- login-will-mount + [own] + (when @st/auth-l + (rt/go :dashboard/projects)) + own) (def ^:const login (mx/component {:render #(login-render % (:rum/local %)) + :will-mount login-will-mount :name "login" :mixins [(mx/local)]}))