Hone logo
Hone
Problems

Angular Resolver: Pre-fetching Data for Smoother Navigation

Angular resolvers are a powerful feature that allows you to fetch data before a route is activated. This is incredibly useful for ensuring your components have all the necessary information readily available upon arrival, leading to a better user experience by avoiding loading spinners or blank states.

Problem Description

Your task is to implement an Angular resolver for a specific route. This resolver should fetch a list of "products" from a simulated API service and make this data available to the component associated with that route.

What needs to be achieved:

  • Create an Angular resolver that intercepts a route navigation.
  • The resolver will call a service to fetch a list of products.
  • The fetched product data should be passed to the target component.

Key requirements:

  • The resolver must be implemented as a TypeScript class implementing the Resolve interface from @angular/router.
  • You will need to create a mock ProductService that simulates fetching data asynchronously (e.g., using setTimeout and an array of product objects).
  • The route should be configured to use this resolver.
  • The target component should be able to access the data provided by the resolver.

Expected behavior:

When a user navigates to the route configured with the resolver, the resolver will execute first. It will fetch the product data. Once the data is successfully fetched, the navigation will proceed, and the product data will be available to the target component.

Edge cases to consider:

  • What happens if the ProductService fails to fetch the data (e.g., network error simulation)? For this challenge, you can assume successful data fetching. However, in a real-world scenario, you'd handle errors gracefully.

Examples

Let's assume we have a Product interface:

interface Product {
  id: number;
  name: string;
  price: number;
}

Example 1: Successful Data Fetch

Input (Simulated API Call):

A call to productService.getProducts() is made. The service returns an array of Product objects after a delay.

Output (Data available in the component):

The component routed to will receive an observable that emits the following data:

[
  { "id": 1, "name": "Laptop", "price": 1200 },
  { "id": 2, "name": "Keyboard", "price": 75 },
  { "id": 3, "name": "Mouse", "price": 25 }
]

Explanation:

The resolver successfully fetches the list of products from the ProductService. This array of products is then passed to the component.

Constraints

  • The resolver implementation should use TypeScript.
  • The ProductService should simulate asynchronous data fetching using setTimeout.
  • The product data is a simple array of objects with id, name, and price properties.
  • Focus on the core resolver implementation and data passing. Advanced error handling for the service can be omitted for this specific challenge.

Notes

  • You'll need to define your Product interface.
  • Create a mock ProductService class.
  • Implement a ProductResolver class that implements Resolve<Product[]>.
  • Configure your Angular routing module to use the ProductResolver for a specific route.
  • Consider how you will access the resolved data within your component. The ActivatedRoute snapshot or observable is your friend here.
  • Think about the return type of your resolver's resolve method. It should match the type of data you are expecting to receive in your component.
Loading editor...
typescript