-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support binding additional arguments (#97)
This PR adds the support for binding additional arguments to the safe action.
- Loading branch information
Showing
26 changed files
with
542 additions
and
218 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/example-app/src/app/(examples)/bind-arguments/onboard-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use server"; | ||
|
||
import { action } from "@/lib/safe-action"; | ||
import { z } from "zod"; | ||
|
||
const schema = z.object({ | ||
username: z.string().min(3).max(30), | ||
}); | ||
|
||
export const onboardUser = action | ||
.metadata({ actionName: "onboardUser" }) | ||
.schema(schema) | ||
.bindArgsSchemas<[userId: z.ZodString, age: z.ZodNumber]>([ | ||
z.string().uuid(), | ||
z.number().min(18).max(150), | ||
]) | ||
.define( | ||
async ({ | ||
parsedInput: { username }, | ||
bindArgsParsedInputs: [userId, age], | ||
}) => { | ||
await new Promise((res) => setTimeout(res, 1000)); | ||
|
||
return { | ||
message: `Welcome on board, ${username}! (age = ${age}, user id = ${userId})`, | ||
}; | ||
} | ||
); |
72 changes: 72 additions & 0 deletions
72
packages/example-app/src/app/(examples)/bind-arguments/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use client"; | ||
|
||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { StyledInput } from "@/app/_components/styled-input"; | ||
import { useAction } from "next-safe-action/hooks"; | ||
import { ResultBox } from "../../_components/result-box"; | ||
import { onboardUser } from "./onboard-action"; | ||
|
||
export default function BindArguments() { | ||
const boundOnboardUser = onboardUser.bind( | ||
null, | ||
crypto.randomUUID(), | ||
Math.floor(Math.random() * 200) | ||
); | ||
|
||
const { execute, result, status, reset } = useAction(boundOnboardUser, { | ||
onSuccess(data, input, reset) { | ||
console.log("HELLO FROM ONSUCCESS", data, input); | ||
|
||
// You can reset result object by calling `reset`. | ||
// reset(); | ||
}, | ||
onError(error, input, reset) { | ||
console.log("OH NO FROM ONERROR", error, input); | ||
|
||
// 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); | ||
}, | ||
}); | ||
|
||
console.log("status:", status); | ||
|
||
return ( | ||
<main className="w-96 max-w-full px-4"> | ||
<StyledHeading>Action binding arguments</StyledHeading> | ||
<form | ||
className="flex flex-col mt-8 space-y-4" | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
const input = Object.fromEntries(formData) as { | ||
username: string; | ||
}; | ||
|
||
// Action call. | ||
execute(input); | ||
}}> | ||
<StyledInput | ||
type="text" | ||
name="username" | ||
id="username" | ||
placeholder="Username" | ||
/> | ||
<StyledButton type="submit">Onboard user</StyledButton> | ||
<StyledButton type="button" onClick={reset}> | ||
Reset | ||
</StyledButton> | ||
</form> | ||
<ResultBox result={result} status={status} /> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.