diff --git a/frontend/src/app/main.cljs b/frontend/src/app/main.cljs index 3df6cd88d3..a7fe969202 100644 --- a/frontend/src/app/main.cljs +++ b/frontend/src/app/main.cljs @@ -14,7 +14,7 @@ [app.config :as cfg] [app.main.data.auth :as da] [app.main.data.messages :as dm] - [app.main.data.users :as udu] + [app.main.data.users :as du] [app.main.repo :as rp] [app.main.store :as st] [app.main.ui :as ui] @@ -91,8 +91,7 @@ (st/emit! (rt/initialize-router ui/routes) (rt/initialize-history on-navigate) - (udu/fetch-profile) - (udu/fetch-user-teams))) + (du/fetch-profile-and-teams))) (defn reinit [] diff --git a/frontend/src/app/main/data/auth.cljs b/frontend/src/app/main/data/auth.cljs index 972f663629..8955fa194d 100644 --- a/frontend/src/app/main/data/auth.cljs +++ b/frontend/src/app/main/data/auth.cljs @@ -47,10 +47,10 @@ (watch [this state stream] (let [team-id (current-team-id profile) props (:props profile)] - (rx/merge - (rx/of (du/profile-fetched profile) - (rt/nav' :dashboard-projects {:team-id team-id})) - + (rx/concat + (rx/of (du/profile-fetched profile)) + (rx/of (du/fetch-teams)) + (rx/of (rt/nav' :dashboard-projects {:team-id team-id})) (when-not (:onboarding-viewed props) (->> (rx/of (modal/show {:type :onboarding})) (rx/delay 1000)))))))) diff --git a/frontend/src/app/main/data/dashboard.cljs b/frontend/src/app/main/data/dashboard.cljs index ad216b03ff..760e5b8f63 100644 --- a/frontend/src/app/main/data/dashboard.cljs +++ b/frontend/src/app/main/data/dashboard.cljs @@ -65,8 +65,6 @@ ;; Data Fetching ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; --- Fetch Team - (defn fetch-team [{:keys [id] :as params}] (letfn [(fetched [team state] @@ -117,7 +115,7 @@ (defn fetch-bundle [{:keys [id] :as params}] (us/assert ::us/uuid id) - (ptk/reify ::fetch-team + (ptk/reify ::fetch-bundle ptk/WatchEvent (watch [_ state stream] (let [profile (:profile state)] diff --git a/frontend/src/app/main/data/users.cljs b/frontend/src/app/main/data/users.cljs index bb15a05268..35a862df35 100644 --- a/frontend/src/app/main/data/users.cljs +++ b/frontend/src/app/main/data/users.cljs @@ -12,6 +12,7 @@ [app.config :as cf] [app.common.data :as d] [app.common.spec :as us] + [app.common.uuid :as uuid] [app.main.data.media :as di] [app.main.data.messages :as dm] [app.main.repo :as rp] @@ -47,19 +48,29 @@ ::lang ::theme])) -;; --- Profile Fetched +(defn fetch-teams + [] + (letfn [(on-fetched [state data] + (let [teams (d/index-by :id data)] + (assoc state :teams teams)))] + (ptk/reify ::fetch-teams + ptk/WatchEvent + (watch [_ state s] + (->> (rp/query! :teams) + (rx/map (fn [data] #(on-fetched % data)))))))) (defn profile-fetched [{:keys [fullname id] :as data}] (us/verify ::profile data) (ptk/reify ::profile-fetched + IDeref + (-deref [_] data) + ptk/UpdateEvent (update [_ state] (-> state (assoc :profile-id id) - (assoc :profile data) - ;; Safeguard if the profile is loaded after teams - (assoc-in [:profile :teams] (get-in state [:profile :teams])))) + (assoc :profile data))) ptk/EffectEvent (effect [_ state stream] @@ -73,12 +84,31 @@ (defn fetch-profile [] - (reify + (ptk/reify ::fetch-profile ptk/WatchEvent - (watch [_ state s] + (watch [_ state stream] (->> (rp/query! :profile) (rx/map profile-fetched))))) +(defn fetch-profile-and-teams + "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 ::fetch-profile-and-teams + ptk/WatchEvent + (watch [_ state 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)))))))))) + ;; --- Update Profile (defn update-profile @@ -204,24 +234,3 @@ (watch [_ state stream] (->> (rp/query :team-users {:team-id team-id}) (rx/map #(partial fetched %))))))) - -(defn user-teams-fetched [data] - (ptk/reify ::user-teams-fetched - ptk/UpdateEvent - (update [_ state] - (let [teams (->> data - (group-by :id) - (d/mapm #(first %2)))] - (assoc-in state [:profile :teams] teams))))) - -(defn fetch-user-teams [] - (ptk/reify ::fetch-user-teams - ptk/WatchEvent - (watch [_ state s] - (->> (rp/query! :teams) - (rx/map user-teams-fetched) - (rx/catch (fn [error] - (if (= (:type error) :not-found) - (rx/of (rt/nav :auth-login)) - (rx/empty)))))))) - diff --git a/frontend/src/app/main/refs.cljs b/frontend/src/app/main/refs.cljs index e7bc1e5d67..43af0686a0 100644 --- a/frontend/src/app/main/refs.cljs +++ b/frontend/src/app/main/refs.cljs @@ -32,6 +32,9 @@ (def profile (l/derived :profile st/state)) +(def teams + (l/derived :teams st/state)) + (def exception (l/derived :exception st/state)) diff --git a/frontend/src/app/main/ui/dashboard/sidebar.cljs b/frontend/src/app/main/ui/dashboard/sidebar.cljs index 9ef53c9003..de53bedac1 100644 --- a/frontend/src/app/main/ui/dashboard/sidebar.cljs +++ b/frontend/src/app/main/ui/dashboard/sidebar.cljs @@ -205,7 +205,7 @@ (mf/defc teams-selector-dropdown [{:keys [team profile locale] :as props}] (let [show-dropdown? (mf/use-state false) - teams (mf/use-state []) + teams (mf/deref refs/teams) on-create-clicked (mf/use-callback @@ -217,12 +217,6 @@ (da/set-current-team! team-id) (st/emit! (rt/nav :dashboard-projects {:team-id team-id}))))] - (mf/use-layout-effect - (mf/deps (:id team)) - (fn [] - (->> (rp/query! :teams) - (rx/subs #(reset! teams %))))) - [:ul.dropdown.teams-dropdown [:li.title (t locale "dashboard.switch-team")] [:hr] @@ -230,7 +224,7 @@ [:span.team-icon i/logo-icon] [:span.team-text (t locale "dashboard.your-penpot")]] - (for [team (remove :is-default @teams)] + (for [team (remove :is-default (vals teams))] [:* {:key (:id team)} [:li.team-name {:on-click (partial team-selected (:id team))} [:span.team-icon diff --git a/frontend/src/app/main/ui/viewer.cljs b/frontend/src/app/main/ui/viewer.cljs index 0c651fb495..d76418cf42 100644 --- a/frontend/src/app/main/ui/viewer.cljs +++ b/frontend/src/app/main/ui/viewer.cljs @@ -271,7 +271,6 @@ (mf/use-effect (mf/deps file-id page-id token) (fn [] - (prn "viewer-page$use-effect") (st/emit! (dv/initialize props)))) (mf/use-effect diff --git a/frontend/src/app/main/ui/viewer/header.cljs b/frontend/src/app/main/ui/viewer/header.cljs index e2eeff8a58..39eb8b84e2 100644 --- a/frontend/src/app/main/ui/viewer/header.cljs +++ b/frontend/src/app/main/ui/viewer/header.cljs @@ -188,12 +188,12 @@ total (count frames) locale (mf/deref i18n/locale) profile (mf/deref refs/profile) - anonymous? (= uuid/zero (:id profile)) + teams (mf/deref refs/teams) team-id (get-in data [:project :team-id]) - has-permission? (and (not anonymous?) - (contains? (:teams profile) team-id)) + has-permission? (and (not= uuid/zero (:id profile)) + (contains? teams team-id)) project-id (get-in data [:project :id]) file-id (get-in data [:file :id])