In this episode, we look at a simple way to refactor our notification system so it can handle different types like "success" or "error". First, we discuss a couple of ways you could approach passing notification types down to your components. One way is to just use the key you flash in the session; the other (and the one we implement here) is to explicitly pass both the notification body and its type in an array or object.
We update our code so that instead of just having a message, we now pass an object with a type
(like "success" or "error") and a body
(the actual message). We then update our notification plugin to read from this new object, making it easy to display different types of notifications. We also add a quick check to make sure both body
and type
actually exist before trying to use them.
With this in place, we can easily show a "success" notification or switch it to an "error" notification, just by changing the values we pass in. At the end, though, we mention that this method can get a bit messy in controllers, so in the next episode we'll explore an even cleaner, more flexible way to manage notification types!