In this episode, we tackle the part of our booking app where we fetch the available appointment slots for a selected date, service, and employee.
We start by setting up a new controller and a route to handle slot-fetching requests from the frontend. We make sure to send along the selected employee, service, and the date we're interested in. The backend uses our slot availability calculator (which we already have from earlier) to figure out which times are open for that specific day and returns those times in a nicely formatted JSON array.
On the frontend, we use Axios to make HTTP requests to the new slots route when the date changes or the page loads. We spend a little time tidying up our data handling: making sure dates are sent and received in the right format, and that our slot data is an array of times rather than a keyed object. We also rework our component state and event handlers so whenever you pick a date in the calendar, we fetch and store the new slots correctly. Along the way there are some small hiccups around date formatting, so we add logic to standardize our date formats and make sure everything stays in sync.
By the end of this episode, our frontend is successfully making requests as soon as you pick a date (or load the page) and storing the available slots in the right place. In the next episode, we'll work on displaying these times and styling them up in the UI, completing the slot selection flow!