Managing the Blog

Flashing Ourselves/Providing Visual Feedback - Part Three

Summary

Coming soon.

Transcript

Okay, let's make our life a little bit easier and add a helper method to add flash messages to our controllers. So I'm just gonna be adding it right here saying add flash, text in the echo context, and the flash type, context, flash, context, flash type, and our message, which is just gonna be a string, and it returns us an error. We can just grab what we had from our register here, saying if we have an error, return the error. If not, then we say add flash, and the value is gonna be context, flash message, and the flash session name, and the values will be context, will be ctx, gonna be uuid new, flash type, and time.now, and message. Gonna be ctx.c, and then we return s save, ctx request, and ctx response. Alright, do we have anything? The final thing is, before we can start using it, we need to register this type here. So just as we register the uuid, we need to register context flash message. Now we can go into our controllers, and let's start by the articles create here, and let's just begin by saying that if we have any error, we have some visual feedback, but we can add a flash saying if error, add flash, ctx, and say context, flash, flash warning, flash error, probably flash error, with a message, could not, or yeah, could not pass validation, probably did not pass validation, error does not equal nil, then we're just gonna log it, because we don't really wanna, again, stop the app. So again, ctx request could not add a flash error, error, there we go. Now let's grab all of this, and in the case that we don't have an error, we're gonna say flash success, successfully created article, created article. Alright, now we just need to do this for our update, again, let's path one here, and one here, so we have the flash error, and probably we are just gonna be showing the same here, did not pass validation, for this one, and here we're just gonna say successfully updated article, because the rest of the spots is gonna be, we show an error page instead of a flash message, so the final place we have is when we call delete, here we are gonna say successfully deleted article. Right, now with all of this in place, we can go ahead and save, jump into the browser, give it a refresh, let's just try and update an article here, so let's say this is released, submit, and we don't see anything, so let's see what go wrong here, type echo context is not, so I created the flash message in a wrong way, so let's just see in our controller here, where we went wrong, and we have the add flash, which is gonna take in the echo context, that's correct, and we just want to say this should do the trick, let me give it a refresh, and let's see, just unrelease it, submit, we don't have an error, do we? Submit again, let's try submit, now, there we go, so we say submit, but since we are not redirecting now, we don't get it until the refresh, so we can see here, successfully updated the article, and if we click delete, successfully deleted the article, let's create one, test with flash, and norm ipsum, again here, 20, submit, successfully created article on the update page, so let's just fix when we have the, on the update, where did we have the update, we can actually steal this from the create, alright, so we no longer return all of this, we just return no, and we have the article, we have the updated article that we can pass, so we're not really utilizing, we are actually just doing a full page refresh here, but that's fine, that's fine for this case, this is how flash messages are supposed to work, so give that a refresh, let's say the read time is only 12 now, and submit, and now successfully updated the article, so now we can mark articles as released and not released, and if we go in here and say, see we have two released, so if we go to our landing page, you can see we have a bunch here that are not marked as released, but are still showing, so the way that we fix that is if we go to our, find our home controller here, get latest articles, and this one doesn't really care if they are released or not, so let's say get latest released articles, and the reason why we're just going to create another model is because this dynamic queries for SQL C is a little bit difficult to work with, we could use something like Squirrel to do a builder, so we could accept arguments, but for now I find it easier just to go in and create a new one, because we will literally just say, where do we have it, this one, so query articles released from articles where, let's just say draft, where draft equals false, order by descending, so that is the same, SQL C generates, and then in models for article, we can now say query, query articles released, it's going to be the same, back in our controller, we're now just going to be using get latest released articles, so now let's go in here and see, we still have all of them, so we should probably have only the ones that has a released ad, but it also indicates that we are not setting the draft correctly, so where draft equals false, so we actually need to fix an error here, because whenever we go in and say new article, draft payload is going to be draft, all right, let's see where we use it here in controller, and release should probably just be release, right, true false, yes, that is correct, then in our update, we are going to be passing draft again, so update article in controller, let's figure out boolean values, all right, let's jump back into this one, go back into our dashboard, and let's again try to redo this flow, now we should have test with flash not showing at least, we do not, and we need to make all the other ones to true, so let me just quickly fix that, all right, so I updated all the values that has no release date to be draft true, go in here and refresh, you can see we now only have two, dashboard, we only have these two right here, and we have article one with released, so let me just delete that, we did a bit of messing up the data, all right, dashboard, we also have these two, let me just delete them, and then see if we can make this work, so now we have no, that's right, dashboard, article 10 here, let's make it released, submit, go home and see it is released, now hopefully it shows up, it does not, that is great, so what am I doing wrong here, so draft, it should actually be this one, right, it had to be, this was the update, yeah, and we have to create it, also the reverse of release, that should have been correct, let me go ahead and delete this one, so let's change it now, submit, and how we are working now, that's good, yeah, all right, let's test another one here, test, release works, associate it, and we're just gonna say lorem ipsum, ipsum, ipsum, 12 minutes, release, submit, successfully created, hopefully now we have two, we have two articles, all right, so now we can manage our block, we can update it, we can release article, we can go in and say, like article 9 or article test release work should have a different title, integration testing with go, all right, submit that, and then if we go to our landing page, we can see integration testing with go, so now we have a fully functioning dashboard that we can use to manage our articles, and that's awesome, however, next step is we want to have subscribers, so the next module is going to be all about how we can add subscribers and validate subscribers, validate that we only have one with valid emails, and then after that we are going to implementing newsletters, so we can actually send updates to all of our subscribers.

Early Access

$95 $65 USD

Get access

Invoices and receipts available for easy company reimbursement