Skip to content

Commit

Permalink
Move selected addresses for permissions to app db
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayesivan committed Jan 8, 2024
1 parent 5915dd4 commit 09498c5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
(:address %))
accounts)]
(rn/use-effect (fn []
(rf/dispatch [:communities/set-addresses-for-permissions
(set (map :address accounts))]))
(rf/dispatch [:communities/initialize-addresses-for-permissions]))
[])
[rn/view {:style style/container}
[quo/page-nav
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-addresses-for-permissions
(: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-addresses-for-permissions])]
[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-addresses-for-permissions])
(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-addresses-for-permissions])
(rf/dispatch [:navigate-back]))}
(i18n/label :t/confirm-changes)]]]))
31 changes: 28 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,31 @@
: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)}))
(rf/reg-event-fx :communities/initialize-addresses-for-permissions
(fn [{:keys [db]}]
(let [accounts (get-in db [:wallet :accounts])
addresses (set (map :address (vals accounts)))]
{:db (assoc db
:communities/addresses-for-permissions addresses
:communities/selected-addresses-for-permissions addresses)})))

(rf/reg-event-fx :communities/update-addresses-for-permissions
(fn [{:keys [db]}]
{:db (assoc db
:communities/addresses-for-permissions
(get-in db [:communities/selected-addresses-for-permissions]))}))

(rf/reg-event-fx :communities/toggle-selected-addresses-for-permissions
(fn [{:keys [db]} [address]]
(let [selected-addresses (get-in db [:communities/selected-addresses-for-permissions])]
{:db (assoc db
:communities/selected-addresses-for-permissions
(if (contains? selected-addresses address)
(disj selected-addresses address)
(conj selected-addresses address)))})))

(rf/reg-event-fx :communities/reset-selected-addresses-for-permissions
(fn [{:keys [db]}]
{:db (assoc db
:communities/selected-addresses-for-permissions
(get-in db [:communities/addresses-for-permissions]))}))
2 changes: 2 additions & 0 deletions src/status_im/subs/root.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
(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-addresses-for-permissions
:communities/selected-addresses-for-permissions)

;;activity center
(reg-root-key-sub :activity-center :activity-center)
Expand Down

0 comments on commit 09498c5

Please sign in to comment.