From 96a06e70bc776fdb6013736efed2f6abfd8933cd Mon Sep 17 00:00:00 2001 From: Ulises M Date: Fri, 20 Dec 2024 17:03:09 -0600 Subject: [PATCH] Make events to update a profile more generic --- .../profile/edit/accent_colour/events.cljs | 25 ++++++----- .../profile/edit/accent_colour/view.cljs | 3 +- .../contexts/profile/edit/header/events.cljs | 42 +++++++++++-------- .../contexts/profile/edit/header/view.cljs | 2 +- .../contexts/profile/edit/name/events.cljs | 29 +++++++------ .../contexts/profile/edit/name/view.cljs | 2 +- 6 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/status_im/contexts/profile/edit/accent_colour/events.cljs b/src/status_im/contexts/profile/edit/accent_colour/events.cljs index 8ce5ec7298..3bc263384b 100644 --- a/src/status_im/contexts/profile/edit/accent_colour/events.cljs +++ b/src/status_im/contexts/profile/edit/accent_colour/events.cljs @@ -4,27 +4,32 @@ [utils.re-frame :as rf])) (rf/reg-event-fx :profile/edit-accent-colour-success - (fn [_ [customization-color]] + (fn [_ [customization-color navigate-back? show-toast?]] {:fx [[:dispatch [:profile/save-local-accent-color customization-color]] - [:dispatch [:navigate-back]] - [:dispatch - [:toasts/upsert - {:type :positive - :theme :dark - :text (i18n/label :t/accent-colour-updated)}]]]})) + (when navigate-back? + [:dispatch [:navigate-back]]) + (when show-toast? + [:dispatch + [:toasts/upsert + {:type :positive + :theme :dark + :text (i18n/label :t/accent-colour-updated)}]])]})) (rf/reg-event-fx :profile/save-local-accent-color (fn [{:keys [db]} [customization-color]] {:db (assoc-in db [:profile/profile :customization-color] customization-color)})) (defn edit-accent-colour - [{:keys [db]} [customization-color]] + [{:keys [db]} + [{:keys [color navigate-back? show-toast?] + :or {navigate-back? true + show-toast? true}}]] (let [key-uid (get-in db [:profile/profile :key-uid])] {:fx [[:json-rpc/call [{:method "wakuext_setCustomizationColor" - :params [{:customizationColor customization-color + :params [{:customizationColor color :keyUid key-uid}] - :on-success [:profile/edit-accent-colour-success customization-color] + :on-success [:profile/edit-accent-colour-success color navigate-back? show-toast?] :on-error #(log/error "failed to edit accent color." {:error %})}]]]})) (rf/reg-event-fx :profile/edit-accent-colour edit-accent-colour) diff --git a/src/status_im/contexts/profile/edit/accent_colour/view.cljs b/src/status_im/contexts/profile/edit/accent_colour/view.cljs index b64648c78f..8ed57cadc8 100644 --- a/src/status_im/contexts/profile/edit/accent_colour/view.cljs +++ b/src/status_im/contexts/profile/edit/accent_colour/view.cljs @@ -58,7 +58,8 @@ {:type :primary :customization-color @unsaved-custom-color :on-press (fn [] - (rf/dispatch [:profile/edit-accent-colour @unsaved-custom-color]))} + (rf/dispatch [:profile/edit-accent-colour + {:color @unsaved-custom-color}]))} (i18n/label :t/save-colour)]]]])))) (defn view diff --git a/src/status_im/contexts/profile/edit/header/events.cljs b/src/status_im/contexts/profile/edit/header/events.cljs index 5e1f787ca3..ca826bc92e 100644 --- a/src/status_im/contexts/profile/edit/header/events.cljs +++ b/src/status_im/contexts/profile/edit/header/events.cljs @@ -11,19 +11,22 @@ (update db :profile/profile dissoc :images))})) (rf/reg-event-fx :profile/edit-profile-picture-success - (fn [_ [images]] + (fn [_ [show-toast? images]] (let [has-picture? (rf/sub [:profile/has-picture])] {:fx [[:dispatch [:profile/update-local-picture (reverse images)]] - [:dispatch - [:toasts/upsert - {:type :positive - :theme :dark - :text (i18n/label (if has-picture? - :t/profile-picture-updated - :t/profile-picture-added))}]]]}))) + (when show-toast? + [:dispatch + [:toasts/upsert + {:type :positive + :theme :dark + :text (i18n/label (if has-picture? + :t/profile-picture-updated + :t/profile-picture-added))}]])]}))) (defn edit-profile-picture - [{:keys [db]} [picture crop-width crop-height]] + [{:keys [db]} + [{:keys [picture crop-width crop-height show-toast?] + :or {show-toast? true}}]] (let [key-uid (get-in db [:profile/profile :key-uid]) crop-width (or crop-width profile-picture-picker/crop-size) crop-height (or crop-height profile-picture-picker/crop-size) @@ -31,25 +34,28 @@ {:fx [[:json-rpc/call [{:method "multiaccounts_storeIdentityImage" :params [key-uid path 0 0 crop-width crop-height] - :on-success [:profile/edit-profile-picture-success]}]]]})) + :on-success [:profile/edit-profile-picture-success show-toast?]}]]]})) (rf/reg-event-fx :profile/edit-picture edit-profile-picture) (rf/reg-event-fx :profile/delete-profile-picture-success - (fn [_] + (fn [_ [show-toast?]] {:fx [[:dispatch [:profile/update-local-picture nil]] - [:dispatch - [:toasts/upsert - {:type :positive - :theme :dark - :text (i18n/label :t/profile-picture-removed)}]]]})) + (when show-toast? + [:dispatch + [:toasts/upsert + {:type :positive + :theme :dark + :text (i18n/label :t/profile-picture-removed)}]])]})) (defn delete-profile-picture - [{:keys [db]}] + [{:keys [db]} + [{:keys [show-toast?] + :or {show-toast? true}}]] (let [key-uid (get-in db [:profile/profile :key-uid])] {:fx [[:json-rpc/call [{:method "multiaccounts_deleteIdentityImage" :params [key-uid] - :on-success [:profile/delete-profile-picture-success]}]]]})) + :on-success [:profile/delete-profile-picture-success show-toast?]}]]]})) (rf/reg-event-fx :profile/delete-picture delete-profile-picture) diff --git a/src/status_im/contexts/profile/edit/header/view.cljs b/src/status_im/contexts/profile/edit/header/view.cljs index 8aa42a98ba..981d71de50 100644 --- a/src/status_im/contexts/profile/edit/header/view.cljs +++ b/src/status_im/contexts/profile/edit/header/view.cljs @@ -16,7 +16,7 @@ has-picture? (rf/sub [:profile/has-picture]) on-change-profile-pic (fn [picture] (if picture - (rf/dispatch [:profile/edit-picture picture]) + (rf/dispatch [:profile/edit-picture {:picture picture}]) (rf/dispatch [:profile/delete-picture])))] [rn/view {:key :edit-profile diff --git a/src/status_im/contexts/profile/edit/name/events.cljs b/src/status_im/contexts/profile/edit/name/events.cljs index c69b260832..e651769da1 100644 --- a/src/status_im/contexts/profile/edit/name/events.cljs +++ b/src/status_im/contexts/profile/edit/name/events.cljs @@ -5,21 +5,26 @@ [utils.re-frame :as rf])) (rf/reg-event-fx :profile/edit-profile-name-success - (fn [_] - {:fx [[:dispatch [:navigate-back]] - [:dispatch - [:toasts/upsert - {:type :positive - :theme :dark - :text (i18n/label :t/name-updated)}]]]})) + (fn [{:keys [db]} [display-name navigate-back? show-toast?]] + {:db (assoc-in db [:profile/profile :display-name] display-name) + :fx [(when navigate-back? + [:dispatch [:navigate-back]]) + (when show-toast? + [:dispatch + [:toasts/upsert + {:type :positive + :theme :dark + :text (i18n/label :t/name-updated)}]])]})) (defn edit-profile-name - [{:keys [db]} [name]] - {:db (assoc-in db [:profile/profile :display-name] name) - :fx [[:json-rpc/call + [_ + [{:keys [display-name navigate-back? show-toast?] + :or {navigate-back? true + show-toast? true}}]] + {:fx [[:json-rpc/call [{:method "wakuext_setDisplayName" - :params [name] - :on-success [:profile/edit-profile-name-success]}]]]}) + :params [display-name] + :on-success [:profile/edit-profile-name-success display-name navigate-back? show-toast?]}]]]}) (rf/reg-event-fx :profile/edit-name edit-profile-name) diff --git a/src/status_im/contexts/profile/edit/name/view.cljs b/src/status_im/contexts/profile/edit/name/view.cljs index 4d98c7dad6..b891c10d0a 100644 --- a/src/status_im/contexts/profile/edit/name/view.cljs +++ b/src/status_im/contexts/profile/edit/name/view.cljs @@ -67,7 +67,7 @@ {:type :primary :customization-color customization-color :on-press (fn [] - (rf/dispatch [:profile/edit-name @full-name])) + (rf/dispatch [:profile/edit-name {:display-name @full-name}])) :disabled? (boolean (or @typing? (string/blank? @full-name) (not (string/blank? @error-msg))))}