Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <Datagrid rowClick> regression #10102

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 93 additions & 63 deletions packages/ra-core/src/routing/useGetPathForRecord.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useCallback } from 'react';
import { useResourceContext, useResourceDefinition } from '../core';
import { useResourceContext, useResourceDefinitions } from '../core';
import { useCreatePath } from './useCreatePath';
import { useRecordContext } from '../controller';
import type { RaRecord } from '../types';
Expand Down Expand Up @@ -49,82 +49,112 @@ export const useGetPathForRecord = <RecordType extends RaRecord = RaRecord>(
'Cannot generate a link for a record without a resource. You must use useGetRouteForRecord within a ResourceContextProvider, or pass a resource prop.'
);
}
const createPath = useCreatePath();
const resourceDefinition = useResourceDefinition({ resource });

// eslint-disable-next-line react-hooks/exhaustive-deps
const linkFunc = useCallback(
typeof link === 'function' ? link : () => link,
[]
);

const defaultLink = resourceDefinition.hasShow
? 'show'
: resourceDefinition.hasEdit
? 'edit'
: false;

const isLinkFalse =
link === false || (link == null && defaultLink === false);
const getPathForRecord = useGetRouteForRecordCallback<RecordType>(options);

// we initialize the path with the link value
const [path, setPath] = useState<string | false | undefined>(() => {
if (record == null || isLinkFalse) return false;
const linkResult = linkFunc(record, resource) ?? defaultLink;
const linkResultIsPromise = isPromise(linkResult);
if (linkResultIsPromise) {
linkResult.then(resolvedLink => {
if (resolvedLink === false) {
// already set to false by default
return;
}
// update the path when the promise resolves
setPath(
createPath({
resource,
id: record.id,
type: resolvedLink,
})
);
});
}
return linkResult === false || linkResultIsPromise
? false
: createPath({ resource, id: record.id, type: linkResult });
getPathForRecord({
record,
resource,
link,
}).then(resolvedLink => {
if (resolvedLink === false) {
// already set to false by default
return;
}
// update the path when the promise resolves
setPath(resolvedLink);
});

return false;
});

// update the path if the record changes
useEffect(() => {
if (record == null || isLinkFalse) {
setPath(false);
return;
}
const linkResult = linkFunc(record, resource) ?? defaultLink;
const linkResultIsPromise = isPromise(linkResult);
if (linkResultIsPromise) {
linkResult.then(resolvedLink => {
if (resolvedLink === false) {
// already set to false by default
return;
}
setPath(
createPath({ resource, id: record.id, type: resolvedLink })
);
});
}
setPath(
linkResult === false || linkResultIsPromise
? false
: createPath({ resource, id: record.id, type: linkResult })
);
}, [createPath, defaultLink, isLinkFalse, linkFunc, record, resource]);
getPathForRecord({
record,
resource,
link,
}).then(resolvedLink => {
// update the path when the promise resolves
setPath(resolvedLink);
});
}, [getPathForRecord, link, record, resource]);

return path;
};

export const useGetRouteForRecordCallback = <
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this function useGetRouteForRecordCallback and extract it to another file (and export it)

RecordType extends RaRecord = RaRecord,
>(
options: UseGetRouteForRecordCallbackOptions = {}
) => {
const resource = useResourceContext(options);
const resourceDefinitions = useResourceDefinitions();
const createPath = useCreatePath();

return useCallback(
async (params: UseGetRouteForRecordOptions<RecordType>) => {
const { link, record } = params || {};
const finalResource = params.resource ?? resource;
if (!finalResource) {
throw new Error(
'Cannot generate a link for a record without a resource. You must use useGetRouteForRecordCallback within a ResourceContextProvider, or pass a resource parameter.'
);
}
const resourceDefinition = resourceDefinitions[finalResource] ?? {};

// eslint-disable-next-line react-hooks/exhaustive-deps
const linkFunc = typeof link === 'function' ? link : () => link;

const defaultLink = resourceDefinition.hasShow
? 'show'
: resourceDefinition.hasEdit
? 'edit'
: false;

const isLinkFalse =
link === false || (link == null && defaultLink === false);

if (record == null || isLinkFalse) {
return false;
}
const linkResult = linkFunc(record, finalResource) ?? defaultLink;
const linkResultIsPromise = isPromise(linkResult);

if (linkResultIsPromise) {
return linkResult.then(resolvedLink => {
if (resolvedLink === false) {
// already set to false by default
return;
}
return createPath({
resource: finalResource,
id: record.id,
type: resolvedLink,
});
});
}

return linkResult === false || linkResultIsPromise
? false
: createPath({
resource: finalResource,
id: record.id,
type: linkResult,
});
},
[createPath, resourceDefinitions, resource]
);
};

const isPromise = (value: any): value is Promise<any> =>
value && typeof value.then === 'function';

export interface UseGetRouteForRecordCallbackOptions {
resource?: string;
}

export interface UseGetRouteForRecordOptions<
RecordType extends RaRecord = RaRecord,
> {
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/button/PrevNextButtons.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ describe('<PrevNextButtons />', () => {
const item = await screen.findByText('217');
fireEvent.click(item);
await screen.findByRole('navigation');
expect(screen.getByText('10 / 57')).toBeDefined();
await screen.findByText('10 / 57');
});

it('should link to the edit view by default', async () => {
render(<Basic />);
const row = await screen.findByText('Deja');
fireEvent.click(row);
fireEvent.click(screen.getByLabelText('Edit'));
fireEvent.click(await screen.findByLabelText('Edit'));
const next = await screen.findByLabelText('Go to next page');
fireEvent.click(next);
expect(screen.getByLabelText('First name').getAttribute('type')).toBe(
Expand Down
Loading
Loading