Skip to content

Commit 8775f84

Browse files
committed
refactor(hooks): add transitioning status, re-add isPending shorthand status
1 parent 3ab1a16 commit 8775f84

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ export const getActionStatus = <ServerError, S extends StandardSchemaV1 | undefi
2222
}): HookActionStatus => {
2323
if (isIdle) {
2424
return "idle";
25-
} else if (isExecuting || isTransitioning) {
25+
} else if (isExecuting) {
2626
return "executing";
27+
} else if (isTransitioning) {
28+
return "transitioning";
2729
} else if (
2830
hasThrownError ||
2931
typeof result.validationErrors !== "undefined" ||
@@ -37,18 +39,12 @@ export const getActionStatus = <ServerError, S extends StandardSchemaV1 | undefi
3739
}
3840
};
3941

40-
export const getActionShorthandStatusObject = ({
41-
status,
42-
isTransitioning,
43-
}: {
44-
status: HookActionStatus;
45-
isTransitioning: boolean;
46-
}): HookShorthandStatus => {
42+
export const getActionShorthandStatusObject = (status: HookActionStatus): HookShorthandStatus => {
4743
return {
4844
isIdle: status === "idle",
4945
isExecuting: status === "executing",
50-
isPending: status === "executing",
51-
isTransitioning,
46+
isTransitioning: status === "transitioning",
47+
isPending: status === "executing" || status === "transitioning",
5248
hasSucceeded: status === "hasSucceeded",
5349
hasErrored: status === "hasErrored",
5450
hasNavigated: status === "hasNavigated",
@@ -89,6 +85,8 @@ export const useActionCallbacks = <ServerError, S extends StandardSchemaV1 | und
8985
case "executing":
9086
await Promise.resolve(onExecute?.({ input })).then(() => {});
9187
break;
88+
case "transitioning":
89+
break;
9290
case "hasSucceeded":
9391
if (navigationError || thrownError) {
9492
break;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const useAction = <ServerError, S extends StandardSchemaV1 | undefined, C
138138
result,
139139
reset,
140140
status,
141-
...getActionShorthandStatusObject({ status, isTransitioning }),
141+
...getActionShorthandStatusObject(status),
142142
};
143143
};
144144

@@ -281,7 +281,7 @@ export const useOptimisticAction = <ServerError, S extends StandardSchemaV1 | un
281281
optimisticState,
282282
reset,
283283
status,
284-
...getActionShorthandStatusObject({ status, isTransitioning }),
284+
...getActionShorthandStatusObject(status),
285285
};
286286
};
287287

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type HookSafeStateActionFn<ServerError, S extends StandardSchemaV1 | unde
4242
/**
4343
* Type of the action status returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
4444
*/
45-
export type HookActionStatus = "idle" | "executing" | "hasSucceeded" | "hasErrored" | "hasNavigated";
45+
export type HookActionStatus = "idle" | "executing" | "transitioning" | "hasSucceeded" | "hasErrored" | "hasNavigated";
4646

4747
/**
4848
* Type of the shorthand status object returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
@@ -51,9 +51,6 @@ export type HookShorthandStatus = {
5151
isIdle: boolean;
5252
isExecuting: boolean;
5353
isTransitioning: boolean;
54-
/**
55-
* @deprecated Use `isExecuting` instead.
56-
*/
5754
isPending: boolean;
5855
hasSucceeded: boolean;
5956
hasErrored: boolean;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ export const useStateAction = <ServerError, S extends StandardSchemaV1 | undefin
7272
input: clientInput as InferInputOrDefault<S, undefined>,
7373
result,
7474
status,
75-
...getActionShorthandStatusObject({ status, isTransitioning }),
75+
...getActionShorthandStatusObject(status),
7676
};
7777
};

0 commit comments

Comments
 (0)