From 847f75193362b2694c023abe0f0aa1e404492821 Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Wed, 5 Apr 2023 17:39:32 +0200
Subject: [PATCH 1/2] Better types for useRedirect and useCreatePath
---
packages/ra-core/src/routing/useCreatePath.spec.tsx | 13 +++++++++++--
packages/ra-core/src/routing/useCreatePath.ts | 5 ++++-
packages/ra-core/src/routing/useRedirect.spec.tsx | 10 +++++++++-
packages/ra-core/src/routing/useRedirect.ts | 10 +++++-----
4 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/packages/ra-core/src/routing/useCreatePath.spec.tsx b/packages/ra-core/src/routing/useCreatePath.spec.tsx
index 040ecdb9417..dafd834a0d1 100644
--- a/packages/ra-core/src/routing/useCreatePath.spec.tsx
+++ b/packages/ra-core/src/routing/useCreatePath.spec.tsx
@@ -1,15 +1,24 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
-import { useCreatePath } from './useCreatePath';
+import { CreatePathType, useCreatePath } from './useCreatePath';
import { AtRoot, SubPath } from './useCreatePath.stories';
+import { Identifier } from '../types';
describe('useCreatePath', () => {
beforeEach(() => {
window.history.replaceState({}, '', '/');
});
- const UseCreatePath = ({ resource, type, id }: any) => {
+ const UseCreatePath = ({
+ resource,
+ type,
+ id,
+ }: {
+ resource: string;
+ type: CreatePathType;
+ id?: Identifier;
+ }) => {
const createPath = useCreatePath();
const path = createPath({ resource, type, id });
return
{path}
;
diff --git a/packages/ra-core/src/routing/useCreatePath.ts b/packages/ra-core/src/routing/useCreatePath.ts
index 1b38a4d3eb9..ab7ff9b1283 100644
--- a/packages/ra-core/src/routing/useCreatePath.ts
+++ b/packages/ra-core/src/routing/useCreatePath.ts
@@ -76,8 +76,11 @@ export const useCreatePath = () => {
);
};
+type AnyString = string & {};
+export type CreatePathType = 'list' | 'edit' | 'show' | 'create' | AnyString;
+
export interface CreatePathParams {
- type: string;
+ type: CreatePathType;
resource: string;
id?: Identifier;
}
diff --git a/packages/ra-core/src/routing/useRedirect.spec.tsx b/packages/ra-core/src/routing/useRedirect.spec.tsx
index ab813e3402a..56e66ee1b3a 100644
--- a/packages/ra-core/src/routing/useRedirect.spec.tsx
+++ b/packages/ra-core/src/routing/useRedirect.spec.tsx
@@ -6,8 +6,9 @@ import { Routes, Route, useLocation } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import { CoreAdminContext } from '../core';
-import { useRedirect } from './useRedirect';
+import { RedirectionSideEffect, useRedirect } from './useRedirect';
import { testDataProvider } from '../dataProvider';
+import { Identifier, RaRecord } from '../types';
const Redirect = ({
redirectTo,
@@ -15,6 +16,12 @@ const Redirect = ({
id = null,
data = null,
state = null,
+}: {
+ redirectTo: RedirectionSideEffect;
+ resource?: string;
+ id?: Identifier | null;
+ data?: Partial | null;
+ state?: object | null;
}) => {
const redirect = useRedirect();
useEffect(() => {
@@ -89,6 +96,7 @@ describe('useRedirect', () => {
it('should support absolute URLs', () => {
const oldLocation = window.location;
+ // @ts-ignore
delete window.location;
// @ts-ignore
window.location = { href: '' };
diff --git a/packages/ra-core/src/routing/useRedirect.ts b/packages/ra-core/src/routing/useRedirect.ts
index a1af02a5eca..c42c149c78d 100644
--- a/packages/ra-core/src/routing/useRedirect.ts
+++ b/packages/ra-core/src/routing/useRedirect.ts
@@ -4,7 +4,7 @@ import { parsePath } from 'history';
import { Identifier, RaRecord } from '../types';
import { useBasename } from './useBasename';
-import { useCreatePath } from './useCreatePath';
+import { CreatePathType, useCreatePath } from './useCreatePath';
type RedirectToFunction = (
resource?: string,
@@ -13,7 +13,7 @@ type RedirectToFunction = (
state?: object
) => To;
-export type RedirectionSideEffect = string | false | RedirectToFunction;
+export type RedirectionSideEffect = CreatePathType | false | RedirectToFunction;
/**
* Hook for Redirection Side Effect
@@ -41,9 +41,9 @@ export const useRedirect = () => {
(
redirectTo: RedirectionSideEffect,
resource: string = '',
- id?: Identifier,
- data?: Partial,
- state: object = {}
+ id?: Identifier | null,
+ data?: Partial | null,
+ state: object | null = {}
) => {
if (!redirectTo) {
return;
From 03becf671469d7bd461c3445e7215b6ca6fd41aa Mon Sep 17 00:00:00 2001
From: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Date: Wed, 5 Apr 2023 17:48:39 +0200
Subject: [PATCH 2/2] Remove null in useRedirect type
---
packages/ra-core/src/routing/useRedirect.spec.tsx | 12 ++++++------
packages/ra-core/src/routing/useRedirect.ts | 6 +++---
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/packages/ra-core/src/routing/useRedirect.spec.tsx b/packages/ra-core/src/routing/useRedirect.spec.tsx
index 56e66ee1b3a..fbbcdedf984 100644
--- a/packages/ra-core/src/routing/useRedirect.spec.tsx
+++ b/packages/ra-core/src/routing/useRedirect.spec.tsx
@@ -13,15 +13,15 @@ import { Identifier, RaRecord } from '../types';
const Redirect = ({
redirectTo,
resource = '',
- id = null,
- data = null,
- state = null,
+ id = undefined,
+ data = undefined,
+ state = undefined,
}: {
redirectTo: RedirectionSideEffect;
resource?: string;
- id?: Identifier | null;
- data?: Partial | null;
- state?: object | null;
+ id?: Identifier;
+ data?: Partial;
+ state?: object;
}) => {
const redirect = useRedirect();
useEffect(() => {
diff --git a/packages/ra-core/src/routing/useRedirect.ts b/packages/ra-core/src/routing/useRedirect.ts
index c42c149c78d..78d9068794c 100644
--- a/packages/ra-core/src/routing/useRedirect.ts
+++ b/packages/ra-core/src/routing/useRedirect.ts
@@ -41,9 +41,9 @@ export const useRedirect = () => {
(
redirectTo: RedirectionSideEffect,
resource: string = '',
- id?: Identifier | null,
- data?: Partial | null,
- state: object | null = {}
+ id?: Identifier,
+ data?: Partial,
+ state: object = {}
) => {
if (!redirectTo) {
return;