From 1eb9e87383d81a51e6932d2aa2c0940f97828977 Mon Sep 17 00:00:00 2001 From: Ajay Sivan Date: Wed, 10 Jan 2024 09:15:17 +0530 Subject: [PATCH] Move selected addresses for permissions to app db (#18430) --- .../actions/accounts_selection/view.cljs | 19 ++-- .../addresses_for_permissions/view.cljs | 95 +++++++++---------- .../contexts/communities/events.cljs | 37 +++++++- .../contexts/communities/events_test.cljs | 19 ++++ src/status_im/subs/root.cljs | 2 +- 5 files changed, 109 insertions(+), 63 deletions(-) create mode 100644 src/status_im/contexts/communities/events_test.cljs diff --git a/src/status_im/contexts/communities/actions/accounts_selection/view.cljs b/src/status_im/contexts/communities/actions/accounts_selection/view.cljs index 29df348ae7a..5778b2346c7 100644 --- a/src/status_im/contexts/communities/actions/accounts_selection/view.cljs +++ b/src/status_im/contexts/communities/actions/accounts_selection/view.cljs @@ -22,16 +22,15 @@ (defn f-view-internal [] - (let [{id :community-id} (rf/sub [:get-screen-params]) - {:keys [name color images]} (rf/sub [:communities/community id]) - accounts (rf/sub [:wallet/accounts-with-customization-color]) - addresses-for-permissions (rf/sub [:communities/addresses-for-permissions]) - selected-accounts (filter #(contains? addresses-for-permissions - (:address %)) - accounts)] + (let [{id :community-id} (rf/sub [:get-screen-params]) + {:keys [name color images]} (rf/sub [:communities/community id]) + accounts (rf/sub [:wallet/accounts-with-customization-color]) + selected-permission-addresses (rf/sub [:communities/selected-permission-addresses]) + selected-accounts (filter #(contains? selected-permission-addresses + (:address %)) + accounts)] (rn/use-effect (fn [] - (rf/dispatch [:communities/set-addresses-for-permissions - (set (map :address accounts))])) + (rf/dispatch [:communities/initialize-permission-addresses])) []) [rn/view {:style style/container} [quo/page-nav @@ -87,7 +86,7 @@ :track-text (i18n/label :t/slide-to-request-to-join) :track-icon :i/face-id :customization-color color - :on-complete #(join-community-and-navigate-back id addresses-for-permissions)}]]])) + :on-complete #(join-community-and-navigate-back id selected-permission-addresses)}]]])) (defn view [] diff --git a/src/status_im/contexts/communities/actions/addresses_for_permissions/view.cljs b/src/status_im/contexts/communities/actions/addresses_for_permissions/view.cljs index a137fab4007..bad0402b5ed 100644 --- a/src/status_im/contexts/communities/actions/addresses_for_permissions/view.cljs +++ b/src/status_im/contexts/communities/actions/addresses_for_permissions/view.cljs @@ -2,7 +2,6 @@ (:require [quo.core :as quo] [quo.foundations.colors :as colors] [react-native.core :as rn] - [reagent.core :as reagent] [status-im.common.not-implemented :as not-implemented] [status-im.contexts.communities.actions.addresses-for-permissions.style :as style] [utils.i18n :as i18n] @@ -16,11 +15,9 @@ :emoji (:emoji item) :customization-color (:customization-color item)} :token-details [] - :checked? (contains? @selected-addresses (:address item)) - :on-change (fn [checked?] - (if checked? - (swap! selected-addresses conj (:address item)) - (swap! selected-addresses disj (:address item)))) + :checked? (contains? selected-addresses (:address item)) + :on-change #(rf/dispatch [:communities/toggle-selected-permission-address + (:address item)]) :container-style {:margin-bottom 8}}]) (defn view @@ -28,49 +25,49 @@ (let [{id :community-id} (rf/sub [:get-screen-params]) {:keys [name color images]} (rf/sub [:communities/community id]) accounts (rf/sub [:wallet/accounts-with-customization-color]) - selected-addresses (reagent/atom (rf/sub [:communities/addresses-for-permissions]))] - (fn [] - [rn/safe-area-view {:style style/container} - [quo/drawer-top - {:type :context-tag - :title (i18n/label :t/addresses-for-permissions) - :community-name name - :button-icon :i/info - :on-button-press not-implemented/alert - :community-logo (get-in images [:thumbnail :uri]) - :customization-color color}] + selected-addresses (rf/sub [:communities/selected-permission-addresses])] + [rn/safe-area-view {:style style/container} + [quo/drawer-top + {:type :context-tag + :title (i18n/label :t/addresses-for-permissions) + :community-name name + :button-icon :i/info + :on-button-press not-implemented/alert + :community-logo (get-in images [:thumbnail :uri]) + :customization-color color}] - [rn/flat-list - {:render-fn account-item - :render-data selected-addresses - :content-container-style {:padding 20} - :key-fn :address - :data accounts}] + [rn/flat-list + {:render-fn account-item + :render-data selected-addresses + :content-container-style {:padding 20} + :key-fn :address + :data accounts}] - (when (empty? @selected-addresses) - [rn/view - {:style style/error-message} - [quo/icon - :i/info - {:color colors/danger-50 - :size 16}] - [quo/text - {:size :paragraph-2 - :style {:color colors/danger-50}} - (i18n/label :t/no-addresses-selected)]]) + (when (empty? selected-addresses) + [rn/view + {:style style/error-message} + [quo/icon + :i/info + {:color colors/danger-50 + :size 16}] + [quo/text + {:size :paragraph-2 + :style {:color colors/danger-50}} + (i18n/label :t/no-addresses-selected)]]) - [rn/view {:style style/buttons} - [quo/button - {:type :grey - :container-style {:flex 1} - :on-press #(rf/dispatch [:navigate-back])} - (i18n/label :t/cancel)] - [quo/button - {:container-style {:flex 1} - :customization-color color - :disabled? (empty? @selected-addresses) - :on-press (fn [] - (rf/dispatch [:communities/set-addresses-for-permissions - @selected-addresses]) - (rf/dispatch [:navigate-back]))} - (i18n/label :t/confirm-changes)]]]))) + [rn/view {:style style/buttons} + [quo/button + {:type :grey + :container-style {:flex 1} + :on-press (fn [] + (rf/dispatch [:communities/reset-selected-permission-addresses]) + (rf/dispatch [:navigate-back]))} + (i18n/label :t/cancel)] + [quo/button + {:container-style {:flex 1} + :customization-color color + :disabled? (empty? selected-addresses) + :on-press (fn [] + (rf/dispatch [:communities/update-previous-permission-addresses]) + (rf/dispatch [:navigate-back]))} + (i18n/label :t/confirm-changes)]]])) diff --git a/src/status_im/contexts/communities/events.cljs b/src/status_im/contexts/communities/events.cljs index acc74faa0c4..8aa7defea4f 100644 --- a/src/status_im/contexts/communities/events.cljs +++ b/src/status_im/contexts/communities/events.cljs @@ -185,6 +185,37 @@ :on-success #(rf/dispatch [:communities/fetched-collapsed-categories-success %]) :on-error #(log/error "failed to fetch collapsed community categories" %)}]})) -(rf/reg-event-fx :communities/set-addresses-for-permissions - (fn [{:keys [db]} [addresses]] - {:db (assoc-in db [:communities/addresses-for-permissions] addresses)})) +(defn initialize-permission-addresses + [{:keys [db]}] + (let [accounts (get-in db [:wallet :accounts]) + addresses (set (map :address (vals accounts)))] + {:db (assoc db + :communities/previous-permission-addresses addresses + :communities/selected-permission-addresses addresses)})) + +(rf/reg-event-fx :communities/initialize-permission-addresses + initialize-permission-addresses) + +(rf/reg-event-fx :communities/update-previous-permission-addresses + (fn [{:keys [db]}] + {:db (assoc db + :communities/previous-permission-addresses + (get-in db [:communities/selected-permission-addresses]))})) + +(defn toggle-selected-permission-address + [{:keys [db]} [address]] + {:db (update db + :communities/selected-permission-addresses + (fn [selected-addresses] + (if (contains? selected-addresses address) + (disj selected-addresses address) + (conj selected-addresses address))))}) + +(rf/reg-event-fx :communities/toggle-selected-permission-address + toggle-selected-permission-address) + +(rf/reg-event-fx :communities/reset-selected-permission-addresses + (fn [{:keys [db]}] + {:db (assoc db + :communities/selected-permission-addresses + (get-in db [:communities/previous-permission-addresses]))})) diff --git a/src/status_im/contexts/communities/events_test.cljs b/src/status_im/contexts/communities/events_test.cljs new file mode 100644 index 00000000000..bd485043217 --- /dev/null +++ b/src/status_im/contexts/communities/events_test.cljs @@ -0,0 +1,19 @@ +(ns status-im.contexts.communities.events-test + (:require [cljs.test :refer [deftest is]] + matcher-combinators.test + [status-im.contexts.communities.events :as events])) + +(deftest initialize-permission-addresses-test + (let [initial-db {:db {:wallet {:accounts {"0x1" {:address "0x1"} + "0x2" {:address "0x2"}}}}} + expected-db {:db (assoc (:db initial-db) + :communities/previous-permission-addresses #{"0x1" "0x2"} + :communities/selected-permission-addresses #{"0x1" "0x2"})}] + (is (match? expected-db (events/initialize-permission-addresses initial-db))))) + +(deftest toggle-selected-permission-address-test + (let [initial-db {:db {:communities/selected-permission-addresses #{"0x1" "0x2"}}}] + (is (match? {:db {:communities/selected-permission-addresses #{"0x1"}}} + (events/toggle-selected-permission-address initial-db ["0x2"]))) + (is (match? {:db {:communities/selected-permission-addresses #{"0x1" "0x2" "0x3"}}} + (events/toggle-selected-permission-address initial-db ["0x3"]))))) diff --git a/src/status_im/subs/root.cljs b/src/status_im/subs/root.cljs index 54594a61c51..fa4959dbf7d 100644 --- a/src/status_im/subs/root.cljs +++ b/src/status_im/subs/root.cljs @@ -145,7 +145,7 @@ (reg-root-key-sub :communities/collapsed-categories :communities/collapsed-categories) (reg-root-key-sub :communities/selected-tab :communities/selected-tab) (reg-root-key-sub :contract-communities :contract-communities) -(reg-root-key-sub :communities/addresses-for-permissions :communities/addresses-for-permissions) +(reg-root-key-sub :communities/selected-permission-addresses :communities/selected-permission-addresses) ;;activity center (reg-root-key-sub :activity-center :activity-center)