Skip to content

Commit 68d8ed2

Browse files
committedJun 12, 2024
fix: make result optional to handle Next navigation events
1 parent 9b37224 commit 68d8ed2

File tree

6 files changed

+48
-41
lines changed

6 files changed

+48
-41
lines changed
 

‎apps/playground/src/app/(examples)/stateful-form/page.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useStateAction } from "next-safe-action/hooks";
88
import { statefulAction } from "./stateful-action";
99

1010
export default function StatefulFormPage() {
11-
const { execute, result, status } = useStateAction(statefulAction, {
11+
const { execute, result, status, input } = useStateAction(statefulAction, {
1212
initResult: { data: { newName: "jane" } }, // optionally pass initial state
1313
onSuccess({ data, input }) {
1414
console.log("HELLO FROM ONSUCCESS", data, input);
@@ -20,7 +20,10 @@ export default function StatefulFormPage() {
2020
console.log("HELLO FROM ONSETTLED", result, input);
2121
},
2222
onExecute({ input }) {
23-
console.log("HELLO FROM ONEXECUTE", input);
23+
console.log(
24+
"HELLO FROM ONEXECUTE",
25+
Object.fromEntries(input?.entries() ?? [])
26+
);
2427
},
2528
});
2629

‎apps/playground/src/app/(examples)/stateful-form/stateful-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const statefulAction = action
2222
await new Promise((res) => setTimeout(res, 1000));
2323

2424
return {
25-
prevName: prevResult.data?.newName,
25+
prevName: prevResult?.data?.newName,
2626
newName: parsedInput.name,
2727
};
2828
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type HookResult<
1313
CVE,
1414
CBAVE,
1515
Data,
16-
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
16+
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
1717
fetchError?: string;
1818
};
1919

‎packages/next-safe-action/src/index.types.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export type SafeActionResult<
3434
Data = unknown,
3535
// eslint-disable-next-line
3636
NextCtx = unknown,
37-
> = {
38-
data?: Data;
39-
serverError?: ServerError;
40-
validationErrors?: CVE;
41-
bindArgsValidationErrors?: CBAVE;
42-
};
37+
> =
38+
| {
39+
data?: Data;
40+
serverError?: ServerError;
41+
validationErrors?: CVE;
42+
bindArgsValidationErrors?: CBAVE;
43+
}
44+
| undefined;
4345

4446
/**
4547
* Type of the function called from components with type safe input data.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const statefulAction = actionClient
5252
await new Promise((res) => setTimeout(res, 1000));
5353

5454
return {
55-
prevName: prevResult.data?.newName,
55+
prevName: prevResult?.data?.newName,
5656
newName: parsedInput.name,
5757
};
5858
});

‎website/docs/types.md

+32-30
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ export type SafeActionResult<
4646
Data = unknown,
4747
// eslint-disable-next-line
4848
NextCtx = unknown,
49-
> = {
50-
data?: Data;
51-
serverError?: ServerError;
52-
validationErrors?: CVE;
53-
bindArgsValidationErrors?: CBAVE;
54-
};
49+
> =
50+
| {
51+
data?: Data;
52+
serverError?: ServerError;
53+
validationErrors?: CVE;
54+
bindArgsValidationErrors?: CBAVE;
55+
};
56+
| undefined;
5557
```
5658

5759
### `SafeActionFn`
@@ -175,30 +177,30 @@ Type of action execution callbacks. These are called after the action is execute
175177

176178
```typescript
177179
export type SafeActionCallbacks<
178-
ServerError,
179-
S extends Schema | undefined,
180-
BAS extends readonly Schema[],
181-
CVE,
182-
CBAVE,
183-
Data,
180+
ServerError,
181+
S extends Schema | undefined,
182+
BAS extends readonly Schema[],
183+
CVE,
184+
CBAVE,
185+
Data,
184186
> = {
185-
onSuccess?: (args: {
186-
data?: Data;
187-
clientInput: S extends Schema ? InferIn<S> : undefined;
188-
bindArgsClientInputs: InferInArray<BAS>;
189-
parsedInput: S extends Schema ? Infer<S> : undefined;
190-
bindArgsParsedInputs: InferArray<BAS>;
191-
}) => MaybePromise<void>;
192-
onError?: (args: {
193-
error: Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">;
194-
clientInput: S extends Schema ? InferIn<S> : undefined;
195-
bindArgsClientInputs: InferInArray<BAS>;
196-
}) => MaybePromise<void>;
197-
onSettled?: (args: {
198-
result: SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>;
199-
clientInput: S extends Schema ? InferIn<S> : undefined;
200-
bindArgsClientInputs: InferInArray<BAS>;
201-
}) => MaybePromise<void>;
187+
onSuccess?: (args: {
188+
data?: Data;
189+
clientInput: S extends Schema ? InferIn<S> : undefined;
190+
bindArgsClientInputs: InferInArray<BAS>;
191+
parsedInput: S extends Schema ? Infer<S> : undefined;
192+
bindArgsParsedInputs: InferArray<BAS>;
193+
}) => MaybePromise<void>;
194+
onError?: (args: {
195+
error: Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">;
196+
clientInput: S extends Schema ? InferIn<S> : undefined;
197+
bindArgsClientInputs: InferInArray<BAS>;
198+
}) => MaybePromise<void>;
199+
onSettled?: (args: {
200+
result: SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>;
201+
clientInput: S extends Schema ? InferIn<S> : undefined;
202+
bindArgsClientInputs: InferInArray<BAS>;
203+
}) => MaybePromise<void>;
202204
};
203205
```
204206

@@ -282,7 +284,7 @@ export type HookResult<
282284
CVE,
283285
CBAVE,
284286
Data,
285-
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
287+
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
286288
fetchError?: string;
287289
};
288290
```

0 commit comments

Comments
 (0)
Please sign in to comment.