[Glitch] Decrease count of filtered notifications when notification requests are accepted or dismissed

Port dfd43869c9 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
pull/2798/head
David Roetzel 2024-07-26 11:36:54 +02:00 committed by Claire
parent 14bc73e94c
commit 0e18e1ba31
3 changed files with 34 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import { createAction } from '@reduxjs/toolkit';
import { import {
apiGetNotificationPolicy, apiGetNotificationPolicy,
apiUpdateNotificationsPolicy, apiUpdateNotificationsPolicy,
@ -14,3 +16,7 @@ export const updateNotificationsPolicy = createDataLoadingThunk(
'notificationPolicy/update', 'notificationPolicy/update',
(policy: Partial<NotificationPolicy>) => apiUpdateNotificationsPolicy(policy), (policy: Partial<NotificationPolicy>) => apiUpdateNotificationsPolicy(policy),
); );
export const decreasePendingNotificationsCount = createAction<number>(
'notificationPolicy/decreasePendingNotificationCount',
);

View File

@ -18,6 +18,7 @@ import {
importFetchedStatuses, importFetchedStatuses,
} from './importer'; } from './importer';
import { submitMarkers } from './markers'; import { submitMarkers } from './markers';
import { decreasePendingNotificationsCount } from './notification_policies';
import { notificationsUpdate } from "./notifications_typed"; import { notificationsUpdate } from "./notifications_typed";
import { register as registerPushNotifications } from './push_notifications'; import { register as registerPushNotifications } from './push_notifications';
import { saveSettings } from './settings'; 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 = () => ({ export const loadPending = () => ({
type: NOTIFICATIONS_LOAD_PENDING, type: NOTIFICATIONS_LOAD_PENDING,
}); });
@ -521,11 +528,13 @@ export const fetchNotificationRequestFail = (id, error) => ({
error, error,
}); });
export const acceptNotificationRequest = id => (dispatch) => { export const acceptNotificationRequest = (id) => (dispatch, getState) => {
const count = selectNotificationCountForRequest(getState(), id);
dispatch(acceptNotificationRequestRequest(id)); dispatch(acceptNotificationRequestRequest(id));
api().post(`/api/v1/notifications/requests/${id}/accept`).then(() => { api().post(`/api/v1/notifications/requests/${id}/accept`).then(() => {
dispatch(acceptNotificationRequestSuccess(id)); dispatch(acceptNotificationRequestSuccess(id));
dispatch(decreasePendingNotificationsCount(count));
}).catch(err => { }).catch(err => {
dispatch(acceptNotificationRequestFail(id, err)); dispatch(acceptNotificationRequestFail(id, err));
}); });
@ -547,11 +556,13 @@ export const acceptNotificationRequestFail = (id, error) => ({
error, error,
}); });
export const dismissNotificationRequest = id => (dispatch) => { export const dismissNotificationRequest = (id) => (dispatch, getState) => {
const count = selectNotificationCountForRequest(getState(), id);
dispatch(dismissNotificationRequestRequest(id)); dispatch(dismissNotificationRequestRequest(id));
api().post(`/api/v1/notifications/requests/${id}/dismiss`).then(() =>{ api().post(`/api/v1/notifications/requests/${id}/dismiss`).then(() =>{
dispatch(dismissNotificationRequestSuccess(id)); dispatch(dismissNotificationRequestSuccess(id));
dispatch(decreasePendingNotificationsCount(count));
}).catch(err => { }).catch(err => {
dispatch(dismissNotificationRequestFail(id, err)); dispatch(dismissNotificationRequestFail(id, err));
}); });

View File

@ -2,13 +2,21 @@ import { createReducer, isAnyOf } from '@reduxjs/toolkit';
import { import {
fetchNotificationPolicy, fetchNotificationPolicy,
decreasePendingNotificationsCount,
updateNotificationsPolicy, updateNotificationsPolicy,
} from 'flavours/glitch/actions/notification_policies'; } from 'flavours/glitch/actions/notification_policies';
import type { NotificationPolicy } from 'flavours/glitch/models/notification_policy'; import type { NotificationPolicy } from 'flavours/glitch/models/notification_policy';
export const notificationPolicyReducer = export const notificationPolicyReducer =
createReducer<NotificationPolicy | null>(null, (builder) => { createReducer<NotificationPolicy | null>(null, (builder) => {
builder.addMatcher( builder
.addCase(decreasePendingNotificationsCount, (state, action) => {
if (state) {
state.summary.pending_notifications_count -= action.payload;
state.summary.pending_requests_count -= 1;
}
})
.addMatcher(
isAnyOf( isAnyOf(
fetchNotificationPolicy.fulfilled, fetchNotificationPolicy.fulfilled,
updateNotificationsPolicy.fulfilled, updateNotificationsPolicy.fulfilled,