Load message on-component-did-mount

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>
This commit is contained in:
Andrea Maria Piana 2021-04-19 10:25:15 +02:00
parent 3b652adaf3
commit 807dabf5b2
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 59 additions and 34 deletions

View File

@ -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
@ -93,20 +92,43 @@
: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
@ -114,5 +136,5 @@
(fx/defn close-center
{:events [:close-notifications-center]}
[{:keys [db]}]
{:db (dissoc db :activity.center/notifications)})
[cofx]
(clean-notifications cofx))

View File

@ -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}

View File

@ -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)]}])]))}))