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

Have the "type" property accept a function that receives data when using toast.promise #819

Open
grazianodev opened this issue Aug 6, 2022 · 1 comment · May be fixed by #851
Open

Have the "type" property accept a function that receives data when using toast.promise #819

grazianodev opened this issue Aug 6, 2022 · 1 comment · May be fixed by #851

Comments

@grazianodev
Copy link

Do you want to request a feature or report a bug?

I want to request a feature

What is the current behavior?

Currently, when using toast.promise you can only set a toast's type as a string, eg.:

toast.promise( fetcher, {
   success: {
      render: ( { data } ) => "Post ${data.title} was successfully created!",
      type: "success"
   },
   error: {
      render: "Something went wrong!",
      type: "error"
   },
})

What is the expected behavior?

It would be nice if type accepted a function that receives the data (just like render), and determine the type based on that data. I'm asking because eg. when requesting data with GraphQL, the request may be successful (ie. the fetcher doesn't throw an error, so the error toast is not used), but the successful response may still contain errors, so I would basically turn the success toast into an error toast, something like this:

toast.promise( fetcher, {
   success: {
      render: ( { data: result } ) => {
         const { data, errors } = result
         return errors ? "Something went wrong" : "Post ${data.title} was successfully created!", 
      },
      type: ( { data: result } ) => {
         const { data, errors } = result
         return errors ? "error" : "success"
      },
   },
   error: {
      render: "Something went wrong!",
      type: "error"
   },
})

I am currently working around this by using a try/catch block and toast.update as explained in #726, but I like the toast.promise signature better :)

@alexandredev3
Copy link

Such a feature would be nice. Do you know if anyone is working on this? If not, I can give it a try myself!

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

Successfully merging a pull request may close this issue.

3 participants