This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
10. Enum endpoint frequencies

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
What we're doing with frequencies here is a really good candidate to introduce an enum. What this will allow us to do is create a class-like thing which has predefined values in
00:09
that we can use to access the values out of our database and transform them perhaps into a label, but also just make sure we have a predefined set of values that we can use through validation to put into the database. So if we head over to our database here, the reason that this works really nicely
00:25
is because under our endpoints, obviously our frequency is just an integer. So if we weren't validating this for any reason, obviously we could have any number in here and that would allow the user to choose something, but even if we were validating, it means that we have a really loose set of values that we could potentially put in here.
00:45
If we store these in an enum, they're all going to be in one place, which makes it much easier to update and also then transform these values into labels that we can use in the UI. Now, if that doesn't make any sense, don't worry at all. We're going to go through everything that we need to set up here.
00:59
So we're going to put this enum just in an enums directory. Of course, as your app grows, it's probably a good idea to structure this in a slightly different way, but this is fine for now. And we're going to call this endpoint frequency.
01:11
And let's go ahead and set this up. So the namespace here is now under app and enums. And let's go ahead and create this enum out. So rather than say class, we're going to actually say enum.
01:21
And we're going to, of course, give this a name endpoint frequency. And we need to type in this. So we need to tell this what the values of the enum are going to be. They can either be a string or they can be an integer.
01:33
In our case, it's going to be an integer because we know we're working with second values. So all we need to do here is set up all the enum values. And this is going to be a backed enum, which means that each of these values or each of these enum cases has a value.
01:48
So I'm going to choose this standard to call them one minute, for example. And we know that one minute is 60 seconds. So we can define as many of these as we want. And just think of these as the allowed options for your endpoint frequency.
02:03
Now, to make this a little bit easier, I'm going to go ahead and multiply each of these 60 seconds by how many minutes I want this to be. This just, for me at least, makes it a little bit more readable. So, for example, five minutes is going to be five times 60 seconds.
02:18
And we can just go down and create as many of these as we want to. So let's go ahead and say 30 minutes. And, of course, make that 30 times 60. And let's go ahead and say one hour.
02:29
And as we know, that's going to be 60 times 60. So that is our enum. We're going to add a little bit more to this later. But for now, that's all we need to do.
02:38
What we want to do now is go over to the create endpoints table. And we want to set the default based on this value. We don't need to do this. But it's a good idea to, every time we touch this frequency data, always refer to our enum.
02:51
So we're going to choose the one minute value here. And we're going to update this endpoint to use this as the default value. So let's go back over to here. Of course, we're going to go ahead and roll back our migration,
03:01
because we're going to be modifying this. And inside of default, let's look at how we get the value for one minute. So let's go ahead and use our endpoint frequency enum. And we're going to go ahead and say one minute.
03:14
In fact, let's make the default five minutes. I think that makes more sense. And then we're going to grab the value from that. So what we're saying now is, from this enum, grab five minutes
03:23
and grab the actual value you want from this. So now, if we go ahead and re-migrate this, let's head over to the database, look at the schema here. And you can see that, sure enough, the default has changed to 300.
03:36
So we're making use of our enum already to add in that default. And of course, we have all of these values, which we're eventually going to iterate through, show in our form, and also use these values to validate the data that's coming in when we create our endpoint.
44 episodes4 hrs 59 mins

Overview

Ready to dive into Inertia? Let's build a real-world app with Laravel, Inertia and Vue! If you're already working with Inertia, you'll pick up some tips and techniques for future projects.

This uptime monitor allows you to create and switch between sites, then add endpoints to monitor with frequency intervals. Using the power of scheduling and queues, it'll automatically alert the email addresses you've added via the UI when an endpoint goes down.

In this course, you'll learn to:

  • Build a real app from scratch with Inertia
  • Work with Laravel queues
  • Perform actions on models at user-defined intervals
  • Work with sub-minute schedules in Laravel
  • Send out channel notifications with Laravel
  • Use API resources to simplify Inertia data
  • Organise apps with events and observers
  • Create modals in Vue
  • Perform inline editing with Inertia forms
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

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