Skip to content

Commit 9b3647f

Browse files
authored
feat(store): use createFeature in feature schematics (#3776)
Closes #3741
1 parent 6219fec commit 9b3647f

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

modules/schematics/src/entity/files/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts.template

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action, createReducer, on } from '@ngrx/store';
1+
import { createFeature, createReducer, on } from '@ngrx/store';
22
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
33
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
44
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
@@ -49,9 +49,17 @@ export const reducer = createReducer(
4949
),
5050
);
5151

52+
export const <%= pluralize(name) %>Feature = createFeature({
53+
name: <%= pluralize(name) %>FeatureKey,
54+
reducer,
55+
extraSelectors: ({ select<%= capitalize(pluralize(name)) %>State }) => ({
56+
...adapter.getSelectors(select<%= capitalize(pluralize(name)) %>State)
57+
}),
58+
});
59+
5260
export const {
5361
selectIds,
5462
selectEntities,
5563
selectAll,
5664
selectTotal,
57-
} = adapter.getSelectors();
65+
} = <%= pluralize(name) %>Feature;
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Action, createReducer, on } from '@ngrx/store';
2-
<% if(feature) { %>import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
1+
import {<% if(feature) { %> createFeature,<% } %> createReducer, on } from '@ngrx/store';
2+
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
33

44
export const <%= camelize(name) %>FeatureKey = '<%= camelize(name) %>';
55

@@ -12,10 +12,14 @@ export const initialState: State = {
1212
};
1313

1414
export const reducer = createReducer(
15-
initialState,
16-
<% if(feature) { %>
15+
initialState,<% if(feature) { %>
1716
on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>s, state => state),
1817
<% if(api) { %> on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sSuccess, (state, action) => state),
19-
on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sFailure, (state, action) => state),
20-
<% } %><% } %>
18+
on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sFailure, (state, action) => state),<% } %><% } %>
2119
);
20+
<% if(feature) { %>
21+
export const <%= camelize(name) %>Feature = createFeature({
22+
name: <%= camelize(name) %>FeatureKey,
23+
reducer,
24+
});
25+
<% } %>

modules/schematics/src/reducer/index.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ describe('Reducer Schematic', () => {
111111
`${projectPath}/src/app/foo.reducer.ts`
112112
);
113113

114-
expect(fileContent).toMatch(/export const reducer = createReducer\(/);
114+
expect(fileContent).toMatch(/export const fooFeature = createFeature\(/);
115+
expect(fileContent).toMatch(/name: fooFeatureKey,/);
116+
expect(fileContent).toMatch(/= createReducer\(/);
115117
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/);
116118
});
117119

@@ -127,7 +129,9 @@ describe('Reducer Schematic', () => {
127129
`${projectPath}/src/app/foo.reducer.ts`
128130
);
129131

130-
expect(fileContent).toMatch(/export const reducer = createReducer\(/);
132+
expect(fileContent).toMatch(/export const fooFeature = createFeature\(/);
133+
expect(fileContent).toMatch(/name: fooFeatureKey,/);
134+
expect(fileContent).toMatch(/= createReducer\(/);
131135
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/);
132136
expect(fileContent).toMatch(
133137
/on\(FooActions.loadFoosSuccess, \(state, action\) => state\)/

0 commit comments

Comments
 (0)