Hone logo
Hone
Problems

Angular HttpClientModule Integration Challenge

This challenge focuses on integrating Angular's HttpClientModule to fetch data from a remote API. Understanding how to make HTTP requests is fundamental for building dynamic, data-driven Angular applications. You will demonstrate your ability to configure and utilize HttpClient to retrieve and display information from a public API.

Problem Description

Your task is to create an Angular component that fetches a list of users from a public API and displays their names and emails. This involves correctly importing and configuring HttpClientModule, injecting HttpClient into your service, making a GET request, and handling the response within your component.

Key Requirements:

  1. Module Setup: Ensure HttpClientModule is imported into your Angular application's root module (app.module.ts) or a relevant feature module.
  2. Service Implementation: Create an Angular service responsible for making the HTTP request. This service should inject the HttpClient.
  3. API Integration: Use the HttpClient to perform a GET request to the following API endpoint: https://jsonplaceholder.typicode.com/users.
  4. Data Display: Create an Angular component that subscribes to the service's method to get the user data and then displays a list of user names and their corresponding emails in the component's template.
  5. Error Handling: Implement basic error handling for the HTTP request. If the request fails, display an error message to the user.

Expected Behavior:

  • When the component loads, it should trigger a request to the specified API.
  • Upon successful retrieval, the component should render an unordered list (<ul>) where each list item (<li>) displays a user's name and email.
  • If the API request fails (e.g., network error, server error), a clear error message should be displayed instead of the user list.

Edge Cases to Consider:

  • What happens if the API returns an empty array of users? The component should gracefully handle this, perhaps by displaying a message like "No users found."
  • Consider potential network interruptions or API downtime. Your error handling should cover these scenarios.

Examples

Example 1: Successful Data Fetch

  • Input (API Response):
    [
      {
        "id": 1,
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "Sincere@april.biz"
      },
      {
        "id": 2,
        "name": "Ervin Howell",
        "username": "Antonette",
        "email": "Shanna@melissa.tv"
      }
    ]
    
  • Output (Component Template Rendering):
    <h2>User List</h2>
    <ul>
      <li>Leanne Graham - Sincere@april.biz</li>
      <li>Ervin Howell - Shanna@melissa.tv</li>
    </ul>
    
  • Explanation: The HttpClient successfully fetched data from the API. The component parsed the JSON response and rendered each user's name and email within list items.

Example 2: API Returns Empty Array

  • Input (API Response):
    []
    
  • Output (Component Template Rendering):
    <h2>User List</h2>
    <p>No users found.</p>
    
  • Explanation: The API returned an empty list. The component detected this and displayed a "No users found." message instead of an empty list.

Example 3: API Request Failure

  • Input (Simulated Network Error): (The application would receive an error object from HttpClient's catchError operator)
  • Output (Component Template Rendering):
    <h2>User List</h2>
    <p>Error fetching users: An error occurred.</p>
    
  • Explanation: The HTTP request failed. The component's error handling logic caught the error and displayed a user-friendly error message.

Constraints

  • Angular Version: Use Angular 12 or later.
  • API Endpoint: Strictly use https://jsonplaceholder.typicode.com/users. Do not mock or create your own API.
  • Data Structure: Assume the API response will be an array of objects, each with at least name and email properties.
  • Component Loading: The HTTP request should be initiated when the component is initialized (e.g., in ngOnInit).
  • Dependencies: You can use standard Angular libraries and RxJS operators.

Notes

  • Remember to import HttpClientModule in your app.module.ts (or a relevant feature module) to make HttpClient available throughout your application.
  • Consider using RxJS tap operator for debugging purposes if needed, but remove it for the final submission.
  • For error handling, you can use RxJS operators like catchError in conjunction with HttpClient.
  • Think about how you will handle the asynchronous nature of HTTP requests using Observables.
  • The goal is to demonstrate a clear understanding of Angular's HttpClient mechanics.
Loading editor...
typescript