This episode is for members only

Sign up to access "Flexible Flash Notifications with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
06. An easy refactor for different notification types

Transcript

00:00
So there are a couple of different ways that we can handle how we're passing down the type. We can either infer this from the key that we use to flash this in the session,
00:10
or we could pass an array down here with the body and the type explicitly. Now, whichever solution you choose, it really doesn't matter. We're going to work on the easier solution in the first part here. And in the next episode, we're going to look at a slightly harder to build,
00:26
but more flexible solution going forward that might save you a little bit of time. OK, so to work on this solution, we're going to go ahead and change over the key to notification. Now, we need to update that over in handle inertia requests here. And we also need to update what we're passing through,
00:43
because now we have no way to define the type. So now I'm going to pass an array down here, and I'm going to set the type here to success. And I'm going to set the body here to that worked nicely. And that's it.
01:00
So now we have effectively an object in here by the time it arrives into our notification plugin. So let's get rid of the toast notification. And let's just console log out the notification in here and see what difference this makes. So you can see here that we've now got an object with a type in here and a body.
01:19
So what we can now do is very easily just put this directly into here. So notification dot body. And in here, we can say notification type. And it's really important at this point to check if we actually have these things available.
01:34
So we're going to create an if statement in here and just say, if notification body and notification type, then we do this. If we don't have either of them, there's no point. We don't have any data in here to work with.
01:46
So if we come over, hit do something, we get a success with that work nicely. And with this done, what we can now do over in here is change this over to error and say that failed nicely. And of course, as you'd imagine,
01:59
then two variables switch around and we get through an error with a new message. Now, I don't like the way of doing this just because it clutters up things inside of my controllers. And in this case, I'd much prefer to say success
02:15
or error or whatever we want to give inside of this key and then just provide the string of the body. Saying that, let's push ourselves a little bit and head over to the next episode and look at a slightly different way that we can do this and clear all of this stuff up.
7 episodes 28 mins

Overview

Flash notifications exist in almost every application. With Inertia, flashing notifications requires a bit more thought, and we're going to take this further and end up with a global notification plugin that just... works.

By the end of the course, you'll have the ability to flash any type of notification, anywhere in your application, without repeating any code.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.