[Glitch] Fix multiple bugs in notification requests and notification policies

Port 0a6b75b71e to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Claire 2024-09-24 17:03:38 +02:00
parent aac6296183
commit 131696277c
5 changed files with 18 additions and 39 deletions

View File

@ -17,6 +17,6 @@ export const updateNotificationsPolicy = createDataLoadingThunk(
(policy: Partial<NotificationPolicy>) => apiUpdateNotificationsPolicy(policy), (policy: Partial<NotificationPolicy>) => apiUpdateNotificationsPolicy(policy),
); );
export const decreasePendingNotificationsCount = createAction<number>( export const decreasePendingRequestsCount = createAction<number>(
'notificationPolicy/decreasePendingNotificationCount', 'notificationPolicy/decreasePendingRequestsCount',
); );

View File

@ -13,11 +13,11 @@ import type {
ApiNotificationJSON, ApiNotificationJSON,
} from 'flavours/glitch/api_types/notifications'; } from 'flavours/glitch/api_types/notifications';
import type { ApiStatusJSON } from 'flavours/glitch/api_types/statuses'; import type { ApiStatusJSON } from 'flavours/glitch/api_types/statuses';
import type { AppDispatch, RootState } from 'flavours/glitch/store'; import type { AppDispatch } from 'flavours/glitch/store';
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions'; import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions';
import { importFetchedAccounts, importFetchedStatuses } from './importer'; import { importFetchedAccounts, importFetchedStatuses } from './importer';
import { decreasePendingNotificationsCount } from './notification_policies'; import { decreasePendingRequestsCount } from './notification_policies';
// TODO: refactor with notification_groups // TODO: refactor with notification_groups
function dispatchAssociatedRecords( function dispatchAssociatedRecords(
@ -169,19 +169,11 @@ export const expandNotificationsForRequest = createDataLoadingThunk(
}, },
); );
const selectNotificationCountForRequest = (state: RootState, id: string) => {
const requests = state.notificationRequests.items;
const thisRequest = requests.find((request) => request.id === id);
return thisRequest ? thisRequest.notifications_count : 0;
};
export const acceptNotificationRequest = createDataLoadingThunk( export const acceptNotificationRequest = createDataLoadingThunk(
'notificationRequest/accept', 'notificationRequest/accept',
({ id }: { id: string }) => apiAcceptNotificationRequest(id), ({ id }: { id: string }) => apiAcceptNotificationRequest(id),
(_data, { dispatch, getState, discardLoadData, actionArg: { id } }) => { (_data, { dispatch, discardLoadData }) => {
const count = selectNotificationCountForRequest(getState(), id); dispatch(decreasePendingRequestsCount(1));
dispatch(decreasePendingNotificationsCount(count));
// The payload is not used in any functions // The payload is not used in any functions
return discardLoadData; return discardLoadData;
@ -191,10 +183,8 @@ export const acceptNotificationRequest = createDataLoadingThunk(
export const dismissNotificationRequest = createDataLoadingThunk( export const dismissNotificationRequest = createDataLoadingThunk(
'notificationRequest/dismiss', 'notificationRequest/dismiss',
({ id }: { id: string }) => apiDismissNotificationRequest(id), ({ id }: { id: string }) => apiDismissNotificationRequest(id),
(_data, { dispatch, getState, discardLoadData, actionArg: { id } }) => { (_data, { dispatch, discardLoadData }) => {
const count = selectNotificationCountForRequest(getState(), id); dispatch(decreasePendingRequestsCount(1));
dispatch(decreasePendingNotificationsCount(count));
// The payload is not used in any functions // The payload is not used in any functions
return discardLoadData; return discardLoadData;
@ -204,13 +194,8 @@ export const dismissNotificationRequest = createDataLoadingThunk(
export const acceptNotificationRequests = createDataLoadingThunk( export const acceptNotificationRequests = createDataLoadingThunk(
'notificationRequests/acceptBulk', 'notificationRequests/acceptBulk',
({ ids }: { ids: string[] }) => apiAcceptNotificationRequests(ids), ({ ids }: { ids: string[] }) => apiAcceptNotificationRequests(ids),
(_data, { dispatch, getState, discardLoadData, actionArg: { ids } }) => { (_data, { dispatch, discardLoadData, actionArg: { ids } }) => {
const count = ids.reduce( dispatch(decreasePendingRequestsCount(ids.length));
(count, id) => count + selectNotificationCountForRequest(getState(), id),
0,
);
dispatch(decreasePendingNotificationsCount(count));
// The payload is not used in any functions // The payload is not used in any functions
return discardLoadData; return discardLoadData;
@ -220,13 +205,8 @@ export const acceptNotificationRequests = createDataLoadingThunk(
export const dismissNotificationRequests = createDataLoadingThunk( export const dismissNotificationRequests = createDataLoadingThunk(
'notificationRequests/dismissBulk', 'notificationRequests/dismissBulk',
({ ids }: { ids: string[] }) => apiDismissNotificationRequests(ids), ({ ids }: { ids: string[] }) => apiDismissNotificationRequests(ids),
(_data, { dispatch, getState, discardLoadData, actionArg: { ids } }) => { (_data, { dispatch, discardLoadData, actionArg: { ids } }) => {
const count = ids.reduce( dispatch(decreasePendingRequestsCount(ids.length));
(count, id) => count + selectNotificationCountForRequest(getState(), id),
0,
);
dispatch(decreasePendingNotificationsCount(count));
// The payload is not used in any functions // The payload is not used in any functions
return discardLoadData; return discardLoadData;

View File

@ -91,5 +91,5 @@ export const apiAcceptNotificationRequests = async (id: string[]) => {
}; };
export const apiDismissNotificationRequests = async (id: string[]) => { export const apiDismissNotificationRequests = async (id: string[]) => {
return apiRequestPost('v1/notifications/dismiss/dismiss', { id }); return apiRequestPost('v1/notifications/requests/dismiss', { id });
}; };

View File

@ -31,7 +31,7 @@ export const FilteredNotificationsIconButton: React.FC<{
history.push('/notifications/requests'); history.push('/notifications/requests');
}, [history]); }, [history]);
if (policy === null || policy.summary.pending_notifications_count === 0) { if (policy === null || policy.summary.pending_requests_count <= 0) {
return null; return null;
} }
@ -70,7 +70,7 @@ export const FilteredNotificationsBanner: React.FC = () => {
}; };
}, [dispatch]); }, [dispatch]);
if (policy === null || policy.summary.pending_notifications_count === 0) { if (policy === null || policy.summary.pending_requests_count <= 0) {
return null; return null;
} }

View File

@ -2,7 +2,7 @@ import { createReducer, isAnyOf } from '@reduxjs/toolkit';
import { import {
fetchNotificationPolicy, fetchNotificationPolicy,
decreasePendingNotificationsCount, decreasePendingRequestsCount,
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';
@ -10,10 +10,9 @@ import type { NotificationPolicy } from 'flavours/glitch/models/notification_pol
export const notificationPolicyReducer = export const notificationPolicyReducer =
createReducer<NotificationPolicy | null>(null, (builder) => { createReducer<NotificationPolicy | null>(null, (builder) => {
builder builder
.addCase(decreasePendingNotificationsCount, (state, action) => { .addCase(decreasePendingRequestsCount, (state, action) => {
if (state) { if (state) {
state.summary.pending_notifications_count -= action.payload; state.summary.pending_requests_count -= action.payload;
state.summary.pending_requests_count -= 1;
} }
}) })
.addMatcher( .addMatcher(