File size: 1,275 Bytes
8a37e0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import type { Middleware } from '@reduxjs/toolkit';
import { isRejectedWithValue } from '@reduxjs/toolkit';
import { toast } from 'features/toast/toast';
import { t } from 'i18next';
import { z } from 'zod';
const zRejectedForbiddenAction = z.object({
payload: z.object({
status: z.literal(403),
data: z.object({
detail: z.string(),
}),
}),
meta: z
.object({
arg: z
.object({
endpointName: z.string().optional(),
})
.optional(),
})
.optional(),
});
export const authToastMiddleware: Middleware = () => (next) => (action) => {
if (isRejectedWithValue(action)) {
try {
const parsed = zRejectedForbiddenAction.parse(action);
const endpointName = parsed.meta?.arg?.endpointName;
if (endpointName === 'getImageDTO') {
// do not show toast if problem is image access
return next(action);
}
const customMessage = parsed.payload.data.detail !== 'Forbidden' ? parsed.payload.data.detail : undefined;
toast({
id: `auth-error-toast-${endpointName}`,
title: t('toast.somethingWentWrong'),
status: 'error',
description: customMessage,
});
} catch (error) {
// no-op
}
}
return next(action);
};
|