#Using Sonner in hooks

7 messages · Page 1 of 1 (latest)

woven crow
#

Is there a way to use Sonner notifications in a e.g. afterChange hook of a collection to notify about errors?

And can I change the depth of the resulting Doc in said hook to get relations as well?

sullen houndBOT
fickle prairie
#

You should be able to import toast from payload ui package, no need to install sonner in your project.

woven crow
#

I tried that without luck. Isn't a hook server-side and I can't use toast directly? There must be a different mechanism to inform a user about errors happening in a hook.

woven crow
#

Is it possible to make a thrown error shown to the user? Like this:

export const afterChangeHook: CollectionAfterChangeHook<Material> = async ({
  doc,
  req,
}) => {

  throw new APIError("Failed in hook");
...

But this shows just a toast saying: "Something went wrong" but not my custom message.

woven crow
#

found the solution. In the thrown Error the HttpStatus code must be a client one (4xx). Then the message shows up in the Admin UI.

export class HookAPIError extends APIError {
  constructor(message: string) {
    super(message, 400, undefined, true)
  }
}
sullen houndBOT
#

This thread is already marked as unanswered.