So this time we want to make this button do something more than just show these dates.
And as you may have noticed, we can still select dates in the past here.
So we're going to we're going to want to fix that, too.
And that isn't too difficult.
So let's go look at our code and see what we're doing at the moment.
So in my roots right now, if I want to check availability and get the response as Jason, which is
what we're doing on that page, here's my handler.
It's availability, Jason.
So I bring this up and I look at it and it says it's just constructing a response, which is this Jason
response type right here.
So it's going to hand back Jason that consists of OK and possibly a message, and I'm giving it false
and available right now.
And that's not useful.
And if you look at the page Generals' page and this is the template for it right now, first thing I'm
going to do is find that date range picture, which is here somewhere.
They're the stereotypical.
I will also give this the mandate and that will take care of the ability to select dates in the past.
So that's fixed.
Now, what we're doing here is on this callback function.
This is where we're calling our Jason our our our search availability Jason path.
So we're calling that handler I was just looking at and what it's passing, it is all of the data in
a form with the name check check availability form.
Well, do I have enough information in that form?
If you recall, we set that up right here.
This is a JavaScript string that says document.
Guille gets the element by ID.
When we click on that button, it then creates this HTML and that's what we use in our R pop up dialogue.
And that is a form with the idea of reservation dates model.
And the only thing in that is start date an ending and there's more information that we need.
So back down here, we're actually passing in the CSF token manually.
The start date in the end date get put into our request just by this.
This let formed equals new form data and then it gets all of the elements in that form.
We also need the remedy and right now we're on generals' quarters.
So let's add another bit of data to this form data dot append.
And we're going to call this remedy because we need that to be able to check availability for room.
And the idea is one and I have to put that in as a string because that's what you put in form requests.
It has to go in the form of a string.
So now I have this information and I can pass it.
And over in this handler, I need to get that information into a format I can work with.
So I'm going to do it right between these things.
This is going to disappear in a moment.
But I'll put what I need in here.
And what I definitely need in here is the start date, which I'll get is a string to start with our
form douget and we called it start in that form.
We also need the end date as a string, which is coming in as end, and we also need the remedy.
And we can go room ID and I'll just ignore the air right now is equal to Sterkel.
Awide ata form doget, and we're calling that room Viddy, and I need to convert these from a string
format into the time time format because I'm going to call that database function that allows us to
search for availability by room for a given date range.
So you remember how to do that.
You define a layout and that's just a string and it's in the form of a four digit year and a two digit
month and a one day to day to day.
And then we just convert our SD and add into time time.
And we do that by saying start date and potentially an error, which I'm going to ignore right now,
is equal to time pass layout and then start date.
And I can do the same thing for Andy.
As long as I change this to.
All right, so now I have my start date, my end date and my remedy, and that's everything I need to
call my database function so I can say I'm going to start in the boolean variable available and it's
a boolean because that's what my my database function gives back to me.
And I'll ignore the air right now is equal to or assign the value of IMDB dot search availability dates
by Brumidi.
And that takes my start date, my end date and my Romandie.
So this variable is now set to true if things are available and it's set to false if things are not
available.
So what I can do is just take this response here, cut it out of this position and put it down into
this position and change this fault's to available.
I don't reply to available available.
I don't really care about the message because I'm not going to use it.
So I'll just leave that as an empty string for now.
I could just leave it out entirely and it would default to an empty string.
But this is what I'm going to do.
Now, you may have noticed I'm ignoring an error here and I'm ignoring an error here and I'm ignoring
an error here and I'm ignoring an error here.
And that's not a good thing.
I probably should do more than that.
But in this situation where I know I'm sending JavaScript of a particular format, I'm less concerned
about it.
Honestly, if I was going into production, I would write the error handling code.
But for the purposes of this lecture, you already know how to handle errors.
So I'm not going to worry about that right now.
I'll leave that as an exercise for you.
So I have this all set up now and it seems that I'm getting exactly what I want.
So let's stop, stop and start our application.
Clear run.
Let's go back to our Web browser, reload this page to make sure everything is current and make sure
that it works the way it wants it's supposed to.
OK, so right now that's I can't select dates in the past.
I'll check for a date of, say, the 25th to the 26th and what's going to happen?
Not much, because we didn't actually put anything in our page to determine to demonstrate whether we're
getting a value back or not.
So let's go back to our page and down here where I'm actually calling my handler, that returns, Jason.
I'm posting it.
That's great.
It's got all the phone data.
That's great.
Then it takes our response and convert it to Jason and then I can do stuff with it.
So what I'm going to do here is just change this.
I'll get rid of the console log data and I'll say if DataDot OK, so if that's true, the console log
room is available.
ELT's.
Console dialogue room is not available.
All right, so let's try this, I don't need to restart my application because I'm actually rebuilding
the page on every request in development mode and I'll open my terminal, my console, my JavaScript
console, and I will check availability.
So I'm going to look for the second to the third and search.
And it says room is not available.
Let's check availability again.
Let's try something that should be available July, July 7th to July 8th.
OK, room is available.
It seems to be working exactly the way that I want.
And you can verify that.
And I recommend you do so by going to your database, looking at your room restrictions table, finding
dates that, you know, that room is not available and searching on that and getting the correct response
and then doing the same thing, but searching for dates, you know, are available and making sure you
get the right response.
And when we actually update our tests, we'll be doing this right in our tests to automate that process.
But as a quick smoke test, as we sometimes call it, it's not at all unhelpful to test it that way.
So what we need to do next is to actually, once the room is available to display useful information
that says this room is available with the button that allows them to click to go make a reservation
or to display an error message saying sorry, the selected dates are not available.
Try another search and we'll do that in the next lecture.