Project: API Dashboard
Build a complete dashboard that fetches records, renders accessible cards, filters them, and cancels stale requests.
By the end, you’ll be able to…
- Structure a small front-end project around explicit loading, empty, error, and success states.
- Fetch and validate JSON data before rendering it with safe DOM APIs.
- Abort superseded requests with AbortController and review the finished interface.
01What you are building
Build a dashboard that loads posts from the public JSONPlaceholder endpoint, renders them as cards, filters them by title, and reports loading, empty, and error states. The project uses three files: index.html for structure, styles.css for layout, and app.js for behavior. Keep the endpoint in one constant so it is easy to replace. Add a module script tag for app.js at the end of the body.
Work in small steps: create the shell, fetch once and inspect the shape, render a list, add filtering, then add retry and cancellation. Test each step in the browser before adding the next. The endpoint returns an array of objects with userId, id, title, and body; treat every field as untrusted until you check it.
<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>API Dashboard</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <main> <h1>Community posts</h1> <label for="filter">Filter by title</label> <input id="filter" type="search" placeholder="Try “async”" autocomplete="off" /> <div class="toolbar"> <p id="status" role="status" aria-live="polite">Loading posts…</p> <button id="refresh" type="button">Refresh</button> <button id="retry" type="button" hidden>Try again</button> </div> <ul id="results" aria-busy="true"></ul> </main> <!-- Load app.js as a module here. --> </body></html>A page with a heading, a filter input, a live status line, and an empty results list.
02Set up state and safe helpers
Start by collecting each element once and keeping the dashboard's data in a single state object. The object holds the loaded posts, the current filter text, and the active controller. Grouping them makes the transitions explicit: a load replaces the posts, typing replaces the query, and starting a request replaces the controller.
setStatus writes to the live region and toggles aria-busy on the list, so the page announces whether work is still in progress. isValidPost narrows an untrusted record to the fields the renderer needs. Keeping both helpers small makes the request and render functions easier to read and test.
03Fetch, validate, then store
loadPosts aborts any previous controller, creates a new one, and stores it. The try block checks response.ok, parses JSON, verifies that the result is an array, and filters out records that do not match the expected shape. Only then does it update state and render.
The catch block ignores AbortError because a newer request has replaced this one. Real failures clear the list and show a retry button. The finally block clears the stored controller only if it still belongs to this request, which prevents an older request from wiping out a newer one.
04Render cards, filter, and cancel stale work
Rendering should create elements rather than concatenate HTML strings. document.createElement plus textContent keeps record text inert, and a DocumentFragment batches the insertions into one DOM update. Every card gets a heading and a paragraph, which gives the list a readable structure for screen readers as well as sighted users.
Filtering happens in memory because all records are already loaded. The input handler updates the query and calls render again; no network request is needed. Refresh calls loadPosts, which aborts any request still in flight. This is the same stale-response problem that affects search boxes and route changes: a slow early request must never overwrite the result of a newer one.
render decides between a populated list and an empty state. It also writes the count into the live region, so assistive technology announces the change without moving focus. Avoid replacing the status element itself; update its text so the live region stays registered.
05Style the shell and review the result
A small stylesheet makes the states legible without obscuring the logic. Use a responsive grid, a readable line length, and a distinct color for errors. Keep the status line in the document flow so it does not cover content, and make sure the focus ring remains visible on the input and both buttons.
Before calling the project finished, review behavior, not just appearance. Confirm that loading, empty, error, and success states each appear, that retry and refresh work, and that aborting a request neither logs an error nor clears newer results.
Check that every fetch checks response.ok, malformed records are filtered before rendering, and all server text uses textContent. Verify keyboard access and status announcements. Finally, note the endpoint and run command in a short README.
:root { color-scheme: light dark; font-family: system-ui, sans-serif;}main { max-width: 56rem; margin: 2rem auto; padding: 0 1rem;}#results { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); list-style: none; padding: 0;}#results li { border: 1px solid color-mix(in srgb, currentColor 20%, transparent); border-radius: 0.75rem; padding: 1rem;}#status[data-state="error"] { color: #b91c1c;}A responsive card grid with a visible error color.
The ideas worth keeping.
- Model loading, success, empty, and error as explicit states before writing the request.
- Validate response records at the boundary and render them with DOM APIs rather than HTML strings.
- Abort superseded requests with a fresh AbortController and ignore AbortError deliberately.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: Add a clear-filter control
Add a button that clears the search input, resets state.query, and re-renders the full list. Keep focus on the input after clearing.
Explore a worked solution
const clearEl = document.querySelector("#clear");clearEl.addEventListener("click", () => { filterEl.value = ""; state.query = ""; render(); filterEl.focus();});The input value, the state, and the rendered list must agree. Resetting both the DOM value and the state before calling render prevents the next keystroke or filter action from using stale text, and returning focus keeps keyboard users in control.
Exercise 2: Cancel work when the page is hidden
Abort the active request when the page is hidden or unloaded, so a background tab does not keep a request alive. Use the pagehide event and the existing state.controller.
Explore a worked solution
window.addEventListener("pagehide", () => { state.controller?.abort(); state.controller = null;});pagehide fires when the page is being unloaded or moved into the back/forward cache. Aborting releases the request and prevents its handler from updating a page that is no longer visible.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Exercise 1: Add a clear-filter control
Add a button that clears the search input, resets state.query, and re-renders the full list. Keep focus on the input after clearing.
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
Pause. Put the pieces together.
You’ve reached the end of Async JavaScript & APIs. Before moving on, use this task to check what you can do independently.
Build an API dashboard that renders loading, success, empty, and error states and cancels an outdated request when the filter changes.
Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.
Review this moduleOne lesson closer.
Reading is a start. Trying it yourself makes the connection.