completefrontend

Write the codeMedium

Write a tea page that fetches three things from an API in parallel and copes with failures and empty answers

Each tea's page, app/teas/[slug]/page.tsx, gets its data from the shop's HTTP API at SHOP_API, from @/lib/api: the tea at /teas/<slug>, its stock at /teas/<slug>/stock, and its reviews at /teas/<slug>/reviews. The starter fetches them one after another and trusts every answer. The page's folder has a loading.tsx and an error.tsx.

Start all three requests at once, each of them once. The tea is required: when the API answers 404 for it, call notFound(); when it answers any other error status, throw an Error with the message Couldn't load the tea: the API answered 503., with the status in it, so error.tsx shows. If the tea's request fails outright, with no answer at all, the page may fail with that error.

The stock and the reviews are extras. When either request fails in any way, an error status or no answer, keep the page and show Stock unavailable right now. or Reviews are unavailable right now. in its section.

Show the tea's name in an <h1> and its about text in a paragraph. Under a Stock heading (<h2>), show In stock: 3, or Sold out when left is 0. Under a Reviews heading, show each review as a list item, Kenji: My Sunday pot., in the API's order, or No reviews yet. when there are none.

Write this: app/teas/[slug]/page.tsx
import { SHOP_API, type Review, type Stock, type Tea } from "@/lib/api";
 
export default async function TeaPage({ params }: PageProps<"/teas/[slug]">) {
  const { slug } = await params;
  const tea: Tea = await (await fetch(`${SHOP_API}/teas/${slug}`)).json();
  const stock: Stock = await (await fetch(`${SHOP_API}/teas/${slug}/stock`)).json();
  const reviews: Review[] = await (await fetch(`${SHOP_API}/teas/${slug}/reviews`)).json();
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>{tea.about}</p>
      <h2>Stock</h2>
      <p>In stock: {stock.left}</p>
      <h2>Reviews</h2>
      <ul>
        {reviews.map((review) => (
          <li key={review.id}>
            {review.author}: {review.text}
          </li>
        ))}
      </ul>
    </main>
  );
}
lib/api.ts (read-only)
/** Where the shop's HTTP API lives. */
export const SHOP_API = "https://api.leafandkettle.example";
 
/** GET /teas/<slug>: the tea, or 404 when the shop doesn't sell it. */
export type Tea = { slug: string; name: string; about: string };
 
/** GET /teas/<slug>/stock: how many boxes are left. */
export type Stock = { left: number };
 
/** GET /teas/<slug>/reviews: the tea's reviews, newest first. */
export type Review = { id: number; author: string; text: string };
The tests your code must pass
  • shows the tea, its stock, and its reviews
  • asks for all three at once, and each only once
  • calls notFound() when the API has no such tea
  • throws for error.tsx when the tea's request fails
  • keeps the page when the stock or the reviews can't be loaded
  • says Sold out and No reviews yet. for empty answers

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…