Introduction

Welcome to this lesson on adding a history tab to your AI Short Story Generator web application. In previous lessons, you set up the basic HTML structure, styled the interface, and implemented user input handling and API calls. Now, you will enhance your application by allowing users to view previously generated stories through a dedicated history tab. This feature will improve the user experience by making it easy to revisit and review past stories, and will lay the groundwork for more advanced features in the future.

Updating the HTML Structure for History Tab

To begin, let's update the index.html file to include the necessary UI elements for the history tab. This will allow users to load and view previously generated stories.

First, add a button for "Load Previous Stories." This button will trigger the function to fetch the story history. Here's how you can add it:

Next, include a paragraph element for the loading message. This message will be displayed while the stories are being fetched:

When working with web applications that make API calls, it's crucial to provide users with clear feedback about what's happening behind the scenes. Without proper loading indicators, users might wonder if their action was registered or if the application is frozen. Similarly, without proper error handling, users might be left confused when something goes wrong.

Finally, add an empty div for the story history container. This is where the fetched stories will be displayed:

By setting the correct element IDs (history-button, history-loading-message, history-container), you ensure that these elements can be easily accessed and manipulated using JavaScript.

Initializing the `fetchHistory()` Function

Now, let's create and initialize the fetchHistory() function in app/static/script.js. This function will handle the process of fetching and displaying the story history.

Start by retrieving the DOM elements for the loading message, button, and container using their IDs:

Next, update the UI to show the loading message and hide the button. This provides immediate feedback to the user that the application is processing their request:

By clearing the container's content, you ensure that any previous stories are removed before new data is loaded.

Implementing the API Call and UI Feedback

With the UI prepared, let's implement the API call to fetch the story history. We'll use the fetch API to send a GET request to the /api/get_stories endpoint.

Start by making the API call:

Next, handle the promise chain to process the response. In the first then() callback, toggle the UI back to its original state after the API call completes:

This ensures that the loading message is hidden and the button is shown again, providing a seamless user experience.

Handling API Response Data

Now, let's extend the then() callback to handle the response data. This involves checking if the returned stories array is empty and updating the history container accordingly.

First, check if the stories array is empty:

If stories are found, iterate over them, create pre elements, and append them to the container:

This approach dynamically creates and displays the story history based on the API data.

Enhancing Error Handling

To ensure robustness, let's add error handling to the fetchHistory() function. This involves adding a catch() block to handle any errors that may occur during the API call.

In the catch() block, hide the loading message, show the button, and display an alert message informing the user of the error:

By logging the error to the console, you can easily debug any issues that arise.

Summary and Preparation for Practice

In this lesson, you have learned how to enhance the AI Short Story Generator by adding loading states and error handling. You updated the HTML structure, initialized the fetchHistory() function, implemented the API call, handled response data, and enhanced error handling. These skills are crucial for providing immediate user feedback and ensuring a robust application.

As you move on to the practice exercises, apply what you've learned to test and refine your implementation. Congratulations on reaching this point in the course, and keep up the great work as you continue to build on your skills!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal