Managing the Blog

Flashing Ourselves/Providing Visual Feedback - Part One

Summary

Coming soon.

Transcript

So we can now manage our block. However, we don't have the visual feedback whenever we do an action like creating, updating, and deleting. So the final episode of this module is going to be doing exactly that, adding these flash messages to our web app, our dashboard, so that we can see if something succeeded or something failed. And the way to do this is actually going to be very straightforward. We need to do very similar to what we did with our app context. So we can go into routes, context, and instead of going into one of the existing ones, let's create a new one here called toast.go, package context. And here we will do very similar to what we had in here. So I can actually just copy-paste it. And then we are going to say flash key. Let's just say flash key here. And we're going to be having another type here called flash type string. There's going to be a bunch of consts. There's going to be flash success. There's a flash type equal to success. We have three more. We have error. It's going to be error. Warning. Warning. Finally, we have info. Info. Great. And then the actual flash message. We're going to still be having an echo context. Let's just also give it an ID. UI ID. Type of flash type. Create it at time.time. And message is going to be a string. There we go. Then we can go into our controllers and we need to add another middleware. So just as we had this register app context, we need one to register the flash messages so that we check every time our request is made. So register flash message context. And we actually need to skip this if the request will path as slash static in it. So let's say strings. Hash prefix. And the C request URL path. Static. So if this is the case, then just return next. There we go. Just return next. If not, then we're going to get flash session. Say flash. Oh, what did I do? Flash session name. Let's add this to our const here. So flash session. All right. Let me pull out all of our flash messages from the session storage. Then we can remove this and say flash messages equals to context. Flash message or slice of context flash messages. And then say if flashes. S dot flashes. And we have the flash session name. And if len flashes, so if we actually have any flashes, then we are going to be saying for flash in range flashes. And we need to convert it to a context flash message. If flashes and then we have like this. Right. Right. Let me just remove this and remove this. All right. So we have if we have any flashes. Right. And like this. And of course, it's going to be a slice of flash messages that it's complaining about. So what are we doing wrong here? Of course, we loop over the flashes and then we say if message OK. Flash context. Flash message. And OK. Then we can add the message to our flash messages slice. So append. And this is a little fiddly, but get there. ID. Type. Message created at. Finally, message message with that one. Right. This result is never used. It will be used in just a second. We then say if error is safe, because now we have pulled out all of the all of the flash messages from the context and we don't want to them to exist after this after this request. So response. Error does not equal nil. I don't really want to block anything here. So we're just going to return it. We could say slug error context here. Pass the context. Say could not save session or flash session. Error being error. At least we have some some notification. Right. Finally, set the flash key string to the flash messages. And then we're just going to call return xc. Final thing. We do this whole registering the we register the app context on on our on our request cycle. Right. So we also need to do that with with the flash messages. So we can say app ctx ctx context with value. And in here we're going to say ctx request context as the parent and then app ctx for key and then app ctx for the app context. All right. Let me say flash ctx key. Context flash key. And then flash ctx. ctx get flash ctx key and string. Finally, we can say here. Return. Let's get rid of that one. And we're going to have the parent context be with ctx. Then flash ctx key. Finally, flash. So now we must register in routes our new middleware. Register flash message context. So it gets called. So again, this is just the order that our middleware gets called in. The session new cookie store creation first and the app context and then finally the flash message context.

Early Access

$95 $65 USD

Get access

Invoices and receipts available for easy company reimbursement