Improved login process and profile loading.

This commit is contained in:
Andrey Antukh 2016-03-31 19:14:06 +03:00
parent 3875780440
commit f837835be4
7 changed files with 61 additions and 34 deletions

View file

@ -17,6 +17,35 @@
[uxbox.locales :refer (tr)] [uxbox.locales :refer (tr)]
[uxbox.ui.messages :as uum])) [uxbox.ui.messages :as uum]))
;; --- Profile Fetched
(defrecord ProfileFetched [data]
rs/UpdateEvent
(-apply-update [this state]
(assoc state :profile data)))
(defn profile-fetched
[data]
(ProfileFetched. data))
;; --- Fetch Profile
(defrecord FetchProfile []
rs/WatchEvent
(-apply-watch [_ state s]
(println "FetchProfile")
(letfn [(on-error [err]
(uum/error (tr "errors.profile-fetch"))
(rx/empty))]
(->> (rp/do :fetch/profile)
(rx/catch on-error)
(rx/map :payload)
(rx/map profile-fetched)))))
(defn fetch-profile
[]
(FetchProfile.))
;; --- Logged In ;; --- Logged In
(defrecord LoggedIn [data] (defrecord LoggedIn [data]
@ -32,6 +61,10 @@
(-apply-effect [this state] (-apply-effect [this state]
(assoc! local-storage :uxbox/auth data))) (assoc! local-storage :uxbox/auth data)))
(defn logged-in?
[v]
(instance? LoggedIn v))
(defn logged-in (defn logged-in
[data] [data]
(LoggedIn. data)) (LoggedIn. data))
@ -47,10 +80,12 @@
(let [params {:username username (let [params {:username username
:password password :password password
:scope "webapp"}] :scope "webapp"}]
(->> (rp/do :login params) (->> (rp/do :fetch/token params)
(rx/catch on-error)
(rx/map :payload) (rx/map :payload)
(rx/map logged-in) (rx/mapcat #(rx/of (logged-in %)
(rx/catch on-error)))))) (fetch-profile))))))))
(def ^:const ^:private +login-schema+ (def ^:const ^:private +login-schema+
{:username [sc/required sc/string] {:username [sc/required sc/string]

View file

@ -11,30 +11,15 @@
[uxbox.repo.core :refer (-do url send!)] [uxbox.repo.core :refer (-do url send!)]
[uxbox.state :as ust])) [uxbox.state :as ust]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defmethod -do :fetch/profile
;; Login [type _]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (let [url (str url "/profile/me")]
(send! {:method :get :url url})))
(defn- request-token (defmethod -do :fetch/token
[params] [type data]
(let [url (str url "/auth/token")] (let [url (str url "/auth/token")]
(send! {:url url (send! {:url url
:method :post :method :post
:auth false :auth false
:body params}))) :body data})))
(defn- request-profile
[]
(rx/of {:fullname "Cirilla Fiona"
:photo "/images/favicon.png"
:username "cirilla"
:email "cirilla@uxbox.io"}))
(defmethod -do :login
[type data]
(->> (rx/zip (request-token data)
(request-profile))
(rx/map (fn [[authdata profile]]
(println authdata profile)
(println authdata profile)
(merge authdata profile)))))

View file

@ -18,6 +18,7 @@
:project-filter ""} :project-filter ""}
:route nil :route nil
:auth (:uxbox/auth local-storage) :auth (:uxbox/auth local-storage)
:profile nil
:workspace nil :workspace nil
:shapes-by-id {} :shapes-by-id {}
:elements-by-id {} :elements-by-id {}

View file

@ -15,6 +15,7 @@
[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]
[uxbox.data.auth :as uda]
[uxbox.ui.lightbox :as ui-lightbox] [uxbox.ui.lightbox :as ui-lightbox]
[uxbox.ui.auth :as ui-auth] [uxbox.ui.auth :as ui-auth]
[uxbox.ui.dashboard :as ui-dashboard] [uxbox.ui.dashboard :as ui-dashboard]
@ -56,9 +57,16 @@
nil nil
)))) ))))
(defn app-will-mount
[own]
(rs/emit! (uda/fetch-profile)
(dp/fetch-projects))
own)
(def app (def app
(mx/component (mx/component
{:render app-render {:render app-render
:will-mount app-will-mount
:mixins [rum/reactive] :mixins [rum/reactive]
:name "app"})) :name "app"}))

View file

@ -32,8 +32,7 @@
(defn projects-page-will-mount (defn projects-page-will-mount
[own] [own]
(rs/emit! (dd/initialize :dashboard/projects) (rs/emit! (dd/initialize :dashboard/projects))
(dp/fetch-projects))
own) own)
(defn projects-page-transfer-state (defn projects-page-transfer-state

View file

@ -47,20 +47,21 @@
;; User Widget ;; User Widget
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:static user-l (def ^:static profile-l
(as-> (l/in [:auth]) $ (as-> (l/key :profile) $
(l/focus-atom $ s/state))) (l/focus-atom $ s/state)))
(defn user-render (defn user-render
[own] [own]
(let [user (rum/react user-l) (let [profile (rum/react profile-l)
local (:rum/local own)] local (:rum/local own)]
(println "user-render" profile)
(html (html
[:div.user-zone {:on-mouse-enter #(swap! local assoc :open true) [:div.user-zone {:on-mouse-enter #(swap! local assoc :open true)
:on-mouse-leave #(swap! local assoc :open false)} :on-mouse-leave #(swap! local assoc :open false)}
[:span (:fullname user)] [:span (:fullname profile)]
[:img {:border "0" [:img {:border "0"
:src (:photo user)}] :src (:photo profile "/images/favicon.png")}]
(user-menu (:open @local))]))) (user-menu (:open @local))])))
(def user (def user

View file

@ -34,11 +34,9 @@
[own] [own]
(let [[projectid pageid] (:rum/props own)] (let [[projectid pageid] (:rum/props own)]
(rs/emit! (dw/initialize projectid pageid) (rs/emit! (dw/initialize projectid pageid)
(dp/fetch-projects)
(udp/fetch-pages projectid) (udp/fetch-pages projectid)
(udh/fetch-page-history pageid) (udh/fetch-page-history pageid)
(udh/fetch-pinned-page-history pageid)) (udh/fetch-pinned-page-history pageid))
own)) own))
(defn- workspace-did-mount (defn- workspace-did-mount