We're making some great progress on our task list, and we're ready to move on to hooking up the I'm feeling funny button. This button is a play on Google's I'm feeling lucky button, which when clicked brings the user to the first search result by default. In our case, we'll be returning a single dad joke. For the I'm feeling funny button, we're going to repurpose the existing Tell me a joke button. Let's just start by relabeling the button with I'm feeling funny. Right now if we click on the button, it calls the on tell joke method, which then calls the search jokes method.
Since we don't have a search term entered right now, it just returns the default joke list. So how do we get the I'm feeling lucky button to only return one single joke. We pop back over to the API Doc's for the dad jokes, you'll see that the search endpoint has other query string parameters available. Looking here we can see that there is a limit parameter which allows us to control the amount of results returned. Currently we are using the default which will return 20 jokes. Let's go ahead and add limit to our search call and set it equal to one.
Now when I click the I'm feeling funny button, we're only getting the one joke back. But since we hard coded the limit of one Our search form only returns one job as well, which is not what we want, we need a way to pass search chokes the limit we want to use. To do that I'm going to add a limit parameter and give it a default value of 20. This way if no limb parameters passed, we still have a valid value we can use. Now let's update the search URL to use the new limit parameter as a value. Let's give our search another try.
Okay, it looks like the search is working again. We're back to square one with our I'm feeling lucky button as is still returning multiple Joe's. Fix this we'll need to update the buttons on click handler. First, I'm going to remove the on tell joke method. Since all this method does is call the search chokes method, we can remove it and just call search jokes directly instead. Now that I've removed on tell joke, I also need to remove the binding method in the constructor as well.
Okay, now we can go ahead and add the search Joe's call to the onClick handler. In this case, though, I want to pass in a value of one for the limit parameter. I want to pass search chose parameter I can't have the onclick event call it directly. Instead I'll use a nonce arrow function which will allow me to call search chokes with parameters and in this case a limit of one. Now when I click on the I'm feeling lucky button, I get back a single joke as expected. If I submit a search, I'm still seeing multiple jokes returned as expected as well.
All right, I think we're good to cross the I'm feeling funny button off our task list. Next up creating the search form components