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

Better types for useRedirect and useCreatePath #8811

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Changes from all commits
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
13 changes: 11 additions & 2 deletions packages/ra-core/src/routing/useCreatePath.spec.tsx
Original file line number Diff line number Diff line change
@@ -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 <div>{path}</div>;
5 changes: 4 additions & 1 deletion packages/ra-core/src/routing/useCreatePath.ts
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 12 additions & 4 deletions packages/ra-core/src/routing/useRedirect.spec.tsx
Original file line number Diff line number Diff line change
@@ -6,15 +6,22 @@ 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,
resource = '',
id = null,
data = null,
state = null,
id = undefined,
data = undefined,
state = undefined,
}: {
redirectTo: RedirectionSideEffect;
resource?: string;
id?: Identifier;
data?: Partial<RaRecord>;
state?: object;
}) => {
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: '' };
4 changes: 2 additions & 2 deletions packages/ra-core/src/routing/useRedirect.ts
Original file line number Diff line number Diff line change
@@ -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