Skip to content

Commit

Permalink
Load message on-component-did-mount
Browse files Browse the repository at this point in the history
I have changed the behavior of loading notifications:

Before it would always use the same function and check for `cursor !=
""`.
This worked on most cases, but sometimes the user would jump back to the
home view without doing any clean up, so cursor would be `""` and it
would not load any notification anymore.

Now the two functions are split in initial load, and load-more.

Initial load is called on component-did-mount so we don't have to worry
about where the user is coming from. We also avoid multiple firing of
the events by checking `loading?`, so it does not result in duplicated
notifications.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
  • Loading branch information
cammellos committed Apr 20, 2021
1 parent 3b652ad commit 807dabf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
40 changes: 31 additions & 9 deletions src/status_im/notifications_center/core.cljs
Expand Up @@ -61,8 +61,7 @@
(fx/defn ensure-and-open-chat
{:events [:ensure-and-open-chat]}
[{:keys [db]} response-js]
{:db (update db :activity.center/notifications dissoc :cursor)
:dispatch-n [[:sanitize-messages-and-process-response response-js]
{:dispatch-n [[:sanitize-messages-and-process-response response-js]
[:chat.ui/navigate-to-chat (.-id (aget (.-chats response-js) 0))]]})

(fx/defn dismiss-all-activity-center-notifications
Expand Down Expand Up @@ -93,26 +92,49 @@
:on-success #()
:on-error #()}]})

(fx/defn load-notifications [{:keys [db]} cursor]
(when-not (:activity.center/loading? db)
{:db (assoc db :activity.center/loading? true)
::json-rpc/call [{:method (json-rpc/call-ext-method "activityCenterNotifications")
:params [cursor 20]
:on-success #(re-frame/dispatch [:activity-center-notifications-success %])
:on-error #(re-frame/dispatch [:activity-center-notifications-error %])}]}))

(fx/defn clean-notifications [{:keys [db]}]
{:db (dissoc db :activity.center/notifications)})

(fx/defn get-activity-center-notifications
{:events [:get-activity-center-notifications]}
[{:keys [db]}]
[{:keys [db] :as cofx}]
(let [{:keys [cursor]} (:activity.center/notifications db)]
(fx/merge cofx
(clean-notifications)
(load-notifications ""))))

(fx/defn load-more-activity-center-notifications
{:events [:load-more-activity-center-notifications]}
[{:keys [db] :as cofx}]
(let [{:keys [cursor]} (:activity.center/notifications db)]
(when (not= cursor "")
{::json-rpc/call [{:method (json-rpc/call-ext-method "activityCenterNotifications")
:params [cursor 20]
:on-success #(re-frame/dispatch [:activity-center-notifications-success %])
:on-error #(log/warn "failed to get notification center activities" %)}]})))
(load-notifications cofx cursor))))

(fx/defn activity-center-notifications-error
{:events [:activity-center-notifications-error]}
[{:keys [db]} error]
(log/warn "failed to load activity center notifications" error)
{:db (dissoc db :activity.center/loading?)})

(fx/defn activity-center-notifications-success
{:events [:activity-center-notifications-success]}
[{:keys [db]} {:keys [cursor notifications]}]
{:db (-> db
(dissoc :activity.center/loading?)
(assoc-in [:activity.center/notifications :cursor] cursor)
(update-in [:activity.center/notifications :notifications]
concat
(map data-store.activities/<-rpc notifications)))})

(fx/defn close-center
{:events [:close-notifications-center]}
[{:keys [db]}]
{:db (dissoc db :activity.center/notifications)})
[cofx]
(clean-notifications cofx))
1 change: 0 additions & 1 deletion src/status_im/ui/screens/home/views.cljs
Expand Up @@ -213,7 +213,6 @@
:style {:margin-left 10}
:accessibility-label "notifications-button"
:on-press #(do
(re-frame/dispatch [:get-activity-center-notifications])
(re-frame/dispatch [:mark-all-activity-center-notifications-as-read])
(re-frame/dispatch [:navigate-to :notifications-center]))
:theme :icon}
Expand Down
52 changes: 28 additions & 24 deletions src/status_im/ui/screens/notifications_center/views.cljs
Expand Up @@ -74,27 +74,31 @@
(reset-state))

(defn center []
(let [{:keys [notifications]} @(re-frame/subscribe [:activity.center/notifications])]
[react/keyboard-avoiding-view {:style {:flex 1}}
[topbar/topbar {:navigation {:on-press #(do
(reset-state)
(re-frame/dispatch [:close-notifications-center])
(re-frame/dispatch [:navigate-back]))}
:title (i18n/label :t/activity)}]
[filter-item]
[list/flat-list
{:key-fn #(or (:chat-id %) (:id %))
:on-end-reached #(re-frame/dispatch [:get-activity-center-notifications])
:keyboard-should-persist-taps :always
:data notifications
:render-fn render-fn}]
(when (or @select-all (> (count @selected-items) 0))
[toolbar/toolbar
{:show-border? true
:left [quo/button {:type :secondary
:theme :negative
:on-press #(toolbar-action false)}
(i18n/label :t/reject-and-delete)]
:right [quo/button {:type :secondary
:on-press #(toolbar-action true)}
(i18n/label :t/accept-and-add)]}])]))
(reagent/create-class
{:display-name "activity-center"
:component-did-mount #(re-frame/dispatch [:get-activity-center-notifications])
:reagent-render (fn []
(let [{:keys [notifications]} @(re-frame/subscribe [:activity.center/notifications])]
[react/keyboard-avoiding-view {:style {:flex 1}}
[topbar/topbar {:navigation {:on-press #(do
(reset-state)
(re-frame/dispatch [:close-notifications-center])
(re-frame/dispatch [:navigate-back]))}
:title (i18n/label :t/activity)}]
[filter-item]
[list/flat-list
{:key-fn #(or (:chat-id %) (:id %))
:on-end-reached #(re-frame/dispatch [:load-more-activity-center-notifications])
:keyboard-should-persist-taps :always
:data notifications
:render-fn render-fn}]
(when (or @select-all (> (count @selected-items) 0))
[toolbar/toolbar
{:show-border? true
:left [quo/button {:type :secondary
:theme :negative
:on-press #(toolbar-action false)}
(i18n/label :t/reject-and-delete)]
:right [quo/button {:type :secondary
:on-press #(toolbar-action true)}
(i18n/label :t/accept-and-add)]}])]))}))

0 comments on commit 807dabf

Please sign in to comment.