diff --git a/frontend/src/app/main.cljs b/frontend/src/app/main.cljs index 33e019b99..50690807d 100644 --- a/frontend/src/app/main.cljs +++ b/frontend/src/app/main.cljs @@ -50,6 +50,23 @@ (mf/mount (mf/element ui/app) (dom/get-element "app")) (mf/mount (mf/element modal) (dom/get-element "modal"))) +(defn- initialize-profile + "Event used mainly on application bootstrap; it fetches the profile + and if and only if the fetched profile corresponds to an + authenticated user; proceed to fetch teams." + [stream] + (rx/merge + (rx/of (du/fetch-profile)) + (->> stream + (rx/filter (ptk/type? ::profile-fetched)) + (rx/take 1) + (rx/map deref) + (rx/mapcat (fn [profile] + (if (du/is-authenticated? profile) + (rx/of (du/fetch-teams)) + (rx/empty)))) + (rx/observe-on :async)))) + (defn initialize [] (ptk/reify ::initialize @@ -61,14 +78,19 @@ (watch [_ _ stream] (rx/merge (rx/of (ev/initialize) - (feat/initialize) - (du/initialize-profile)) + (feat/initialize)) + (initialize-profile stream) + + ;; Once profile is fetched, initialize all penpot application + ;; routes (->> stream (rx/filter du/profile-fetched?) (rx/take 1) (rx/map #(rt/init-routes))) + ;; Once profile fetched and the current user is authenticated, + ;; proceed to initialize the websockets connection. (->> stream (rx/filter du/profile-fetched?) (rx/map deref) diff --git a/frontend/src/app/main/data/users.cljs b/frontend/src/app/main/data/users.cljs index 92d2e8bb1..06ff50642 100644 --- a/frontend/src/app/main/data/users.cljs +++ b/frontend/src/app/main/data/users.cljs @@ -117,28 +117,6 @@ (->> (rp/cmd! :get-profile) (rx/map profile-fetched))))) -;; --- EVENT: INITIALIZE PROFILE - -(defn initialize-profile - "Event used mainly on application bootstrap; it fetches the profile - and if and only if the fetched profile corresponds to an - authenticated user; proceed to fetch teams." - [] - (ptk/reify ::initialize-profile - ptk/WatchEvent - (watch [_ _ stream] - (rx/merge - (rx/of (fetch-profile)) - (->> stream - (rx/filter (ptk/type? ::profile-fetched)) - (rx/take 1) - (rx/map deref) - (rx/mapcat (fn [profile] - (if (= uuid/zero (:id profile)) - (rx/empty) - (rx/of (fetch-teams))))) - (rx/observe-on :async)))))) - ;; --- EVENT: login (defn- logged-in @@ -164,7 +142,8 @@ (when (is-authenticated? profile) (->> (rx/of (profile-fetched profile) (fetch-teams) - (get-redirect-event)) + (get-redirect-event) + (ws/initialize)) (rx/observe-on :async))))))) (declare login-from-register)