From ca743cc7cbf3eaf777febb1f16b669d06ae07231 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 11 Dec 2024 15:20:39 +0100 Subject: [PATCH] :bug: Fix incorrect profile refresh after onboarding end step and many other setting screens --- frontend/src/app/main.cljs | 22 +------- frontend/src/app/main/data/auth.cljs | 2 +- frontend/src/app/main/data/profile.cljs | 51 +++++++++++++------ .../src/app/main/ui/auth/verify_token.cljs | 4 +- .../app/main/ui/settings/change_email.cljs | 2 +- .../src/app/main/ui/settings/options.cljs | 5 +- .../src/app/main/ui/settings/profile.cljs | 7 ++- 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/frontend/src/app/main.cljs b/frontend/src/app/main.cljs index 0a584446b..9b759ab16 100644 --- a/frontend/src/app/main.cljs +++ b/frontend/src/app/main.cljs @@ -55,23 +55,6 @@ [] (mf/render! app-root (mf/element ui/app))) -(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 (dp/fetch-profile)) - (->> stream - (rx/filter dp/profile-fetched?) - (rx/take 1) - (rx/map deref) - (rx/mapcat (fn [profile] - (if (dp/is-authenticated? profile) - (rx/of (dp/initialize-profile profile)) - (rx/empty)))) - (rx/observe-on :async)))) - (defn initialize [] (ptk/reify ::initialize @@ -83,9 +66,8 @@ (watch [_ _ stream] (rx/merge (rx/of (ev/initialize) - (feat/initialize)) - - (initialize-profile stream) + (feat/initialize) + (dp/refresh-profile)) ;; Watch for profile deletion events (->> stream diff --git a/frontend/src/app/main/data/auth.cljs b/frontend/src/app/main/data/auth.cljs index dd8a39879..c1ba640b5 100644 --- a/frontend/src/app/main/data/auth.cljs +++ b/frontend/src/app/main/data/auth.cljs @@ -73,7 +73,7 @@ ptk/WatchEvent (watch [_ _ stream] (->> (rx/merge - (rx/of (dp/initialize-profile profile) + (rx/of (dp/set-profile profile) (ws/initialize) (dtm/fetch-teams)) diff --git a/frontend/src/app/main/data/profile.cljs b/frontend/src/app/main/data/profile.cljs index 6c43354fa..6ae164169 100644 --- a/frontend/src/app/main/data/profile.cljs +++ b/frontend/src/app/main/data/profile.cljs @@ -48,11 +48,11 @@ ;; --- EVENT: fetch-profile -(defn initialize-profile +(defn set-profile "Initialize profile state, only logged-in profile data should be passed to this event" [{:keys [id] :as profile}] - (ptk/reify ::initialize-profile + (ptk/reify ::set-profile IDeref (-deref [_] profile) @@ -93,6 +93,20 @@ (rx/map (partial ptk/data-event ::profile-fetched)) (rx/catch on-fetch-profile-exception))))) +(defn refresh-profile + [] + (ptk/reify ::refresh-profile + ptk/WatchEvent + (watch [_ _ stream] + (rx/merge + (rx/of (fetch-profile)) + (->> stream + (rx/filter profile-fetched?) + (rx/map deref) + (rx/filter is-authenticated?) + (rx/take 1) + (rx/map set-profile)))))) + ;; --- Update Profile (defn persist-profile @@ -106,29 +120,34 @@ params (select-keys profile [:fullname :lang :theme])] (->> (rp/cmd! :update-profile params) (rx/tap on-success) + (rx/map set-profile) (rx/catch on-error)))))) (defn update-profile - [data] + "Optimistic update of the current profile. + + Props are ignored because there is a specific event for updating + props" + [profile] (dm/assert! "expected valid profile data" - (check-profile data)) + (check-profile profile)) (ptk/reify ::update-profile ptk/WatchEvent (watch [_ state _] - (let [data (dissoc data :props) - profile (:profile state) - profile' (d/deep-merge profile data)] + (let [profile' (get state :profile) + profile (d/deep-merge profile' (dissoc profile :props))] - (rx/concat - (rx/of #(assoc % :profile profile')) + (rx/merge + (rx/of (set-profile profile)) - (when (not= (:theme profile) (:theme profile')) + (when (not= (:theme profile) + (:theme profile')) (rx/of (ptk/data-event ::ev/event {::ev/name "activate-theme" ::ev/origin "settings" - :theme (:theme profile')})))))))) + :theme (:theme profile)})))))))) ;; --- Toggle Theme @@ -178,7 +197,7 @@ ptk/WatchEvent (watch [_ _ _] (->> (rp/cmd! :cancel-email-change {}) - (rx/map (constantly (fetch-profile))))))) + (rx/map (constantly (refresh-profile))))))) ;; --- Update Password (Form) @@ -227,7 +246,7 @@ ptk/WatchEvent (watch [_ _ _] (->> (rp/cmd! :update-profile-props {:props props}) - (rx/map (constantly (fetch-profile))))))) + (rx/map (constantly (refresh-profile))))))) (defn mark-onboarding-as-viewed ([] (mark-onboarding-as-viewed nil)) @@ -239,7 +258,7 @@ props {:onboarding-viewed true :release-notes-viewed version}] (->> (rp/cmd! :update-profile-props {:props props}) - (rx/map (constantly (fetch-profile))))))))) + (rx/map (constantly (refresh-profile))))))))) (defn mark-questions-as-answered [onboarding-questions] @@ -253,7 +272,7 @@ (let [props {:onboarding-questions-answered true :onboarding-questions onboarding-questions}] (->> (rp/cmd! :update-profile-props {:props props}) - (rx/map (constantly (fetch-profile)))))))) + (rx/map (constantly (refresh-profile)))))))) ;; --- Update Photo @@ -283,7 +302,7 @@ (rx/map prepare) (rx/mapcat #(rp/cmd! :update-profile-photo %)) (rx/tap on-success) - (rx/map (constantly (fetch-profile))) + (rx/map (constantly (refresh-profile))) (rx/catch on-error)))))) (defn fetch-users diff --git a/frontend/src/app/main/ui/auth/verify_token.cljs b/frontend/src/app/main/ui/auth/verify_token.cljs index 01196186c..ec80e4e9a 100644 --- a/frontend/src/app/main/ui/auth/verify_token.cljs +++ b/frontend/src/app/main/ui/auth/verify_token.cljs @@ -34,7 +34,7 @@ (let [msg (tr "dashboard.notifications.email-changed-successfully")] (ts/schedule 100 #(st/emit! (ntf/success msg))) (st/emit! (rt/nav :settings-profile) - (du/fetch-profile)))) + (du/refresh-profile)))) (defmethod handle-token :auth [tdata] @@ -47,7 +47,7 @@ (let [team-id (:team-id tdata)] (st/emit! (ntf/success (tr "auth.notifications.team-invitation-accepted")) - (du/fetch-profile) + (du/refresh-profile) (dcm/go-to-dashboard-recent :team-id team-id))) :pending diff --git a/frontend/src/app/main/ui/settings/change_email.cljs b/frontend/src/app/main/ui/settings/change_email.cljs index 040364cff..5535c08c3 100644 --- a/frontend/src/app/main/ui/settings/change_email.cljs +++ b/frontend/src/app/main/ui/settings/change_email.cljs @@ -41,7 +41,7 @@ (defn- on-success [profile data] (if (:changed data) - (st/emit! (du/fetch-profile) + (st/emit! (du/refresh-profile) (modal/hide)) (let [message (tr "notifications.validation-email-sent" (:email profile))] (st/emit! (ntf/info message) diff --git a/frontend/src/app/main/ui/settings/options.cljs b/frontend/src/app/main/ui/settings/options.cljs index 1331d4772..f3ab76e28 100644 --- a/frontend/src/app/main/ui/settings/options.cljs +++ b/frontend/src/app/main/ui/settings/options.cljs @@ -22,9 +22,8 @@ [:theme {:optional true} [:string {:max 250}]]]) (defn- on-success - [profile] - (st/emit! (ntf/success (tr "notifications.profile-saved")) - (du/initialize-profile profile))) + [_] + (st/emit! (ntf/success (tr "notifications.profile-saved")))) (defn- on-submit [form _event] diff --git a/frontend/src/app/main/ui/settings/profile.cljs b/frontend/src/app/main/ui/settings/profile.cljs index 613208b88..763ee3c83 100644 --- a/frontend/src/app/main/ui/settings/profile.cljs +++ b/frontend/src/app/main/ui/settings/profile.cljs @@ -25,12 +25,15 @@ [:fullname [::sm/text {:max 250}]] [:email ::sm/email]]) +(defn- on-success + [_] + (st/emit! (ntf/success (tr "notifications.profile-saved")))) + (defn- on-submit [form _event] (let [data (:clean-data @form)] (st/emit! (du/update-profile data) - (du/persist-profile) - (ntf/success (tr "notifications.profile-saved"))))) + (du/persist-profile {:on-success on-success})))) ;; --- Profile Form