Skip to content

Commit 9806af0

Browse files
committed
[TypeScript] Fix onSuccess / onFailure types
Closes #5761 Supersedes #5787
1 parent 0d19c5a commit 9806af0

17 files changed

+89
-56
lines changed

packages/ra-core/src/controller/button/useDeleteWithConfirmController.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
useRedirect,
1313
RedirectionSideEffect,
1414
} from '../../sideEffect';
15-
import { Record, MutationMode } from '../../types';
16-
import { OnFailure, OnSuccess } from '../saveModifiers';
15+
import { Record, MutationMode, OnFailure, OnSuccess } from '../../types';
1716
import { useResourceContext } from '../../core';
1817

1918
/**

packages/ra-core/src/controller/button/useDeleteWithUndoController.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
useRedirect,
88
RedirectionSideEffect,
99
} from '../../sideEffect';
10-
import { Record } from '../../types';
11-
import { OnFailure, OnSuccess } from '../saveModifiers';
10+
import { Record, OnFailure, OnSuccess } from '../../types';
1211
import { useResourceContext } from '../../core';
1312

1413
/**

packages/ra-core/src/controller/details/SaveContext.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ import { createContext, MutableRefObject, useContext, useMemo } from 'react';
33
import pick from 'lodash/pick';
44

55
import { RedirectionSideEffect } from '../../sideEffect';
6-
import { Record } from '../../types';
7-
import {
8-
OnFailure,
9-
OnSuccess,
10-
SideEffectContextValue,
11-
TransformData,
12-
} from '../saveModifiers';
6+
import { Record, OnFailure, OnSuccess } from '../../types';
7+
import { SideEffectContextValue, TransformData } from '../saveModifiers';
138

149
interface SaveContextValue extends SideEffectContextValue {
1510
onFailureRef?: MutableRefObject<OnFailure>;

packages/ra-core/src/controller/details/useCreateController.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import {
1313
RedirectionSideEffect,
1414
} from '../../sideEffect';
1515
import {
16-
OnSuccess,
1716
SetOnSuccess,
18-
OnFailure,
1917
SetOnFailure,
2018
TransformData,
2119
SetTransformData,
@@ -24,7 +22,7 @@ import {
2422
import { useTranslate } from '../../i18n';
2523
import useVersion from '../useVersion';
2624
import { CRUD_CREATE } from '../../actions';
27-
import { Record } from '../../types';
25+
import { Record, OnSuccess, OnFailure } from '../../types';
2826
import { useResourceContext } from '../../core';
2927

3028
export interface CreateProps<RecordType extends Omit<Record, 'id'> = Record> {

packages/ra-core/src/controller/details/useEditController.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import inflection from 'inflection';
33

44
import useVersion from '../useVersion';
55
import { useCheckMinimumRequiredProps } from '../checkMinimumRequiredProps';
6-
import { Record, Identifier, MutationMode } from '../../types';
6+
import {
7+
Record,
8+
Identifier,
9+
MutationMode,
10+
OnSuccess,
11+
OnFailure,
12+
} from '../../types';
713
import {
814
useNotify,
915
useRedirect,
@@ -14,9 +20,7 @@ import { useGetOne, useUpdate } from '../../dataProvider';
1420
import { useTranslate } from '../../i18n';
1521
import { CRUD_GET_ONE, CRUD_UPDATE } from '../../actions';
1622
import {
17-
OnSuccess,
1823
SetOnSuccess,
19-
OnFailure,
2024
SetOnFailure,
2125
TransformData,
2226
SetTransformData,

packages/ra-core/src/controller/saveModifiers.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { createContext, useRef } from 'react';
3+
import { OnSuccess, OnFailure } from '../types';
34

45
export const SideEffectContext = createContext<SideEffectContextValue>({});
56

@@ -67,9 +68,7 @@ export const useSaveModifiers = ({
6768
};
6869
};
6970

70-
export type OnSuccess = (response: any) => void;
7171
export type SetOnSuccess = (onSuccess: OnSuccess) => void;
72-
export type OnFailure = (error: { message?: string }) => void;
7372
export type SetOnFailure = (onFailure: OnFailure) => void;
7473
export type TransformData = (data: any) => any | Promise<any>;
7574
export type SetTransformData = (transform: TransformData) => void;

packages/ra-core/src/dataProvider/performQuery/QueryFunctionParams.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Dispatch } from 'redux';
2-
import { DataProvider } from '../../types';
2+
import { DataProvider, OnSuccess, OnFailure } from '../../types';
33

44
export interface QueryFunctionParams {
55
/** The fetch type, e.g. `UPDATE_MANY` */
@@ -8,9 +8,12 @@ export interface QueryFunctionParams {
88
resource: string;
99
/** The root action name, e.g. `CRUD_GET_MANY` */
1010
action: string;
11-
rest: any;
12-
onSuccess?: (args?: any) => void;
13-
onFailure?: (error: any) => void;
11+
rest?: {
12+
fetch?: string;
13+
meta?: object;
14+
};
15+
onSuccess?: OnSuccess;
16+
onFailure?: OnFailure;
1417
dataProvider: DataProvider;
1518
dispatch: Dispatch;
1619
logoutIfAccessDenied: (error?: any) => Promise<boolean>;

packages/ra-core/src/dataProvider/useDataProviderWithDeclarativeSideEffects.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ const useDataProviderWithDeclarativeSideEffects = (): DataProviderProxy => {
4040
['onSuccess', 'onFailure'].includes(key)
4141
)
4242
) {
43-
const sideEffect = getSideEffects(resource, options);
43+
const sideEffect = getSideEffects(
44+
resource,
45+
options as unknown
46+
);
4447
let {
4548
onSuccess: ignoreOnSuccess, // Used to extract options without onSuccess
4649
onFailure: ignoreOnFailure, // Used to extract options without onFailure

packages/ra-core/src/dataProvider/useDeclarativeSideEffects.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import {
33
useRedirect,
44
useRefresh,
55
useUnselectAll,
6+
NotificationSideEffect,
7+
RedirectionSideEffect,
68
} from '../sideEffect';
9+
import { OnSuccess, OnFailure } from '../types';
710
import { useMemo } from 'react';
811

12+
const defaultSideEffects = {
13+
onSuccess: undefined,
14+
onFailure: undefined,
15+
};
916
const useDeclarativeSideEffects = () => {
1017
const notify = useNotify();
1118
const redirect = useRedirect();
@@ -15,11 +22,11 @@ const useDeclarativeSideEffects = () => {
1522
return useMemo(
1623
() => (
1724
resource,
18-
{ onSuccess, onFailure }: any = {
19-
onSuccess: undefined,
20-
onFailure: undefined,
21-
}
22-
) => {
25+
{
26+
onSuccess,
27+
onFailure,
28+
}: DeclarativeSideEffects = defaultSideEffects
29+
): FunctionSideEffects => {
2330
const convertToFunctionSideEffect = (resource, sideEffects) => {
2431
if (!sideEffects || typeof sideEffects === 'function') {
2532
return sideEffects;
@@ -68,4 +75,20 @@ const useDeclarativeSideEffects = () => {
6875
);
6976
};
7077

78+
export interface DeclarativeSideEffect {
79+
notification?: NotificationSideEffect;
80+
redirectTo?: RedirectionSideEffect;
81+
refresh?: boolean;
82+
unselectAll?: boolean;
83+
}
84+
85+
export interface DeclarativeSideEffects {
86+
onSuccess?: DeclarativeSideEffect;
87+
onFailure?: DeclarativeSideEffect;
88+
}
89+
export interface FunctionSideEffects {
90+
onSuccess: OnSuccess;
91+
onFailure: OnFailure;
92+
}
93+
7194
export default useDeclarativeSideEffects;

packages/ra-core/src/dataProvider/useMutation.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { useCallback } from 'react';
22
import merge from 'lodash/merge';
33

44
import { useSafeSetState } from '../util/hooks';
5-
import { MutationMode } from '../types';
5+
import { MutationMode, OnSuccess, OnFailure } from '../types';
66
import useDataProvider from './useDataProvider';
77
import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDeclarativeSideEffects';
8+
import { DeclarativeSideEffect } from './useDeclarativeSideEffects';
89

910
/**
1011
* Get a callback to fetch the data provider through Redux, usually for mutations.
@@ -33,8 +34,8 @@ import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDecl
3334
* @param {string} options.action Redux action type
3435
* @param {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider
3536
* @param {boolean} options.returnPromise Set to true to return the result promise of the mutation
36-
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() }
37-
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
37+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. () => refresh()
38+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. (error) => notify(error.message)
3839
* @param {boolean} options.withDeclarativeSideEffectsSupport Set to true to support legacy side effects e.g. { onSuccess: { refresh: true } }
3940
*
4041
* @returns A tuple with the mutation callback and the request state. Destructure as [mutate, { data, total, error, loading, loaded }].
@@ -220,8 +221,8 @@ export interface Mutation {
220221
export interface MutationOptions {
221222
action?: string;
222223
returnPromise?: boolean;
223-
onSuccess?: (response: any) => any | Object;
224-
onFailure?: (error?: any) => any | Object;
224+
onSuccess?: OnSuccess | DeclarativeSideEffect;
225+
onFailure?: OnFailure | DeclarativeSideEffect;
225226
withDeclarativeSideEffectsSupport?: boolean;
226227
/** @deprecated use mutationMode: undoable instead */
227228
undoable?: boolean;

packages/ra-core/src/dataProvider/useQuery.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useEffect } from 'react';
22

33
import { useSafeSetState } from '../util/hooks';
4+
import { OnSuccess, OnFailure } from '../types';
45
import useDataProvider from './useDataProvider';
56
import useDataProviderWithDeclarativeSideEffects from './useDataProviderWithDeclarativeSideEffects';
7+
import { DeclarativeSideEffect } from './useDeclarativeSideEffects';
68
import useVersion from '../controller/useVersion';
79

810
/**
@@ -20,8 +22,8 @@ import useVersion from '../controller/useVersion';
2022
* @param {Object} query.payload The payload object, e.g; { post_id: 12 }
2123
* @param {Object} options
2224
* @param {string} options.action Redux action type
23-
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() }
24-
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
25+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. () => refresh()
26+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. (error) => notify(error.message)
2527
* @param {boolean} options.withDeclarativeSideEffectsSupport Set to true to support legacy side effects e.g. { onSuccess: { refresh: true } }
2628
*
2729
* @returns The current request state. Destructure as { data, total, error, loading, loaded }.
@@ -143,8 +145,8 @@ export interface Query {
143145

144146
export interface QueryOptions {
145147
action?: string;
146-
onSuccess?: (response: any) => any | Object;
147-
onFailure?: (error?: any) => any | Object;
148+
onSuccess?: OnSuccess | DeclarativeSideEffect;
149+
onFailure?: OnFailure | DeclarativeSideEffect;
148150
withDeclarativeSideEffectsSupport?: boolean;
149151
}
150152

packages/ra-core/src/dataProvider/useQueryWithStore.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import useDataProvider from './useDataProvider';
66
import useVersion from '../controller/useVersion';
77
import getFetchType from './getFetchType';
88
import { useSafeSetState } from '../util/hooks';
9-
import { ReduxState } from '../types';
9+
import { ReduxState, OnSuccess, OnFailure } from '../types';
1010

1111
export interface Query {
1212
type: string;
@@ -23,8 +23,8 @@ export interface StateResult {
2323
}
2424

2525
export interface QueryOptions {
26-
onSuccess?: (args?: any) => void;
27-
onFailure?: (error: any) => void;
26+
onSuccess?: OnSuccess;
27+
onFailure?: OnFailure;
2828
action?: string;
2929
[key: string]: any;
3030
}
@@ -79,8 +79,8 @@ const defaultIsDataLoaded = (data: any): boolean => data !== undefined;
7979
* @param {Object} query.payload The payload object, e.g; { post_id: 12 }
8080
* @param {Object} options
8181
* @param {string} options.action Redux action type
82-
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() }
83-
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
82+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. () => refresh()
83+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. (error) => notify(error.message)
8484
* @param {Function} dataSelector Redux selector to get the result. Required.
8585
* @param {Function} totalSelector Redux selector to get the total (optional, only for LIST queries)
8686
* @param {Function} isDataLoaded

packages/ra-core/src/form/FormWithRedirect.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import useInitializeFormWithRecord from './useInitializeFormWithRecord';
1111
import useWarnWhenUnsavedChanges from './useWarnWhenUnsavedChanges';
1212
import sanitizeEmptyValues from './sanitizeEmptyValues';
1313
import getFormInitialValues from './getFormInitialValues';
14-
import { FormContextValue, Record } from '../types';
14+
import { FormContextValue, Record, OnSuccess, OnFailure } from '../types';
1515
import { RedirectionSideEffect } from '../sideEffect';
1616
import { useDispatch } from 'react-redux';
1717
import { setAutomaticRefresh } from '../actions/uiActions';
@@ -192,8 +192,8 @@ export type FormWithRedirectSave = (
192192
data: Partial<Record>,
193193
redirectTo: RedirectionSideEffect,
194194
options?: {
195-
onSuccess?: (data?: any) => void;
196-
onFailure?: (error: any) => void;
195+
onSuccess?: OnSuccess;
196+
onFailure?: OnFailure;
197197
}
198198
) => void;
199199
export interface FormWithRedirectOwnProps {

packages/ra-core/src/sideEffect/fetch.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
fetchActionsWithTotalResponse,
1616
sanitizeFetchType,
1717
} from '../core';
18+
import { DeclarativeSideEffect } from '../dataProvider/useDeclarativeSideEffects';
1819

1920
function validateResponseFormat(
2021
response,
@@ -73,8 +74,8 @@ interface ActionWithSideEffect {
7374
meta: {
7475
fetch: string;
7576
resource: string;
76-
onSuccess?: any;
77-
onFailure?: any;
77+
onSuccess?: DeclarativeSideEffect;
78+
onFailure?: DeclarativeSideEffect;
7879
};
7980
}
8081

packages/ra-core/src/types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ export type DataProviderProxy = {
286286
};
287287

288288
export type MutationMode = 'pessimistic' | 'optimistic' | 'undoable';
289+
export type OnSuccess = (response?: any) => void;
290+
export type OnFailure = (error?: any) => void;
289291

290292
export interface UseDataProviderOptions {
291293
action?: string;
@@ -294,8 +296,8 @@ export interface UseDataProviderOptions {
294296
// @deprecated use mode: 'undoable' instead
295297
undoable?: boolean;
296298
mutationMode?: MutationMode;
297-
onSuccess?: any;
298-
onFailure?: any;
299+
onSuccess?: OnSuccess;
300+
onFailure?: OnFailure;
299301
}
300302

301303
export type LegacyDataProvider = (

packages/ra-ui-materialui/src/form/TabbedForm.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
MutationMode,
2020
Record,
2121
RedirectionSideEffect,
22+
OnSuccess,
23+
OnFailure,
2224
} from 'ra-core';
2325
import { FormRenderProps } from 'react-final-form';
2426
import get from 'lodash/get';
@@ -141,8 +143,8 @@ export interface TabbedFormProps
141143
data: Partial<Record>,
142144
redirectTo: RedirectionSideEffect,
143145
options?: {
144-
onSuccess?: (data?: any) => void;
145-
onFailure?: (error: any) => void;
146+
onSuccess?: OnSuccess;
147+
onFailure?: OnFailure;
146148
}
147149
) => void;
148150
submitOnEnter?: boolean;

packages/ra-ui-materialui/src/types.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
ResourceComponentProps,
99
ResourceComponentPropsWithId,
1010
MutationMode,
11+
OnSuccess,
12+
OnFailure,
1113
} from 'ra-core';
1214

1315
export interface ListProps extends ResourceComponentProps {
@@ -38,8 +40,8 @@ export interface EditProps extends ResourceComponentPropsWithId {
3840
/** @deprecated use mutationMode: undoable instead */
3941
undoable?: boolean;
4042
mutationMode?: MutationMode;
41-
onSuccess?: (data: RaRecord) => void;
42-
onFailure?: (error: any) => void;
43+
onSuccess?: OnSuccess;
44+
onFailure?: OnFailure;
4345
transform?: (data: RaRecord) => RaRecord | Promise<RaRecord>;
4446
title?: string | ReactElement;
4547
}
@@ -51,8 +53,8 @@ export interface CreateProps extends ResourceComponentProps {
5153
className?: string;
5254
component?: ElementType;
5355
record?: Partial<RaRecord>;
54-
onSuccess?: (data: RaRecord) => void;
55-
onFailure?: (error: any) => void;
56+
onSuccess?: OnSuccess;
57+
onFailure?: OnFailure;
5658
transform?: (data: RaRecord) => RaRecord | Promise<RaRecord>;
5759
title?: string | ReactElement;
5860
}

0 commit comments

Comments
 (0)