Skip to content

Commit

Permalink
Merge pull request #10362 from marmelab/fix-can-access-resources
Browse files Browse the repository at this point in the history
Fix `useCanAccessResources` cannot have react-query options
  • Loading branch information
fzaninotto authored Nov 20, 2024
2 parents b7db83d + fffec2c commit b6b10e5
Showing 1 changed file with 25 additions and 52 deletions.
77 changes: 25 additions & 52 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,20 @@ export const useCanAccessResources = <
});

return { canAccess, resource };
})
);

const result = queries.reduce(
(acc, { resource, canAccess }) => {
acc[resource] = canAccess;
return acc;
},
};
}),
combine: combineSourceAccessResults<ErrorType>,
{} as Record<string, boolean>
);

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

const result = useMemo(() => {
Expand Down Expand Up @@ -111,7 +117,10 @@ 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;
Expand Down Expand Up @@ -143,39 +152,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 b6b10e5

Please sign in to comment.