This repository has been archived by the owner on Mar 25, 2023. It is now read-only.
/
roles.reducers.ts
95 lines (82 loc) · 2.59 KB
/
roles.reducers.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import * as event from './roles.actions';
import { Role } from '../../../shared/models/role.model';
/**
* @ngrx/entity provides a predefined interface for handling
* a structured dictionary of records. This interface
* includes an array of ids, and a dictionary of the provided
* model type by id. This interface is extended to include
* any additional interface properties.
*/
export interface State extends EntityState<Role> {
loading: boolean;
}
export interface RolesState {
list: State;
}
export const roleReducers = {
list: reducer,
};
/**
* createEntityAdapter creates many an object of helper
* functions for single or multiple operations
* against the dictionary of records. The configuration
* object takes a record id selector function and
* a sortComparer option which is set to a compare
* function if the records are to be sorted.
*/
export const adapter: EntityAdapter<Role> = createEntityAdapter<Role>({
selectId: (item: Role) => item.id,
sortComparer: false,
});
/** getInitialState returns the default initial state
* for the generated entity state. Initial state
* additional properties can also be defined.
*/
export const initialState: State = adapter.getInitialState({
loading: false,
});
export function reducer(state = initialState, action: event.Actions): State {
switch (action.type) {
case event.LOAD_ROLES_REQUEST: {
return {
...state,
loading: true,
};
}
case event.LOAD_ROLES_RESPONSE: {
const roles = action.payload;
return {
/**
* The addMany function provided by the created adapter
* adds many records to the entity dictionary
* and returns a new state including those records. If
* the collection is to be sorted, the adapter will
* sort each record upon entry into the sorted array.
*/
...adapter.addAll(roles, state),
loading: false,
};
}
default: {
return state;
}
}
}
export const getRolesState = createFeatureSelector<RolesState>('roles');
export const getRolesEntitiesState = createSelector(
getRolesState,
state => state.list,
);
export const { selectIds, selectEntities, selectAll, selectTotal } = adapter.getSelectors(
getRolesEntitiesState,
);
export const isLoading = createSelector(
getRolesEntitiesState,
state => state.loading,
);
export const roleTypes = createSelector(
selectAll,
roles => Array.from(new Set(roles.map(role => role.type))),
);