forked from angular-redux/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
55 lines (46 loc) · 1.4 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { dispatch } from '@angular-redux/store';
import { Injectable } from '@angular/core';
import { FluxStandardAction } from 'flux-standard-action';
import { Animal, AnimalError, AnimalType } from '../model';
// Flux-standard-action gives us stronger typing of our actions.
export type Payload = Animal[] | AnimalError;
export interface MetaData {
animalType: AnimalType;
}
export type AnimalAPIAction<T extends Payload = Animal[]> = FluxStandardAction<
T,
MetaData
>;
@Injectable()
export class AnimalAPIActions {
static readonly LOAD_ANIMALS = 'LOAD_ANIMALS';
static readonly LOAD_STARTED = 'LOAD_STARTED';
static readonly LOAD_SUCCEEDED = 'LOAD_SUCCEEDED';
static readonly LOAD_FAILED = 'LOAD_FAILED';
@dispatch()
loadAnimals = (animalType: AnimalType): AnimalAPIAction => ({
type: AnimalAPIActions.LOAD_ANIMALS,
meta: { animalType },
});
loadStarted = (animalType: AnimalType): AnimalAPIAction => ({
type: AnimalAPIActions.LOAD_STARTED,
meta: { animalType },
});
loadSucceeded = (
animalType: AnimalType,
payload: Animal[],
): AnimalAPIAction<Animal[]> => ({
type: AnimalAPIActions.LOAD_SUCCEEDED,
meta: { animalType },
payload,
});
loadFailed = (
animalType: AnimalType,
error: AnimalError,
): AnimalAPIAction<AnimalError> => ({
type: AnimalAPIActions.LOAD_FAILED,
meta: { animalType },
payload: error,
error: true,
});
}