Skip to content

resolve useForm mutation according to mutation mode #1728

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 19, 2022
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
90 changes: 56 additions & 34 deletions packages/core/src/hooks/form/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,37 +184,56 @@ export const useForm = <

const onFinishCreate = async (values: TVariables) => {
setWarnWhen(false);
mutateCreate(
{
values,
resource: resource.name,
successNotification,
errorNotification,
metaData,
dataProviderName,
invalidates,
},
{
onSuccess: (data, variables, context) => {
if (onMutationSuccess) {
onMutationSuccess(data, values, context);
}

const id = data?.data?.id;

handleSubmitWithRedirect({
redirect,
resource,
id,
});

const onSuccess = (id?: BaseKey) => {
handleSubmitWithRedirect({
redirect,
resource,
id,
});
};

if (mutationMode !== "pessimistic") {
setTimeout(() => {
onSuccess();
});
}

return new Promise<void>((resolve, reject) => {
if (mutationMode !== "pessimistic") {
resolve();
}
return mutateCreate(
{
values,
resource: resource.name,
successNotification,
errorNotification,
metaData,
dataProviderName,
invalidates,
},
onError: (error: TError, variables, context) => {
if (onMutationError) {
return onMutationError(error, values, context);
}
{
onSuccess: (data, _, context) => {
if (onMutationSuccess) {
onMutationSuccess(data, values, context);
}

const id = data?.data?.id;

onSuccess(id);

resolve();
},
onError: (error: TError, _, context) => {
if (onMutationError) {
return onMutationError(error, values, context);
}
reject();
},
},
},
);
);
});
};

const onFinishUpdate = async (values: TVariables) => {
Expand Down Expand Up @@ -251,8 +270,11 @@ export const useForm = <
}

// setTimeout is required to make onSuccess e.g. callbacks to work if component unmounts i.e. on route change
return new Promise<void>((resolve, reject) =>
setTimeout(() => {
return new Promise<void>((resolve, reject) => {
if (mutationMode !== "pessimistic") {
resolve();
}
return setTimeout(() => {
mutateUpdate(variables, {
onSuccess: (data, _, context) => {
if (onMutationSuccess) {
Expand All @@ -265,15 +287,15 @@ export const useForm = <

resolve();
},
onError: (error: TError, variables, context) => {
onError: (error: TError, _, context) => {
if (onMutationError) {
return onMutationError(error, values, context);
}
reject();
},
});
}),
);
});
});
};

const createResult = {
Expand Down