UNPKG

loop-modules

Version:

Shared modules for the Loop product suite.

67 lines (55 loc) 1.85 kB
import { Action } from '@ngrx/store'; import { type } from '../utils/type'; const CATEGORY:string = 'LoopScenario'; /** * For each action type in an action group, make a simple * enum object for all of this group's action types. * * The 'type' utility function coerces strings into string * literal types and runs a simple check to guarantee all * action types in the application are unique. */ export interface ILoopScenarioActions { QUERY: string; TOGGLE_SELECTED: string; SET_ENTRIES: string; SET_SELECTED: string; } export const ActionTypes: ILoopScenarioActions = { QUERY: type(`[${CATEGORY}] Query`), TOGGLE_SELECTED: type(`[${CATEGORY}] Toggle Selected`), SET_ENTRIES: type(`[${CATEGORY}] Set Entries`), SET_SELECTED: type(`[${CATEGORY}] Set Selected`), }; /** * Every action is comprised of at least a type and an optional * payload. Expressing actions as classes enables powerful * type checking in reducer functions. * * See Discriminated Unions: https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions */ export class QueryAction implements Action { type = ActionTypes.QUERY; constructor(public payload: any[]) { } } export class ToggleSelectedAction implements Action { type = ActionTypes.TOGGLE_SELECTED; constructor(public payload: any) { } } export class SetEntriesAction implements Action { type = ActionTypes.SET_ENTRIES; constructor(public payload: any[] = []) { } } export class SetSelectedAction implements Action { type = ActionTypes.SET_SELECTED; constructor(public payload: any[]) { } } /** * Export a type alias of all actions in this action group * so that reducers can easily compose action types */ export type Actions = QueryAction | ToggleSelectedAction | SetEntriesAction | SetSelectedAction;