diff --git a/src/uxbox/data/auth.cljs b/src/uxbox/data/auth.cljs index 2cbf96f17..c6101d31f 100644 --- a/src/uxbox/data/auth.cljs +++ b/src/uxbox/data/auth.cljs @@ -17,6 +17,35 @@ [uxbox.locales :refer (tr)] [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 (defrecord LoggedIn [data] @@ -32,6 +61,10 @@ (-apply-effect [this state] (assoc! local-storage :uxbox/auth data))) +(defn logged-in? + [v] + (instance? LoggedIn v)) + (defn logged-in [data] (LoggedIn. data)) @@ -47,10 +80,12 @@ (let [params {:username username :password password :scope "webapp"}] - (->> (rp/do :login params) + (->> (rp/do :fetch/token params) + (rx/catch on-error) (rx/map :payload) - (rx/map logged-in) - (rx/catch on-error)))))) + (rx/mapcat #(rx/of (logged-in %) + (fetch-profile)))))))) + (def ^:const ^:private +login-schema+ {:username [sc/required sc/string] diff --git a/src/uxbox/repo/auth.cljs b/src/uxbox/repo/auth.cljs index d06920bf6..068d54bd2 100644 --- a/src/uxbox/repo/auth.cljs +++ b/src/uxbox/repo/auth.cljs @@ -11,30 +11,15 @@ [uxbox.repo.core :refer (-do url send!)] [uxbox.state :as ust])) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Login -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defmethod -do :fetch/profile + [type _] + (let [url (str url "/profile/me")] + (send! {:method :get :url url}))) -(defn- request-token - [params] +(defmethod -do :fetch/token + [type data] (let [url (str url "/auth/token")] (send! {:url url :method :post :auth false - :body params}))) - -(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))))) + :body data}))) diff --git a/src/uxbox/state.cljs b/src/uxbox/state.cljs index f66b34185..5b1739476 100644 --- a/src/uxbox/state.cljs +++ b/src/uxbox/state.cljs @@ -18,6 +18,7 @@ :project-filter ""} :route nil :auth (:uxbox/auth local-storage) + :profile nil :workspace nil :shapes-by-id {} :elements-by-id {} diff --git a/src/uxbox/ui.cljs b/src/uxbox/ui.cljs index abd509390..ee6a9c65d 100644 --- a/src/uxbox/ui.cljs +++ b/src/uxbox/ui.cljs @@ -15,6 +15,7 @@ [uxbox.router :as r] [uxbox.rstore :as rs] [uxbox.data.projects :as dp] + [uxbox.data.auth :as uda] [uxbox.ui.lightbox :as ui-lightbox] [uxbox.ui.auth :as ui-auth] [uxbox.ui.dashboard :as ui-dashboard] @@ -56,9 +57,16 @@ nil )))) +(defn app-will-mount + [own] + (rs/emit! (uda/fetch-profile) + (dp/fetch-projects)) + own) + (def app (mx/component {:render app-render + :will-mount app-will-mount :mixins [rum/reactive] :name "app"})) diff --git a/src/uxbox/ui/dashboard.cljs b/src/uxbox/ui/dashboard.cljs index 449cb25aa..261ecb605 100644 --- a/src/uxbox/ui/dashboard.cljs +++ b/src/uxbox/ui/dashboard.cljs @@ -32,8 +32,7 @@ (defn projects-page-will-mount [own] - (rs/emit! (dd/initialize :dashboard/projects) - (dp/fetch-projects)) + (rs/emit! (dd/initialize :dashboard/projects)) own) (defn projects-page-transfer-state diff --git a/src/uxbox/ui/users.cljs b/src/uxbox/ui/users.cljs index be214a5a0..2b6c6c152 100644 --- a/src/uxbox/ui/users.cljs +++ b/src/uxbox/ui/users.cljs @@ -47,20 +47,21 @@ ;; User Widget ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(def ^:static user-l - (as-> (l/in [:auth]) $ +(def ^:static profile-l + (as-> (l/key :profile) $ (l/focus-atom $ s/state))) (defn user-render [own] - (let [user (rum/react user-l) + (let [profile (rum/react profile-l) local (:rum/local own)] + (println "user-render" profile) (html [:div.user-zone {:on-mouse-enter #(swap! local assoc :open true) :on-mouse-leave #(swap! local assoc :open false)} - [:span (:fullname user)] + [:span (:fullname profile)] [:img {:border "0" - :src (:photo user)}] + :src (:photo profile "/images/favicon.png")}] (user-menu (:open @local))]))) (def user diff --git a/src/uxbox/ui/workspace.cljs b/src/uxbox/ui/workspace.cljs index 01340ec73..685693d7d 100644 --- a/src/uxbox/ui/workspace.cljs +++ b/src/uxbox/ui/workspace.cljs @@ -34,11 +34,9 @@ [own] (let [[projectid pageid] (:rum/props own)] (rs/emit! (dw/initialize projectid pageid) - (dp/fetch-projects) (udp/fetch-pages projectid) (udh/fetch-page-history pageid) (udh/fetch-pinned-page-history pageid)) - own)) (defn- workspace-did-mount