Skip to content

Commit

Permalink
Move selected addresses for permissions to app db (#18430)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayesivan authored Jan 10, 2024
1 parent 61b0a2e commit 1eb9e87
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -16,61 +15,59 @@
: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
[]
(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)]]]))
37 changes: 34 additions & 3 deletions src/status_im/contexts/communities/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))}))
19 changes: 19 additions & 0 deletions src/status_im/contexts/communities/events_test.cljs
Original file line number Diff line number Diff line change
@@ -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"])))))
2 changes: 1 addition & 1 deletion src/status_im/subs/root.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1eb9e87

Please sign in to comment.