Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Sep 30, 2019
1 parent 4c88b3b commit b002b35
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 66 deletions.
2 changes: 1 addition & 1 deletion examples/simple/src/comments/PostQuickCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PostQuickCreate = ({ onCancel, onSave, ...props }) => {
onSuccess: {
callback: ({ payload: { data } }) => onSave(data),
},
onError: {
onFailure: {
callback: ({ error }) => {
dispatch(showNotification(error.message, 'error'));
},
Expand Down
12 changes: 6 additions & 6 deletions packages/ra-core/src/dataFetchActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export const fetchActionsWithTotalResponse = [GET_LIST, GET_MANY_REFERENCE];

export const sanitizeFetchType = (fetchType: string) => {
switch (fetchType) {
case CREATE:
return 'create';
case GET_LIST:
return 'getList';
case GET_ONE:
Expand All @@ -33,14 +31,16 @@ export const sanitizeFetchType = (fetchType: string) => {
return 'getMany';
case GET_MANY_REFERENCE:
return 'getManyReference';
case DELETE:
return 'delete';
case DELETE_MANY:
return 'deleteMany';
case CREATE:
return 'create';
case UPDATE:
return 'update';
case UPDATE_MANY:
return 'updateMany';
case DELETE:
return 'delete';
case DELETE_MANY:
return 'deleteMany';
default:
return fetchType;
}
Expand Down
30 changes: 7 additions & 23 deletions packages/ra-core/src/dataProvider/Mutation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FunctionComponent, useCallback } from 'react';
import merge from 'lodash/merge';
import { FunctionComponent } from 'react';

import useMutation from './useMutation';

Expand Down Expand Up @@ -58,27 +57,12 @@ const Mutation: FunctionComponent<Props> = ({
resource,
payload,
options,
}) => {
const [mutate, state] = useMutation();

const finalMutate = useCallback(
(event: any, callTimeData?: any, callTimeOptions?: any) =>
mutate(
{
resource,
payload: merge({}, payload, callTimeData),
type,
},
merge(
{ withDeclarativeSideEffectsSupport: true },
options,
callTimeOptions
)
),
[mutate, resource, JSON.stringify({ payload, options })] // eslint-disable-line react-hooks/exhaustive-deps
}) =>
children(
...useMutation(
{ type, resource, payload },
{ ...options, withDeclarativeSideEffectsSupport: true }
)
);

return children(finalMutate, state);
};

export default Mutation;
24 changes: 12 additions & 12 deletions packages/ra-core/src/dataProvider/getFetchType.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
CREATE,
DELETE,
DELETE_MANY,
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
GET_ONE,
CREATE,
UPDATE,
UPDATE_MANY,
DELETE,
DELETE_MANY,
} from '../dataFetchActions';

/**
Expand All @@ -19,24 +19,24 @@ import {
*/
export default actionType => {
switch (actionType) {
case 'create':
return CREATE;
case 'delete':
return DELETE;
case 'deleteMany':
return DELETE_MANY;
case 'getList':
return GET_LIST;
case 'getOne':
return GET_ONE;
case 'getMany':
return GET_MANY;
case 'getManyReference':
return GET_MANY_REFERENCE;
case 'getOne':
return GET_ONE;
case 'create':
return CREATE;
case 'update':
return UPDATE;
case 'updateMany':
return UPDATE_MANY;
case 'delete':
return DELETE;
case 'deleteMany':
return DELETE_MANY;

default:
return actionType;
Expand Down
15 changes: 2 additions & 13 deletions packages/ra-core/src/dataProvider/useCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,7 @@ import useMutation from './useMutation';
* return <button disabled={loading} onClick={create}>Like</div>;
* };
*/
const useCreate = (
resource: string,
data: any = {},
options?: any
): [
(event: any, callTimePayload?: any, callTimeOptions?: any) => void,
{
data?: any;
error?: any;
loading: boolean;
loaded: boolean;
}
] => useMutation({ type: 'create', resource, payload: data }, options);
const useCreate = (resource: string, data: any = {}, options?: any) =>
useMutation({ type: 'create', resource, payload: { data } }, options);

export default useCreate;
4 changes: 2 additions & 2 deletions packages/ra-core/src/dataProvider/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ export interface MutationOptions {
action?: string;
undoable?: boolean;
onSuccess?: (response: any) => any | Object;
onError?: (error?: any) => any | Object;
onFailure?: (error?: any) => any | Object;
withDeclarativeSideEffectsSupport?: boolean;
}

export type UseMutationValue = [
(query: Mutation, options?: MutationOptions) => void,
(query: Partial<Mutation>, options?: Partial<MutationOptions>) => void,
{
data?: any;
total?: number;
Expand Down
10 changes: 1 addition & 9 deletions packages/ra-core/src/dataProvider/useUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ const useUpdate = (
data?: any,
previousData?: any,
options?: any
): [
(event: any, callTimePayload?: any, callTimeOptions?: any) => void,
{
data?: any;
error?: any;
loading: boolean;
loaded: boolean;
}
] =>
) =>
useMutation(
{ type: 'update', resource, payload: { id, data, previousData } },
options
Expand Down

0 comments on commit b002b35

Please sign in to comment.