Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support stateful actions via stateAction method and useStateAction hook #110

Merged
merged 48 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
20bec63
build: require Next.js 14.3.0 canary version
TheEdoRan Apr 26, 2024
c0f21a5
chore(no-release): fix code snippet on website to use `authAction` (#…
tianhuil Apr 27, 2024
d041b5f
chore: update turborepo
TheEdoRan Apr 27, 2024
f646593
chore: rename `playground` script to `pg`
TheEdoRan Apr 27, 2024
efc4fc6
style(prettier): update config
TheEdoRan Apr 28, 2024
6ed8b29
chore(playground): icon before text link
TheEdoRan Apr 28, 2024
e8c52c1
chore: remove very old readmes
TheEdoRan Apr 28, 2024
6f92c0d
chore: remove `rebuild:lib` script
TheEdoRan Apr 28, 2024
a1cc703
feat: support stateful actions via `stateAction` method
TheEdoRan Apr 28, 2024
4ff0693
refactor(types): replace internal `PrettyMerge` type with `Prettify`
TheEdoRan Apr 28, 2024
375aea1
build(turborepo): force `build:lib`
TheEdoRan Apr 28, 2024
8ab743e
refactor: remove `StateServerCodeFn` and rename `clientInput` to `inp…
TheEdoRan Apr 29, 2024
c7edaa6
refactor(validation-errors): rename `ErrorList` type to `VEList`
TheEdoRan Apr 29, 2024
a407696
refactor(validation-errors): don't export `VEList` type
TheEdoRan Apr 29, 2024
a6b4884
refactor: directly export types from index
TheEdoRan Apr 29, 2024
70971e0
chore(website): fix contributing heading
TheEdoRan Apr 29, 2024
196c2be
chore: add local test file to .gitignore
TheEdoRan Apr 29, 2024
797bfe0
style(eslint): disable `ban-ts-comment` ts rule
TheEdoRan Apr 29, 2024
123bf94
refactor: re-export `StateServerCodeFn`
TheEdoRan Apr 29, 2024
07454f3
refactor(types): remove `const` typings for all generics, except for …
TheEdoRan Apr 29, 2024
789fea5
refactor: rename `prevState` to `prevResult` in `StateServerCodeFn`
TheEdoRan Apr 29, 2024
58c94a1
refactor(hooks): update `useOptimisticAction` API
TheEdoRan Apr 29, 2024
8a77b08
build(package): set `packageManager` to pnpm
TheEdoRan Apr 30, 2024
953e614
refactor: keep data `undefined` for empty response actions
TheEdoRan Apr 30, 2024
75fd741
refactor(utils): export `EMPTY_RESULT` from utils file
TheEdoRan Apr 30, 2024
527f7e1
feat(hooks): add `useStateAction` hook for stateful actions
TheEdoRan Apr 30, 2024
3e53eb6
chore(playground): update with stateful form example
TheEdoRan Apr 30, 2024
6dee2e3
refactor: default `ctx` and `metadata` to `undefined` instead of `null`
TheEdoRan Apr 30, 2024
f387911
build: require Next.js 14.3.0-canary.32
TheEdoRan Apr 30, 2024
816a22c
build: require Next.js 14.3.0-canary.36
TheEdoRan May 2, 2024
6d14f97
chore(package): add funding section
TheEdoRan May 3, 2024
e2515a0
docs: update JSDocs
TheEdoRan May 3, 2024
ba2e714
Merge branch 'main' into state-action
TheEdoRan May 3, 2024
01f3edd
docs(hooks): update JSDocs
TheEdoRan May 3, 2024
379473e
chore(website): update existing docs
TheEdoRan May 3, 2024
9040f2f
style(prettier): disable markdown formatting
TheEdoRan May 3, 2024
4768adf
docs: update JSDocs links for hooks
TheEdoRan May 3, 2024
bf00308
chore(playground): update stateful form example
TheEdoRan May 6, 2024
6353fcb
refactor(hooks): let `useOptimisticAction` handle just optimistic data
TheEdoRan May 6, 2024
ed4fb4c
docs: update JSDocs
TheEdoRan May 6, 2024
b919e2b
chore(website): update documentation with stateful actions sections
TheEdoRan May 6, 2024
47098a3
chore(website): fix broken link in getting started page
TheEdoRan May 6, 2024
16b6a26
build: require Next.js 14.3.0-canary.40
TheEdoRan May 6, 2024
3e46780
build(package): remove `packageManager` property
TheEdoRan May 6, 2024
72d11fd
chore(website): update required Next.js version
TheEdoRan May 6, 2024
13e65b5
refactor(hooks): export `EMPTY_HOOK_RESULT`
TheEdoRan May 6, 2024
f32db2e
chore(website): fix `EMPTY_HOOK_RESULT` name
TheEdoRan May 6, 2024
f2b6d47
build: require Next.js 14.3.0-canary.42
TheEdoRan May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 66 additions & 66 deletions .github/ISSUE_TEMPLATE/1_bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,71 @@ description: Create a report to help us fix bugs.
title: "[BUG] "
labels: ["bug"]
body:
- type: checkboxes
attributes:
label: Are you using the latest version of this library?
description: Please confirm that you are using the latest version of next-safe-action.
options:
- label: I verified that the issue exists in the latest next-safe-action release
- type: checkboxes
attributes:
label: Are you using the latest version of this library?
description: Please confirm that you are using the latest version of next-safe-action.
options:
- label: I verified that the issue exists in the latest next-safe-action release
required: true
- type: checkboxes
attributes:
label: Is there an existing issue for this?
description: Please search to see if an issue already exists for the bug you encountered.
options:
- label: I have searched the existing issues and found nothing that matches
required: true
- type: textarea
attributes:
label: Describe the bug
description: A clear and concise description of what the bug is.
placeholder: I found out that the '...' functionality is not working.
validations:
required: true
- type: checkboxes
attributes:
label: Is there an existing issue for this?
description: Please search to see if an issue already exists for the bug you encountered.
options:
- label: I have searched the existing issues and found nothing that matches
- type: textarea
attributes:
label: Reproduction steps
description: Steps to reproduce the incorrect behavior.
placeholder: |
A step-by-step reproduction of the bug. For example:
1. Go to '...'
2. Click on '...'
3. Scroll down to '...'
4. See error
validations:
required: true
- type: textarea
attributes:
label: Describe the bug
description: A clear and concise description of what the bug is.
placeholder: I found out that the '...' functionality is not working.
validations:
required: true
- type: textarea
attributes:
label: Reproduction steps
description: Steps to reproduce the incorrect behavior.
placeholder: |
A step-by-step reproduction of the bug. For example:
1. Go to '...'
2. Click on '...'
3. Scroll down to '...'
4. See error
validations:
required: true
- type: textarea
attributes:
label: Expected behavior
description: A description of what you expected to happen instead.
placeholder: I expected that this happened instead.
validations:
required: true
- type: input
attributes:
label: Reproduction example
description: Link to an example that reproduces the bug. Could be, for example, a GitHub repo or a CodeSandbox link.
placeholder: https://github.com/...
validations:
required: true
- type: markdown
attributes:
value: Information about the environment you are using.
- type: input
attributes:
label: Operating System
placeholder: Windows 11, macOS, Ubuntu 22.04
validations:
required: true
- type: input
attributes:
label: Library version
placeholder: 6.0.0
validations:
required: true
- type: textarea
attributes:
label: Additional context
description: Add any other context about the problem here.
validations:
required: false
- type: textarea
attributes:
label: Expected behavior
description: A description of what you expected to happen instead.
placeholder: I expected that this happened instead.
validations:
required: true
- type: input
attributes:
label: Reproduction example
description: Link to an example that reproduces the bug. Could be, for example, a GitHub repo or a CodeSandbox link.
placeholder: https://github.com/...
validations:
required: true
- type: markdown
attributes:
value: Information about the environment you are using.
- type: input
attributes:
label: Operating System
placeholder: Windows 11, macOS, Ubuntu 22.04
validations:
required: true
- type: input
attributes:
label: Library version
placeholder: 6.0.0
validations:
required: true
- type: textarea
attributes:
label: Additional context
description: Add any other context about the problem here.
validations:
required: false
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ contact_links:
about: Do you want to help us improve the library? Open a new discussion to suggest an idea.
- name: Ask a question
url: https://github.com/TheEdoRan/next-safe-action/discussions/new?category=q-a
about: Do you want to ask something about the library? Open a new discussion to get help from the community.
about: Do you want to ask something about the library? Open a new discussion to get help from the community.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ yarn-error.log*
# Turborepo
.turbo

# Local test file
/packages/next-safe-action/src/test.ts

# Website
/website/.docusaurus
/website/node_modules
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll.eslint": "explicit"
},
"editor.rulers": [120],
"[markdown]": {
"editor.formatOnSave": false
}
}
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ After forking, cloning the repository and optionally creating a new branch from
pnpm install
```

Then, you can run the `rebuild:lib` command to rebuild the library code, and then test it in the playground app:
Then, you can run the `build:lib` command to rebuild the library code, and then test it in the playground app:

```sh
pnpm run rebuild:lib && pnpm run playground
pnpm run build:lib && pnpm run pg
```

> [!TIP]
> If you see many type errors in the playground app after running the `rebuild:lib` command, try to restart the TS Server of VS Code. This should fix the errors.
> If you see many type errors in the playground app after running the `build:lib` command, try to restart the TS Server of VS Code. This should fix the errors.

If you updated user facing APIs of the library, you're **not required**, but **highly incouraged** to update [the documentation](../website/docs) of the library to reflect the changes you've made. This can be done in later stages of the PR too, for instance when a maintainer already approved your code updates.

Expand Down
4 changes: 2 additions & 2 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@hookform/resolvers": "^3.3.4",
"@typeschema/zod": "^0.13.3",
"lucide-react": "^0.372.0",
"next": "14.2.2",
"next": "14.3.0-canary.42",
"next-safe-action": "workspace:*",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand All @@ -27,7 +27,7 @@
"@types/react-dom": "18.2.25",
"autoprefixer": "10.4.19",
"eslint": "^8.57.0",
"eslint-config-next": "14.2.2",
"eslint-config-next": "14.3.0-canary.42",
"postcss": "8.4.38",
"tailwindcss": "3.4.3",
"typescript": "^5.4.5"
Expand Down
10 changes: 5 additions & 5 deletions apps/playground/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
15 changes: 3 additions & 12 deletions apps/playground/src/app/(examples)/bind-arguments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,14 @@ export default function BindArguments() {
);

const { execute, result, status, reset } = useAction(boundOnboardUser, {
onSuccess({ data, input, reset }) {
onSuccess({ data, input }) {
console.log("HELLO FROM ONSUCCESS", data, input);

// You can reset result object by calling `reset`.
// reset();
},
onError({ error, input, reset }) {
onError({ error, input }) {
console.log("OH NO FROM ONERROR", error, input);

// You can reset result object by calling `reset`.
// reset();
},
onSettled({ result, input, reset }) {
onSettled({ result, input }) {
console.log("HELLO FROM ONSETTLED", result, input);

// You can reset result object by calling `reset`.
// reset();
},
onExecute({ input }) {
console.log("HELLO FROM ONEXECUTE", input);
Expand Down
27 changes: 0 additions & 27 deletions apps/playground/src/app/(examples)/client-form/page.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions apps/playground/src/app/(examples)/client-form/signup-action.ts

This file was deleted.

15 changes: 15 additions & 0 deletions apps/playground/src/app/(examples)/empty-response/empty-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use server";

import { action } from "@/lib/safe-action";
import { z } from "zod";

const schema = z.object({
userId: z.string().uuid(),
});

export const emptyAction = action
.metadata({ actionName: "emptyAction" })
.schema(schema)
.action(async () => {
await new Promise((res) => setTimeout(res, 500));
});
44 changes: 44 additions & 0 deletions apps/playground/src/app/(examples)/empty-response/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"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 EmptyResponse() {
const { execute, result, status, reset } = useAction(emptyAction, {
onSuccess({ data, input }) {
console.log("HELLO FROM ONSUCCESS", data, input);
},
onError({ error, input }) {
console.log("OH NO FROM ONERROR", error, input);
},
onSettled({ result, input }) {
console.log("HELLO FROM ONSETTLED", result, input);
},
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 response data</StyledHeading>
<StyledButton
type="button"
className="mt-4"
onClick={() => {
execute({ userId: crypto.randomUUID() });
}}>
Execute action
</StyledButton>
<StyledButton className="mt-4" type="button" onClick={reset}>
Reset
</StyledButton>
<ResultBox result={result} status={status} />
</main>
);
}
15 changes: 3 additions & 12 deletions apps/playground/src/app/(examples)/hook/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,14 @@ import { deleteUser } from "./deleteuser-action";
export default function Hook() {
// Safe action (`deleteUser`) and optional callbacks passed to `useAction` hook.
const { execute, result, status, reset } = useAction(deleteUser, {
onSuccess({ data, input, reset }) {
onSuccess({ data, input }) {
console.log("HELLO FROM ONSUCCESS", data, input);

// You can reset result object by calling `reset`.
// reset();
},
onError({ error, input, reset }) {
onError({ error, input }) {
console.log("OH NO FROM ONERROR", error, input);

// You can reset result object by calling `reset`.
// reset();
},
onSettled({ result, input, reset }) {
onSettled({ result, input }) {
console.log("HELLO FROM ONSETTLED", result, input);

// You can reset result object by calling `reset`.
// reset();
},
onExecute({ input }) {
console.log("HELLO FROM ONEXECUTE", input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import { action } from "@/lib/safe-action";

export const emptyAction = action
.metadata({ actionName: "onboardUser" })
.action(async (obj) => {
console.log("OBJ ->", obj);

export const noargsAction = action
.metadata({ actionName: "noargsAction" })
.action(async () => {
await new Promise((res) => setTimeout(res, 500));

return {
Expand Down
Loading