From 58606dc87c57b847daeec51d54f148a404012592 Mon Sep 17 00:00:00 2001 From: BalogunofAfrica <45393944+BalogunofAfrica@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:13:29 +0100 Subject: [PATCH] fix: handle limit event (#19066) --- .../contexts/chat/home/new_chat/view.cljs | 59 ++++++++++++++----- translations/en.json | 1 + 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/status_im/contexts/chat/home/new_chat/view.cljs b/src/status_im/contexts/chat/home/new_chat/view.cljs index 1ac18f50ab..07f5fa4ec3 100644 --- a/src/status_im/contexts/chat/home/new_chat/view.cljs +++ b/src/status_im/contexts/chat/home/new_chat/view.cljs @@ -49,20 +49,41 @@ (rf/dispatch [:open-modal :new-contact]))} (i18n/label :t/add-a-contact)]])) +(def ^:private contacts-selection-limit (dec constants/max-group-chat-participants)) + +(defn- toggle-selection + [public-key user-selected?] + (let [selected-contacts-count (rf/sub [:selected-contacts-count])] + (if user-selected? + (re-frame/dispatch [:deselect-contact public-key]) + (if (= contacts-selection-limit + selected-contacts-count) + (do + (rf/dispatch + [:toasts/upsert + {:id :remove-nickname + :type :negative + :text (i18n/label :t/new-group-limit + {:max-contacts + contacts-selection-limit})}]) + true) + (re-frame/dispatch [:select-contact public-key]))))) + (defn contact-item-render - [_] - (fn [{:keys [public-key] :as item}] - (let [user-selected? (rf/sub [:is-contact-selected? public-key]) - on-toggle #(if user-selected? - (re-frame/dispatch [:deselect-contact public-key]) - (re-frame/dispatch [:select-contact public-key]))] - [contact-list-item/contact-list-item - {:on-press on-toggle - :allow-multiple-presses? true - :accessory {:type :checkbox - :checked? user-selected? - :on-check on-toggle}} - item]))) + [{:keys [public-key] :as item} set-has-error] + (let [user-selected? (rf/sub [:is-contact-selected? public-key]) + on-toggle (fn [] + (-> public-key + (toggle-selection user-selected?) + boolean + set-has-error))] + [contact-list-item/contact-list-item + {:on-press on-toggle + :allow-multiple-presses? true + :accessory {:type :checkbox + :checked? user-selected? + :on-check on-toggle}} + item])) (defn- view-internal [{:keys [scroll-enabled? on-scroll close theme]}] @@ -70,6 +91,10 @@ selected-contacts-count (rf/sub [:selected-contacts-count]) selected-contacts (rf/sub [:group/selected-contacts]) one-contact-selected? (= selected-contacts-count 1) + [has-error? set-has-error] (rn/use-state false) + render-fn (rn/use-callback (fn [item] + (contact-item-render item set-has-error)) + [set-has-error]) contacts-selected? (pos? selected-contacts-count) {:keys [primary-name public-key]} (when one-contact-selected? (rf/sub [:contacts/contact-by-identity @@ -92,10 +117,12 @@ {:size :paragraph-2 :weight :regular :style {:margin-bottom 2 - :color (colors/theme-colors colors/neutral-40 colors/neutral-50 theme)}} + :color (if has-error? + (colors/theme-colors colors/danger-50 colors/danger-60 theme) + (colors/theme-colors colors/neutral-40 colors/neutral-50 theme))}} (i18n/label :t/selected-count-from-max {:selected selected-contacts-count - :max constants/max-group-chat-participants})])]] + :max contacts-selection-limit})])]] (if (empty? contacts) [no-contacts-view {:theme theme}] [gesture/section-list @@ -104,7 +131,7 @@ :sections (rf/sub [:contacts/filtered-active-sections]) :render-section-header-fn contact-list/contacts-section-header :content-container-style {:padding-bottom 70} - :render-fn contact-item-render + :render-fn render-fn :scroll-enabled @scroll-enabled? :on-scroll on-scroll}]) (when contacts-selected? diff --git a/translations/en.json b/translations/en.json index 1d0c74a214..0960fe6495 100644 --- a/translations/en.json +++ b/translations/en.json @@ -1037,6 +1037,7 @@ "new-contract": "New Contract", "new-group": "New group", "new-group-chat": "New group chat", + "new-group-limit": "You can only add {{max-contacts}} contacts to the group chat", "new-network": "New network", "new-pin-description": "Enter new 6-digit passcode", "new-puk-description": "Enter new 12-digit PUK",