Skip to content

Commit 4182a5a

Browse files
Fix(show/edit): Avoid a redirect loop when useGetOne returns an error in shadcn-admin-kit
1 parent c529941 commit 4182a5a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

packages/ra-core/src/controller/edit/useEditController.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { useParams } from 'react-router-dom';
33

44
import { useAuthenticated, useRequireAccess } from '../../auth';
55
import { RaRecord, MutationMode, TransformData } from '../../types';
6-
import { useRedirect, RedirectionSideEffect } from '../../routing';
6+
import {
7+
useRedirect,
8+
RedirectionSideEffect,
9+
useCreatePath,
10+
} from '../../routing';
711
import { useNotify } from '../../notification';
812
import {
913
useGetOne,
@@ -82,6 +86,7 @@ export const useEditController = <
8286
const getRecordRepresentation = useGetRecordRepresentation(resource);
8387
const translate = useTranslate();
8488
const notify = useNotify();
89+
const createPath = useCreatePath();
8590
const redirect = useRedirect();
8691
const refresh = useRefresh();
8792
const { id: routeId } = useParams<'id'>();
@@ -118,10 +123,19 @@ export const useEditController = <
118123
(!isPendingAuthenticated && !isPendingCanAccess) ||
119124
disableAuthentication,
120125
onError: () => {
126+
console.warn('useEditController: record not found');
121127
notify('ra.notification.item_doesnt_exist', {
122128
type: 'error',
123129
});
124-
redirect('list', resource);
130+
// We need to flushSync to ensure the redirect happens before the refresh
131+
// Otherwise this can cause an infinite loop when the record is not found
132+
redirect(() => ({
133+
pathname: createPath({
134+
resource,
135+
type: 'list',
136+
}),
137+
flushSync: true,
138+
}));
125139
refresh();
126140
},
127141
refetchOnReconnect: false,

packages/ra-core/src/controller/show/useShowController.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
UseGetOneOptions,
1010
} from '../../dataProvider';
1111
import { useTranslate } from '../../i18n';
12-
import { useRedirect } from '../../routing';
12+
import { useCreatePath, useRedirect } from '../../routing';
1313
import { useNotify } from '../../notification';
1414
import {
1515
useResourceContext,
@@ -80,6 +80,7 @@ export const useShowController = <
8080
const getRecordRepresentation = useGetRecordRepresentation(resource);
8181
const translate = useTranslate();
8282
const notify = useNotify();
83+
const createPath = useCreatePath();
8384
const redirect = useRedirect();
8485
const refresh = useRefresh();
8586
const { id: routeId } = useParams<'id'>();
@@ -109,7 +110,16 @@ export const useShowController = <
109110
notify('ra.notification.item_doesnt_exist', {
110111
type: 'error',
111112
});
112-
redirect('list', resource);
113+
114+
// We need to flushSync to ensure the redirect happens before the refresh
115+
// Otherwise this can cause an infinite loop when the record is not found
116+
redirect(() => ({
117+
pathname: createPath({
118+
resource,
119+
type: 'list',
120+
}),
121+
flushSync: true,
122+
}));
113123
refresh();
114124
},
115125
retry: false,

0 commit comments

Comments
 (0)