Make events to update a profile more generic

This commit is contained in:
Ulises M 2024-12-20 17:03:09 -06:00
parent d2d17bb57f
commit 96a06e70bc
No known key found for this signature in database
GPG Key ID: 5A15782EB758534E
6 changed files with 60 additions and 43 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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))))}