From 0e18e1ba31034ac103c7a7b5acd0e7cd299ed062 Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Fri, 26 Jul 2024 11:36:54 +0200 Subject: [PATCH] [Glitch] Decrease count of filtered notifications when notification requests are accepted or dismissed Port dfd43869c9180d4f4ebfc26ebce30f468a8f5422 to glitch-soc Signed-off-by: Claire --- .../glitch/actions/notification_policies.ts | 6 +++++ .../flavours/glitch/actions/notifications.js | 15 +++++++++++-- .../glitch/reducers/notification_policy.ts | 22 +++++++++++++------ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/notification_policies.ts b/app/javascript/flavours/glitch/actions/notification_policies.ts index 76452de324..65b9882d3b 100644 --- a/app/javascript/flavours/glitch/actions/notification_policies.ts +++ b/app/javascript/flavours/glitch/actions/notification_policies.ts @@ -1,3 +1,5 @@ +import { createAction } from '@reduxjs/toolkit'; + import { apiGetNotificationPolicy, apiUpdateNotificationsPolicy, @@ -14,3 +16,7 @@ export const updateNotificationsPolicy = createDataLoadingThunk( 'notificationPolicy/update', (policy: Partial) => apiUpdateNotificationsPolicy(policy), ); + +export const decreasePendingNotificationsCount = createAction( + 'notificationPolicy/decreasePendingNotificationCount', +); diff --git a/app/javascript/flavours/glitch/actions/notifications.js b/app/javascript/flavours/glitch/actions/notifications.js index 7a97e71bdd..fc879dce64 100644 --- a/app/javascript/flavours/glitch/actions/notifications.js +++ b/app/javascript/flavours/glitch/actions/notifications.js @@ -18,6 +18,7 @@ import { importFetchedStatuses, } from './importer'; import { submitMarkers } from './markers'; +import { decreasePendingNotificationsCount } from './notification_policies'; import { notificationsUpdate } from "./notifications_typed"; import { register as registerPushNotifications } from './push_notifications'; import { saveSettings } from './settings'; @@ -96,6 +97,12 @@ const fetchRelatedRelationships = (dispatch, notifications) => { } }; +const selectNotificationCountForRequest = (state, id) => { + const requests = state.getIn(['notificationRequests', 'items']); + const thisRequest = requests.find(request => request.get('id') === id); + return thisRequest ? thisRequest.get('notifications_count') : 0; +}; + export const loadPending = () => ({ type: NOTIFICATIONS_LOAD_PENDING, }); @@ -521,11 +528,13 @@ export const fetchNotificationRequestFail = (id, error) => ({ error, }); -export const acceptNotificationRequest = id => (dispatch) => { +export const acceptNotificationRequest = (id) => (dispatch, getState) => { + const count = selectNotificationCountForRequest(getState(), id); dispatch(acceptNotificationRequestRequest(id)); api().post(`/api/v1/notifications/requests/${id}/accept`).then(() => { dispatch(acceptNotificationRequestSuccess(id)); + dispatch(decreasePendingNotificationsCount(count)); }).catch(err => { dispatch(acceptNotificationRequestFail(id, err)); }); @@ -547,11 +556,13 @@ export const acceptNotificationRequestFail = (id, error) => ({ error, }); -export const dismissNotificationRequest = id => (dispatch) => { +export const dismissNotificationRequest = (id) => (dispatch, getState) => { + const count = selectNotificationCountForRequest(getState(), id); dispatch(dismissNotificationRequestRequest(id)); api().post(`/api/v1/notifications/requests/${id}/dismiss`).then(() =>{ dispatch(dismissNotificationRequestSuccess(id)); + dispatch(decreasePendingNotificationsCount(count)); }).catch(err => { dispatch(dismissNotificationRequestFail(id, err)); }); diff --git a/app/javascript/flavours/glitch/reducers/notification_policy.ts b/app/javascript/flavours/glitch/reducers/notification_policy.ts index 2d5450ce44..0ec68f8950 100644 --- a/app/javascript/flavours/glitch/reducers/notification_policy.ts +++ b/app/javascript/flavours/glitch/reducers/notification_policy.ts @@ -2,17 +2,25 @@ import { createReducer, isAnyOf } from '@reduxjs/toolkit'; import { fetchNotificationPolicy, + decreasePendingNotificationsCount, updateNotificationsPolicy, } from 'flavours/glitch/actions/notification_policies'; import type { NotificationPolicy } from 'flavours/glitch/models/notification_policy'; export const notificationPolicyReducer = createReducer(null, (builder) => { - builder.addMatcher( - isAnyOf( - fetchNotificationPolicy.fulfilled, - updateNotificationsPolicy.fulfilled, - ), - (_state, action) => action.payload, - ); + builder + .addCase(decreasePendingNotificationsCount, (state, action) => { + if (state) { + state.summary.pending_notifications_count -= action.payload; + state.summary.pending_requests_count -= 1; + } + }) + .addMatcher( + isAnyOf( + fetchNotificationPolicy.fulfilled, + updateNotificationsPolicy.fulfilled, + ), + (_state, action) => action.payload, + ); });