# M Features - Favorites

`import { ... } from '@metromobilite/m-features/favorites'`

This module provides services, a directive and more to work with favorites.

Main entry points are:

1. The favorite directive => `<button [mfFavorite]="<favoriteData>" #fav="favorite">...</button>`
  - The favorite data object is data to save in a favorite object. (eg: a poi object { properties: string, etc...})
2. The favorites handler service
  -	Allow to work with favorites according to their types. (clusters, poi, etc...)
  - An exception is thrown when an unknown type is detected.

> /!\ ATTENTION
> The handler.find method should get the right item in most cases,
> but in the case of cluster type items (due to multiple favorite items with the same data (except the line property which is different)),
> this method returns the first element with the correct data.
> You can add a line property (eg: `handler.find(data, {line: 'SEM:A'})`). But this is not always possible.

## Favorite Handlers

This module use a mechanism, base on Angular services and the multiple injection feature, to handle favorite types.

3 handlers are provided by default:

- poi (handle poi type)
- clusters (to display a line selector dialog to the user)
- unknown, throw an error.

Handlers have a weight property to determine the calls sequence. (eg: unknown must call at the end => Number.MAX_SAFE_INTEGER).
The default handlers have a large amount of weights. So you can put your handler at the right place easily.

A base classe is also provided if you want to create custom handlers.

### Implement a custom handler

my-custom-handler.favorite.ts
```typescript
import { Injectable } from '@angular/core';
import { FavoriteData, HandlerOptions } from '../favorite.model';
import { FavoriteHandlerBase } from './base.favorite';

@Injectable({ providedIn: 'root' })
export class MyCustomFavoriteHandler extends FavoriteHandlerBase {

	weight = 0;

	constructor(
		favoritesService: FavoritesService,
	) {
		super(favoritesService);
	}

	matchType(data: FavoriteData): string | null {
		// Here the logic to determine the favorite type
	}

	is(data: FavoriteData, options: HandlerOptions): boolean {
		// Here the logic to determine how to know if a data is already saved as a favorite.
	}

}

```
