Skip to content

Commit

Permalink
Allow to extend useEditController
Browse files Browse the repository at this point in the history
`useCreateController` accepts a onSuccess and a onFailure props, but `useEditController` don't
So, I made this PR to have the same behavior for both of them
  • Loading branch information
Kmaschta committed Dec 4, 2019
1 parent 90950fb commit 8c1ed74
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
9 changes: 8 additions & 1 deletion packages/ra-core/src/controller/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export interface CreateControllerProps {
loaded: boolean;
saving: boolean;
defaultTitle: string;
save: (record: Partial<Record>, redirect: RedirectionSideEffect) => void;
save: (
record: Partial<Record>,
redirect: RedirectionSideEffect,
callbacks?: {
onSuccess: () => void;
onFailure: (error: string | { message?: string }) => void;
}
) => void;
resource: string;
basePath: string;
record?: Partial<Record>;
Expand Down
56 changes: 36 additions & 20 deletions packages/ra-core/src/controller/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ export interface EditControllerProps {
loaded: boolean;
saving: boolean;
defaultTitle: string;
save: (data: Record, redirect?: RedirectionSideEffect) => void;
save: (
data: Record,
redirect?: RedirectionSideEffect,
callbacks?: {
onSuccess: () => void;
onFailure: (error: string | { message?: string }) => void;
}
) => void;
resource: string;
basePath: string;
record?: Record;
Expand Down Expand Up @@ -92,29 +99,38 @@ const useEditController = (props: EditProps): EditControllerProps => {
);

const save = useCallback(
(data: Partial<Record>, redirectTo = 'list') =>
(
data: Partial<Record>,
redirectTo = 'list',
{ onSuccess, onFailure } = {}
) =>
update(
{ payload: { data } },
{
action: CRUD_UPDATE,
onSuccess: () => {
notify(
successMessage || 'ra.notification.updated',
'info',
{
smart_count: 1,
},
undoable
);
redirect(redirectTo, basePath, data.id, data);
},
onFailure: error =>
notify(
typeof error === 'string'
? error
: error.message || 'ra.notification.http_error',
'warning'
),
onSuccess: onSuccess
? onSuccess
: () => {
notify(
successMessage || 'ra.notification.updated',
'info',
{
smart_count: 1,
},
undoable
);
redirect(redirectTo, basePath, data.id, data);
},
onFailure: onFailure
? onFailure
: error =>
notify(
typeof error === 'string'
? error
: error.message ||
'ra.notification.http_error',
'warning'
),
undoable,
}
),
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4981,6 +4981,14 @@ dashdash@^1.12.0:
dependencies:
assert-plus "^1.0.0"

data-generator-retail@^2.7.0:
version "2.9.5"
resolved "https://registry.yarnpkg.com/data-generator-retail/-/data-generator-retail-2.9.5.tgz#9a1a541f7bc19c00b633b0d17e6b39837e398976"
integrity sha512-scx3c91hhTMxFIvNgAO2Y2Xor4eIR8FfZ7LBCHVlsUt7DEIUvH6juHIVjT6ytzQjwBuR03UzEOlZpIMim1+KqQ==
dependencies:
date-fns "~1.29.0"
faker "^4.1.0"

data-urls@^1.0.0, data-urls@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/data-urls/-/data-urls-1.1.0.tgz#15ee0582baa5e22bb59c77140da8f9c76963bbfe"
Expand Down

0 comments on commit 8c1ed74

Please sign in to comment.