Redirect to dashboad when logged user enters to login page.

This commit is contained in:
Andrey Antukh 2016-04-26 19:45:26 +03:00
parent 483043202a
commit 468f6a27e1
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
5 changed files with 51 additions and 38 deletions

View file

@ -7,7 +7,8 @@
(ns uxbox.core (ns uxbox.core
(:require-macros [uxbox.util.syntax :refer [define-once]]) (: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.router :as rt]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.ui :as ui])) [uxbox.ui :as ui]))

View file

@ -21,33 +21,40 @@
(as-> (l/in [:route]) $ (as-> (l/in [:route]) $
(l/focus-atom $ s/state))) (l/focus-atom $ s/state)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; --- Update Location (Event)
;; Events
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (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 (defn update-location
[{:keys [handler route-params] :as params}] [{:keys [handler route-params] :as params}]
(reify (UpdateLocation. handler route-params))
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)))))
(defn navigate ;; --- Navigate (Event)
([id] (navigate id nil))
([id params] (defrecord Navigate [id params]
{:pre [(keyword? id)]}
(reify
rs/EffectEvent rs/EffectEvent
(-apply-effect [_ state] (-apply-effect [_ state]
;; (println "navigate" id params) ;; (println "navigate" id params)
(let [loc (merge {:handler id} (let [loc (merge {:handler id}
(when params (when params
{:route-params params}))] {:route-params params}))]
(bidi.router/set-location! @+router+ loc)))))) (bidi.router/set-location! @+router+ loc))))
(defn navigate
([id] (navigate id nil))
([id params]
{:pre [(keyword? id)]}
(Navigate. id params)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Router declaration ;; Router declaration

View file

@ -13,6 +13,14 @@
(defonce state (atom {})) (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 (defn get-initial-state
[] []
{:dashboard {:project-order :name {:dashboard {:project-order :name

View file

@ -11,7 +11,7 @@
[goog.dom :as gdom] [goog.dom :as gdom]
[rum.core :as rum] [rum.core :as rum]
[lentes.core :as l] [lentes.core :as l]
[uxbox.state :as s] [uxbox.state :as st]
[uxbox.router :as r] [uxbox.router :as r]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.data.projects :as dp] [uxbox.data.projects :as dp]
@ -25,16 +25,6 @@
[uxbox.ui.mixins :as mx] [uxbox.ui.mixins :as mx]
[uxbox.ui.shapes])) [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 ;; --- Constants
(def ^:const +unrestricted+ #{:auth/login}) (def ^:const +unrestricted+ #{:auth/login})
@ -45,7 +35,7 @@
(defn app-render (defn app-render
[own] [own]
(let [route (rum/react r/route-l) (let [route (rum/react r/route-l)
auth (rum/react auth-data-l) auth (rum/react st/auth-l)
location (:id route) location (:id route)
params (:params route)] params (:params route)]
(if (and (restricted? location) (not auth)) (if (and (restricted? location) (not auth))
@ -67,7 +57,7 @@
(defn app-will-mount (defn app-will-mount
[own] [own]
(when @auth-data-l (when @st/auth-l
(rs/emit! (udu/fetch-profile))) (rs/emit! (udu/fetch-profile)))
own) own)
@ -82,7 +72,7 @@
(defn loader-render (defn loader-render
[own] [own]
(when (rum/react loader-l) (when (rum/react st/loader-l)
(html (html
[:div.loader-content i/loader]))) [:div.loader-content i/loader])))

View file

@ -3,8 +3,8 @@
[lentes.core :as l] [lentes.core :as l]
[cuerdas.core :as str] [cuerdas.core :as str]
[rum.core :as rum] [rum.core :as rum]
[uxbox.router :as r] [uxbox.router :as rt]
[uxbox.state :as s] [uxbox.state :as st]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.data.auth :as da] [uxbox.data.auth :as da]
[uxbox.data.messages :as udm] [uxbox.data.messages :as udm]
@ -67,11 +67,18 @@
:value "Continue" :value "Continue"
:type "submit"}] :type "submit"}]
[:div.login-links [:div.login-links
[:a {:on-click #(r/go :auth/recover-password)} "Forgot your password?"] [:a {:on-click #(rt/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/register)} "Don't have an account?"]]]]]])))
(defn- login-will-mount
[own]
(when @st/auth-l
(rt/go :dashboard/projects))
own)
(def ^:const login (def ^:const login
(mx/component (mx/component
{:render #(login-render % (:rum/local %)) {:render #(login-render % (:rum/local %))
:will-mount login-will-mount
:name "login" :name "login"
:mixins [(mx/local)]})) :mixins [(mx/local)]}))