Skip to content

Commit 4a5f24e

Browse files
authored
Merge pull request #8811 from marmelab/better-redirect-type
Better types for useRedirect and useCreatePath
2 parents 0e1a1ce + 03becf6 commit 4a5f24e

4 files changed

Lines changed: 29 additions & 9 deletions

File tree

packages/ra-core/src/routing/useCreatePath.spec.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import * as React from 'react';
22
import { render, screen } from '@testing-library/react';
33

4-
import { useCreatePath } from './useCreatePath';
4+
import { CreatePathType, useCreatePath } from './useCreatePath';
55
import { AtRoot, SubPath } from './useCreatePath.stories';
6+
import { Identifier } from '../types';
67

78
describe('useCreatePath', () => {
89
beforeEach(() => {
910
window.history.replaceState({}, '', '/');
1011
});
1112

12-
const UseCreatePath = ({ resource, type, id }: any) => {
13+
const UseCreatePath = ({
14+
resource,
15+
type,
16+
id,
17+
}: {
18+
resource: string;
19+
type: CreatePathType;
20+
id?: Identifier;
21+
}) => {
1322
const createPath = useCreatePath();
1423
const path = createPath({ resource, type, id });
1524
return <div>{path}</div>;

packages/ra-core/src/routing/useCreatePath.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ export const useCreatePath = () => {
7676
);
7777
};
7878

79+
type AnyString = string & {};
80+
export type CreatePathType = 'list' | 'edit' | 'show' | 'create' | AnyString;
81+
7982
export interface CreatePathParams {
80-
type: string;
83+
type: CreatePathType;
8184
resource: string;
8285
id?: Identifier;
8386
}

packages/ra-core/src/routing/useRedirect.spec.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import { Routes, Route, useLocation } from 'react-router-dom';
66
import { createMemoryHistory } from 'history';
77

88
import { CoreAdminContext } from '../core';
9-
import { useRedirect } from './useRedirect';
9+
import { RedirectionSideEffect, useRedirect } from './useRedirect';
1010
import { testDataProvider } from '../dataProvider';
11+
import { Identifier, RaRecord } from '../types';
1112

1213
const Redirect = ({
1314
redirectTo,
1415
resource = '',
15-
id = null,
16-
data = null,
17-
state = null,
16+
id = undefined,
17+
data = undefined,
18+
state = undefined,
19+
}: {
20+
redirectTo: RedirectionSideEffect;
21+
resource?: string;
22+
id?: Identifier;
23+
data?: Partial<RaRecord>;
24+
state?: object;
1825
}) => {
1926
const redirect = useRedirect();
2027
useEffect(() => {
@@ -89,6 +96,7 @@ describe('useRedirect', () => {
8996

9097
it('should support absolute URLs', () => {
9198
const oldLocation = window.location;
99+
// @ts-ignore
92100
delete window.location;
93101
// @ts-ignore
94102
window.location = { href: '' };

packages/ra-core/src/routing/useRedirect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parsePath } from 'history';
44

55
import { Identifier, RaRecord } from '../types';
66
import { useBasename } from './useBasename';
7-
import { useCreatePath } from './useCreatePath';
7+
import { CreatePathType, useCreatePath } from './useCreatePath';
88

99
type RedirectToFunction = (
1010
resource?: string,
@@ -13,7 +13,7 @@ type RedirectToFunction = (
1313
state?: object
1414
) => To;
1515

16-
export type RedirectionSideEffect = string | false | RedirectToFunction;
16+
export type RedirectionSideEffect = CreatePathType | false | RedirectToFunction;
1717

1818
/**
1919
* Hook for Redirection Side Effect

0 commit comments

Comments
 (0)