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

expose the input in useServerAction #155

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

IdoPesok
Copy link
Owner

@IdoPesok IdoPesok commented Jul 2, 2024

adds ability for hook to store input raw as mentioned in #124

Copy link

vercel bot commented Jul 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zsa ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 8, 2024 8:57am

Copy link

changeset-bot bot commented Jul 2, 2024

🦋 Changeset detected

Latest commit: 7d5f7c8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
zsa-react Minor
zsa Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -144,6 +144,7 @@ function MyComponent() {
- `execute`: A function to execute the server action with the provided input.
- `setOptimistic`: A function to set an optimistic update for the `data` value. It accepts either the new data directly or a function that receives the current data and returns the new data.
- `reset`: A function to reset the state of the server action to its initial values.
- `lastUsedInput`: The last input evaluated by the server action to get to the current state. This value will be the input before parsing and validation. If the input is FormData, it will be represented as a JSON object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly this solution has the same problem as described here no?

#124 (comment)

Why returning data when error is not a good idea? I would like to understand.

Traditional server techs like Rails or others validate a form and you can access old data in the fields that didn't fail.

And the important part is you always access data, in the same way, it doesn't matter if there was an error.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think we have a different definition of data. The way I define it, and the way its represented in zsa docs, is that it is the return value of a successful handler. What you returned in your PR is not data, rather it is what I define as input. Input is the value passed to the server action.

  • data: the successful return value of a server action's handler
  • input: the value sent to the server action over the network
  • error: a thrown error in a server action

So for example, if we have a simple action

export const simpleAction = createServerAction()
  .input(
    z.object({
      name: z.string(),
    })
  )
  .handler(async ({ input }) => {
    return `Hello, ${input.name}`
  })

Applied definitions:

  • data: Hello, ${name}
  • input: { name: "..." }
  • error: a thrown error in a server action

ZSA server actions must follow the following type:

(input) => [data, null] | [null, err]

I think we are getting at the same thing, but what you are looking for is what I define as input and you define as data.

Rather than doing const { data } = useServerAction(...), you will do const { input } = useServerAction(...). From the open source code you posted, I see your function doesn't have data because it is doing a redirect.

Hopefully that makes sense!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes totally sense, thanks for the detailed response!

looking forward to try this one. ping me when you have ready and I'll be the first Beta tester :)

@IdoPesok IdoPesok changed the title wip expose the input in useServerAction Jul 8, 2024
@IdoPesok
Copy link
Owner Author

IdoPesok commented Jul 8, 2024

@andresgutgon all tests are now passing here if you want to Beta test 🙏

@andresgutgon
Copy link
Contributor

all tests are now passing here if you want to Beta test

I won't work on this until the weekend probably. Feel free to merge and I'll rebase my fork and try

Thanks!

@andresgutgon
Copy link
Contributor

Hi @IdoPesok sorry for the delay 🙏
It works 🎉
image

@andresgutgon
Copy link
Contributor

@IdoPesok anything missing here? I would love to have this 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants