diff --git a/packages/example-app/src/app/hook/deleteuser-form.tsx b/packages/example-app/src/app/hook/deleteuser-form.tsx index 59b9e0df..6ad32e42 100644 --- a/packages/example-app/src/app/hook/deleteuser-form.tsx +++ b/packages/example-app/src/app/hook/deleteuser-form.tsx @@ -24,6 +24,12 @@ const DeleteUserForm = ({ userId, deleteUser }: Props) => { // You can reset result object by calling `reset`. // reset(); }, + onSettled(result, input, reset) { + console.log("HELLO FROM ONSETTLED", result, input); + + // You can reset result object by calling `reset`. + // reset(); + }, onExecute(input) { console.log("HELLO FROM ONEXECUTE", input); }, diff --git a/packages/example-app/src/app/optimistic-hook/addlikes-form.tsx b/packages/example-app/src/app/optimistic-hook/addlikes-form.tsx index 7a013f13..94eed12d 100644 --- a/packages/example-app/src/app/optimistic-hook/addlikes-form.tsx +++ b/packages/example-app/src/app/optimistic-hook/addlikes-form.tsx @@ -30,6 +30,12 @@ const AddLikesForm = ({ likesCount, addLikes }: Props) => { // You can reset result object by calling `reset`. // reset(); }, + onSettled(result, input, reset) { + console.log("HELLO FROM ONSETTLED", result, input); + + // You can reset result object by calling `reset`. + // reset(); + }, onExecute(input) { console.log("HELLO FROM ONEXECUTE", input); }, diff --git a/packages/next-safe-action/src/hook.ts b/packages/next-safe-action/src/hook.ts index c8ccbbf9..c7e3e929 100644 --- a/packages/next-safe-action/src/hook.ts +++ b/packages/next-safe-action/src/hook.ts @@ -44,12 +44,14 @@ const useActionCallbacks = ( const onExecuteRef = useRef(cb?.onExecute); const onSuccessRef = useRef(cb?.onSuccess); const onErrorRef = useRef(cb?.onError); + const onSettledRef = useRef(cb?.onSettled); // Execute the callback on success or error, if provided. useEffect(() => { const onExecute = onExecuteRef.current; const onSuccess = onSuccessRef.current; const onError = onErrorRef.current; + const onSettled = onSettledRef.current; const executeCallbacks = async () => { switch (status) { @@ -58,9 +60,11 @@ const useActionCallbacks = ( break; case "hasSucceded": await Promise.resolve(onSuccess?.(result.data!, input, reset)); + await Promise.resolve(onSettled?.(result, input, reset)); break; case "hasErrored": await Promise.resolve(onError?.(result, input, reset)); + await Promise.resolve(onSettled?.(result, input, reset)); break; } }; diff --git a/packages/next-safe-action/src/types.ts b/packages/next-safe-action/src/types.ts index 93949844..8b9aa08c 100644 --- a/packages/next-safe-action/src/types.ts +++ b/packages/next-safe-action/src/types.ts @@ -46,6 +46,11 @@ export type HookCallbacks = { input: z.input, reset: () => void ) => MaybePromise; + onSettled?: ( + result: HookResult, + input: z.input, + reset: () => void + ) => MaybePromise; }; /**