Resort Actions
Create a new directory src/app/state/resort, along with the file src/app/state/resort/resort.actions.ts:
mkdir src/app/state/resort
touch src/app/state/resort/resort.actions.tsOpen the resort.actions.ts file and create an enum with the following members:
- SearchResorts
- SearchResortsFail
- SearchResortsSuccess
Specify a unique string value for each enum member:
export enum ResortActionTypes {
SearchResorts = '[Resort] Search',
SearchResortsFail = '[Resort] Search Fail',
SearchResortsSuccess = '[Resort] Search Success'
}Action Classes
Create a class for each action that implements the Action interface:
import { Action } from '@ngrx/store';
export class SearchResorts implements Action {
readonly type = ResortActionTypes.SearchResorts;
constructor(public q: string) {}
}
export class SearchResortsFail implements Action {
readonly type = ResortActionTypes.SearchResortsFail;
constructor(public error: Error) {}
}
export class SearchResortsSuccess implements Action {
readonly type = ResortActionTypes.SearchResortsSuccess;
constructor(public resorts: Resort[]) {}
}- Each action declares a read-only
typeproperty. - The
SearchResortsactionconstructor()function requires the user query, which is thestringthat the user will enter in the application. - The
SearchResortsFailaction will be invoked when an error has ocurred. Theerrormust be specified when creating the action. - The
SearchResortsSuccessaction will be invoked when the HTTP request completes. An array ofResortobjects must be specified.
Type Union
Create the ResortActions type:
export type ResortActions =
| SearchResorts
| SearchResortsFail
| SearchResortsSuccess;