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

Action for Button with Error #1152

Open
danieldeichfuss opened this issue Feb 3, 2025 · 0 comments
Open

Action for Button with Error #1152

danieldeichfuss opened this issue Feb 3, 2025 · 0 comments
Labels

Comments

@danieldeichfuss
Copy link

danieldeichfuss commented Feb 3, 2025

Project Information

Notes Extension, using Next w/App Router

Feedback

I'm using a <Button /> with a wrapping <Action />. The action has an asynchronous action that saves something to the DB. This works great for the pending state, but I don't know how to show a failed state in case of an error. Currently it always shows success, even if I add isFailed={myErrorState} to the <Button /> component.

I tested and the only way I could find to make this work as expected would be to throw an error inside of the handler passed to the action prop. But this has the side effect of remounting the component and loosing all the changes of the user.

On a side note, we are using Next with App Router and error handling with Next server actions works a bit differently. They return an object with a state and are supposed to never throw.

Do you see an alternative here? And it would be great if the Action component documentation would get a bit more info about its behaviour.

Here is the code:

  const handleSave = async () => {
    setErrorMessage(null);
   
    // throwing inside of saveNote does not show an error state in the Button (and is against Next best practice)
    const result = await saveNote(
      {
        contextId,
        content: noteContent,
      },
      noteData?.id,
    );

    if (result.success) {
      setInteractionMode(InteractionMode.VIEW);
    } else {
      setErrorMessage(result.error.message);
      // throwing an error here works, but resets the component which is not desired
      // throw new Error("this works")
    }
  };
...

<Action action={handleSave}>
  <Button size="m" isFailed={!!errorMessage}>
    <Text>{ButtonLabels.save}</Text>
    <IconSave />
  </Button>
</Action>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant