Skip to content

Commit

Permalink
Fix useCanAccessResources cannot have react-query options
Browse files Browse the repository at this point in the history
  • Loading branch information
React-Admin CI committed Nov 20, 2024
1 parent c0012c7 commit 9897e61
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions packages/ra-core/src/auth/useCanAccessResources.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { useMemo } from 'react';
import {
useQueries,
UseQueryOptions,
UseQueryResult,
} from '@tanstack/react-query';
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import useAuthProvider from './useAuthProvider';
import { HintedString } from '../types';
import { useRecordContext } from '../controller';
Expand Down Expand Up @@ -50,18 +46,18 @@ export const useCanAccessResources = <
RecordType extends Record<string, any> = Record<string, any>,
ErrorType extends Error = Error,
>(
params: UseCanAccessResourcesOptions<RecordType, ErrorType>
params: UseCanAccessResourcesOptions<RecordType>
): UseCanAccessResourcesResult<ErrorType> => {
const authProvider = useAuthProvider();
const record = useRecordContext<RecordType>(params);

const { action, resources } = params;
const { action, resources, ...options } = params;

const queryResult = useQueries({
queries: resources.map(resource => {
return {
queryKey: ['auth', 'canAccess', resource, action, record],
queryFn: async ({ signal }) => {
const queryResult = useQuery({
queryKey: ['auth', 'canAccess', resources, action, record],
queryFn: async ({ signal }) => {
const queries = await Promise.all(
resources.map(async resource => {
if (!authProvider || !authProvider.canAccess) {
return { canAccess: true, resource };
}
Expand All @@ -75,10 +71,22 @@ export const useCanAccessResources = <
});

return { canAccess, resource };
},
};
}),
combine: combineSourceAccessResults<ErrorType>,
})
);

const result = resources.reduce(
(acc, resource) => ({
...acc,
[resource]:
queries.find(query => query.resource === resource)
?.canAccess ?? false,
}),
{} as Record<string, boolean>
);

return result;
},
...options,
});

const result = useMemo(() => {
Expand Down Expand Up @@ -111,10 +119,14 @@ export const useCanAccessResources = <
export interface UseCanAccessResourcesOptions<
RecordType extends Record<string, any> = Record<string, any>,
ErrorType extends Error = Error,
> extends Omit<UseQueryOptions<boolean, ErrorType>, 'queryKey' | 'queryFn'> {
> extends Omit<
UseQueryOptions<Record<string, boolean>, ErrorType>,
'queryKey' | 'queryFn'
> {
resources: string[];
action: HintedString<'list' | 'create' | 'edit' | 'show' | 'delete'>;
record?: RecordType;
enabled?: boolean;
}

export type UseCanAccessResourcesResult<ErrorType = Error> =
Expand Down Expand Up @@ -143,39 +155,3 @@ export interface UseCanAccessResourcesSuccessResult {
error: null;
isPending: false;
}

const combineSourceAccessResults = <ErrorType>(
results: UseQueryResult<
{
canAccess: boolean;
resource: string;
},
ErrorType
>[]
): {
data?: Record<string, boolean>;
isPending: boolean;
isError: boolean;
error?: ErrorType;
} => {
return {
data: results
? results.reduce(
(acc, { data }) => {
if (!data) {
return acc;
}
const { resource, canAccess } = data;
return {
...acc,
[resource]: canAccess,
};
},
{} as Record<string, boolean>
)
: undefined,
isPending: results.some(result => result.isPending),
isError: results.some(result => result.isError),
error: results.find(result => result.error)?.error || undefined,
};
};

0 comments on commit 9897e61

Please sign in to comment.