-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow omitting schema argument in
schema
method (#102)
This PR adds the ability to omit passing a validation schema to the `schema` method.
- Loading branch information
Showing
11 changed files
with
203 additions
and
66 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/example-app/src/app/(examples)/empty-schema/empty-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,14 @@ | ||
"use server"; | ||
|
||
import { action } from "@/lib/safe-action"; | ||
|
||
export const emptyAction = action | ||
.metadata({ actionName: "onboardUser" }) | ||
.schema() | ||
.action(async () => { | ||
await new Promise((res) => setTimeout(res, 500)); | ||
|
||
return { | ||
message: "Well done!", | ||
}; | ||
}); |
56 changes: 56 additions & 0 deletions
56
packages/example-app/src/app/(examples)/empty-schema/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,56 @@ | ||
"use client"; | ||
|
||
import { StyledButton } from "@/app/_components/styled-button"; | ||
import { StyledHeading } from "@/app/_components/styled-heading"; | ||
import { useAction } from "next-safe-action/hooks"; | ||
import { ResultBox } from "../../_components/result-box"; | ||
import { emptyAction } from "./empty-action"; | ||
|
||
export default function EmptySchema() { | ||
const { execute, result, status, reset } = useAction(emptyAction, { | ||
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 without schema</StyledHeading> | ||
<form | ||
className="flex flex-col mt-8 space-y-4" | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
|
||
// Action call. | ||
execute(); | ||
}}> | ||
<StyledButton type="submit">Execute action</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
Oops, something went wrong.