Ria Choi
← Portfolio

Movie Search Page

A React movie discovery page focused on TMDB integration, light poster loading, and optional Appwrite analytics.

Movie Search Page

I wanted browsing and searching to feel like one experience even though the data came from an external API. I focused on how the app loads the catalog and images, how it communicates request state, and how to record useful searches without making analytics a condition for displaying a result.

Using one result view for discovery and search

With an empty query, React requests popular titles from TMDB Discover. With a query, it calls TMDB Search and delivers the response to the same movie grid. The request clears the previous error at the start, checks the HTTP status, and switches off the loading state after success or failure.

JavaScript
const fetchMovies = async (query = "") => {
  setIsLoading(true);
  setErrorMessage("");

  try {
    const endpoint = query
      ? `${API_BASE_URL}/search/movie?query=${encodeURI(query)}`
      : `${API_BASE_URL}/discover/movie?sort_by=popularity.desc`;

    const response = await fetch(endpoint, API_OPTIONS);

    if (!response.ok) {
      throw new Error("Failed to fetch movies");
    }

    const data = await response.json();

    if (data.Response === "False") {
      setErrorMessage(data.Error || "Failed to fetch movies");
      setMovieList([]);
      return;
    }

    setMovieList(data.results || []);

    if (query && data.results.length > 0) {
      await updateSearchCount(query, data.results[0]);
    }
  } catch (error) {
    console.error(`Error fetching movies: ${error}`);
    setErrorMessage("Error fetching movies. Please try again later.");
  } finally {
    setIsLoading(false);
  }
};

Reducing image work in a poster-heavy page

Cards request a 500-pixel poster rendition rather than the original asset. Native lazy loading defers offscreen requests. A placeholder covers missing poster data or an image that fails to load, so the result card remains readable.

JSX
<div className="movie-card">
  <img
    src={
      poster_path
        ? `https://image.tmdb.org/t/p/w500/${poster_path}`
        : "/noMovieWide.png"
    }
    alt={title}
    loading="lazy"
    onError={(event) => {
      event.currentTarget.onerror = null;
      event.currentTarget.src = "/noMovieWide.png";
    }}
  />

Keeping analytics isolated

The Appwrite helper normalizes search terms, looks up one matching row, and increments its counter. When a simultaneous request creates the row first, the helper reuses that row. Missing Appwrite configuration skips tracking, and tracking errors are handled inside the helper.

What I learned

For this page, optimization meant loading the content that the visitor could see first and making optional analytics safe to skip. The fetch lifecycle and image-loading policy were two separate places to make the page feel more responsive.

Related article

Modern Data Fetching in React