-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
Toast exception support #344
Toast exception support #344
Conversation
Try another approach to better positioning `collapse` button
# Conflicts: # src/View/Components/Main.php # stubs/app.blade.php
This is a interesting approach. I am afraid that listener can conflict in some way within existing app listeners. Mostly, people maybe using this kind of hook on “app.js” That is my case … I did exactly you have proposed on FLOW demo , but on my own App Exception and placed the hooks on “app.js”. |
@robsontenorio Would you be ok to share what your hook looks like? There may be something I can do to be flexible to that use case. |
document.addEventListener('livewire:init', () => {
Livewire.hook('request', ({fail}) => {
fail(({status, content, preventDefault}) => {
try {
content = JSON.parse(content)
if (status === 419 || content.status === 419) {
window.location.reload()
preventDefault()
}
if (content.toast) {
toast({toast: content.toast});
preventDefault()
}
} catch (e) {
console.log(e)
}
})
})
}) |
I have just tested this locally, and it runs both hooks without issue. Interestingly, it seems toast itself de-bounces the call in some way, which means if your code is in your app.js and mine remains in the toast component, both hooks are executed but only 1 toast is shown. I have added try and catch to my use case as I think that is a good idea too. |
@jedjones-uk Nice! Are you able to use the existing trait on your ToastException class instead of coding again all the toast feature? https://github.com/robsontenorio/mary/blob/main/src/Traits/Toast.php |
Unfortunately, the way the trait is written means it doesn't help with my exception use case, but I did spend some time thinking about it. One solution we could consider is: Create a 3rd DTO style class which is used to define the toast and it's data. This would provide shortcut methods and so on as we have, and then we update the trait "toast()" method and the exception method to take just the DTO and then render it how each use case makes sense. The main issue is that for decent naming you'd have some heavy breaking changes, you want the DTO to be called Your new usage might be public function livewireMethod(){
$toastDTO = Toast::error('title','description');
$this->toast($toastDTO);
//Or
$this->toast(Toast::error('title','description'));
//Or
throw new ToastException::make($toastDTO);
}
public function methodAnywhereOutsideLivewire(){
$toastDTO = Toast::error('title','description');
throw new ToastException::make($toastDTO);
//Or
throw new ToastException::make(Toast::error('title','description'));
} |
Don't worry. Let's stick to your approach! If you don't mind I appreciate if you can add a note about it on docs. Maybe on very bottom of this page. It is very easy to setup docs on localhost. |
@robsontenorio Done here: robsontenorio/mary-ui.com#31 |
Thanks ! |
Another small suggested improvement, to allow throwing standard exceptions anywhere in the stack that are output in mary as Toasts.
I have aimed to copy the defaults of the Toast as it is now, the only functionality missing is the redirects as it wouldn't make sense.
Usage is straight forward: