Skip to content

Commit 6319f07

Browse files
authoredMay 18, 2024··
refactor(hooks): return shorthand statuses from hooks (#131)
This PR re-adds `isIdle`, `isExecuting`, `hasSucceeded` and `hasErrored` shorthand status properties to hooks return objects. It also removes util functions with the same exported from `next-safe-action/status`. re #129
1 parent 50dff38 commit 6319f07

File tree

9 files changed

+40
-73
lines changed

9 files changed

+40
-73
lines changed
 

‎apps/playground/src/app/(examples)/hook/page.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ import { deleteUser } from "./deleteuser-action";
99

1010
export default function Hook() {
1111
// Safe action (`deleteUser`) and optional callbacks passed to `useAction` hook.
12-
const { execute, result, status, reset } = useAction(deleteUser, {
12+
const {
13+
execute,
14+
result,
15+
status,
16+
reset,
17+
isIdle,
18+
isExecuting,
19+
hasSucceeded,
20+
hasErrored,
21+
} = useAction(deleteUser, {
1322
onSuccess({ data, input }) {
1423
console.log("HELLO FROM ONSUCCESS", data, input);
1524
},
@@ -24,7 +33,7 @@ export default function Hook() {
2433
},
2534
});
2635

27-
console.log("status:", status);
36+
console.dir({ status, isIdle, isExecuting, hasSucceeded, hasErrored });
2837

2938
return (
3039
<main className="w-96 max-w-full px-4">

‎packages/next-safe-action/package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"exports": {
1313
".": "./dist/index.mjs",
1414
"./zod": "./dist/zod.mjs",
15-
"./hooks": "./dist/hooks.mjs",
16-
"./status": "./dist/status.mjs"
15+
"./hooks": "./dist/hooks.mjs"
1716
},
1817
"typesVersions": {
1918
"*": {
@@ -25,9 +24,6 @@
2524
],
2625
"hooks": [
2726
"./dist/hooks.d.mts"
28-
],
29-
"status": [
30-
"./dist/status.d.mts"
3127
]
3228
}
3329
},

‎packages/next-safe-action/src/hooks.ts

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ const getActionStatus = <
5757
}
5858
};
5959

60+
const getActionShorthandStatusObject = (status: HookActionStatus) => {
61+
return {
62+
isIdle: status === "idle",
63+
isExecuting: status === "executing",
64+
hasSucceeded: status === "hasSucceeded",
65+
hasErrored: status === "hasErrored",
66+
};
67+
};
68+
6069
const useActionCallbacks = <
6170
ServerError,
6271
S extends Schema | undefined,
@@ -178,6 +187,7 @@ export const useAction = <
178187
result,
179188
reset,
180189
status,
190+
...getActionShorthandStatusObject(status),
181191
};
182192
};
183193

@@ -264,6 +274,7 @@ export const useOptimisticAction = <
264274
optimisticData,
265275
reset,
266276
status,
277+
...getActionShorthandStatusObject(status),
267278
};
268279
};
269280

@@ -325,6 +336,7 @@ export const useStateAction = <
325336
input: clientInput,
326337
result,
327338
status,
339+
...getActionShorthandStatusObject(status),
328340
};
329341
};
330342

‎packages/next-safe-action/src/status.ts

-41
This file was deleted.

‎packages/next-safe-action/tsup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "tsup";
22

33
export default defineConfig({
4-
entry: ["src/index.ts", "src/zod.ts", "src/hooks.ts", "src/status.ts"],
4+
entry: ["src/index.ts", "src/zod.ts", "src/hooks.ts"],
55
format: ["esm"],
66
clean: true,
77
splitting: false,

‎website/docs/execution/hooks/check-action-status.md

-21
This file was deleted.

‎website/docs/execution/hooks/useaction.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ As you can see, here we display a greet message after the action is performed, i
7777
| `execute` | `(input: InferIn<S>) => void` | An action caller with no return. The input is the same as the safe action you passed to the hook. |
7878
| `input` | `InferIn<S> \| undefined` | The input passed to the `execute` function. |
7979
| `result` | [`HookResult`](/docs/types#hookresult) | When the action gets called via `execute`, this is the result object. |
80-
| `status` | [`HookActionStatus`](/docs/types#hookresult) | The action current status. |
8180
| `reset` | `() => void` | Programmatically reset `input` and `result` object with this function. |
81+
| `status` | [`HookActionStatus`](/docs/types#hookresult) | The action current status. |
82+
| `isIdle` | `boolean` | True if the action status is `idle`. |
83+
| `isExecuting` | `boolean` | True if the action status is `executing`. |
84+
| `hasSucceeded` | `boolean` | True if the action status is `hasSucceeded`. |
85+
| `hasErrored` | `boolean` | True if the action status is `hasErrored`. |
8286

8387
Explore a working example [here](<https://github.com/TheEdoRan/next-safe-action/tree/main/apps/playground/src/app/(examples)/hook>).

‎website/docs/execution/hooks/useoptimisticaction.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ export default function AddLikes({ likesCount }: Props) {
127127
| `execute` | `(input: InferIn<S>) => void` | An action caller with no return. The input is the same as the safe action you passed to the hook. |
128128
| `input` | `InferIn<S> \| undefined` | The input passed to the `execute` function. |
129129
| `result` | [`HookResult`](/docs/types#hookresult) | When the action gets called via `execute`, this is the result object. |
130-
| `status` | [`HookActionStatus`](/docs/types#hookresult) | The action current status. |
131-
| `reset` | `() => void` | Programmatically reset `input` and `result` object with this function. |
132130
| `optimisticData` | `Data` (return type of the `safeActionFn` you passed as first argument) | This is the data that gets updated immediately after `execute` is called, with the behavior you defined in the `reducer` function hook argument. The initial state is what you provided to the hook via `initialOptimisticData` argument. |
131+
| `reset` | `() => void` | Programmatically reset `input` and `result` object with this function. |
132+
| `status` | [`HookActionStatus`](/docs/types#hookresult) | The action current status. |
133+
| `isIdle` | `boolean` | True if the action status is `idle`. |
134+
| `isExecuting` | `boolean` | True if the action status is `executing`. |
135+
| `hasSucceeded` | `boolean` | True if the action status is `hasSucceeded`. |
136+
| `hasErrored` | `boolean` | True if the action status is `hasErrored`. |
133137

134138
Explore a working example [here](<https://github.com/TheEdoRan/next-safe-action/tree/main/apps/playground/src/app/(examples)/optimistic-hook>).

‎website/docs/execution/hooks/usestateaction.md

+4
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,9 @@ You can pass an optional initial result to `useStateAction`, with the `initResul
9898
| `input` | `InferIn<S> \| undefined` | The input passed to the `execute` function. |
9999
| `result` | [`HookResult`](/docs/types#hookresult) | When the action gets called via `execute`, this is the result object. |
100100
| `status` | [`HookActionStatus`](/docs/types#hookresult) | The action current status. |
101+
| `isIdle` | `boolean` | True if the action status is `idle`. |
102+
| `isExecuting` | `boolean` | True if the action status is `executing`. |
103+
| `hasSucceeded` | `boolean` | True if the action status is `hasSucceeded`. |
104+
| `hasErrored` | `boolean` | True if the action status is `hasErrored`. |
101105

102106
Explore a working example [here](<https://github.com/TheEdoRan/next-safe-action/tree/main/apps/playground/src/app/(examples)/stateful-form>).

0 commit comments

Comments
 (0)
Please sign in to comment.