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 (fx/defn ensure-and-open-chat
{:events [:ensure-and-open-chat]} {:events [:ensure-and-open-chat]}
[{:keys [db]} response-js] [{: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))]]}) [:chat.ui/navigate-to-chat (.-id (aget (.-chats response-js) 0))]]})
(fx/defn dismiss-all-activity-center-notifications (fx/defn dismiss-all-activity-center-notifications
@ -93,20 +92,43 @@
:on-success #() :on-success #()
:on-error #()}]}) :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 (fx/defn get-activity-center-notifications
{:events [: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)] (let [{:keys [cursor]} (:activity.center/notifications db)]
(when (not= cursor "") (when (not= cursor "")
{::json-rpc/call [{:method (json-rpc/call-ext-method "activityCenterNotifications") (load-notifications cofx cursor))))
:params [cursor 20]
:on-success #(re-frame/dispatch [:activity-center-notifications-success %]) (fx/defn activity-center-notifications-error
:on-error #(log/warn "failed to get notification center activities" %)}]}))) {: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 (fx/defn activity-center-notifications-success
{:events [:activity-center-notifications-success]} {:events [:activity-center-notifications-success]}
[{:keys [db]} {:keys [cursor notifications]}] [{:keys [db]} {:keys [cursor notifications]}]
{:db (-> db {:db (-> db
(dissoc :activity.center/loading?)
(assoc-in [:activity.center/notifications :cursor] cursor) (assoc-in [:activity.center/notifications :cursor] cursor)
(update-in [:activity.center/notifications :notifications] (update-in [:activity.center/notifications :notifications]
concat concat
@ -114,5 +136,5 @@
(fx/defn close-center (fx/defn close-center
{:events [:close-notifications-center]} {:events [:close-notifications-center]}
[{:keys [db]}] [cofx]
{:db (dissoc db :activity.center/notifications)}) (clean-notifications cofx))

View File

@ -213,7 +213,6 @@
:style {:margin-left 10} :style {:margin-left 10}
:accessibility-label "notifications-button" :accessibility-label "notifications-button"
:on-press #(do :on-press #(do
(re-frame/dispatch [:get-activity-center-notifications])
(re-frame/dispatch [:mark-all-activity-center-notifications-as-read]) (re-frame/dispatch [:mark-all-activity-center-notifications-as-read])
(re-frame/dispatch [:navigate-to :notifications-center])) (re-frame/dispatch [:navigate-to :notifications-center]))
:theme :icon} :theme :icon}

View File

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