diff --git a/modules/schematics/src/entity/files/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts.template b/modules/schematics/src/entity/files/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts.template index 7439f68efd..bb9efecd98 100644 --- a/modules/schematics/src/entity/files/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts.template +++ b/modules/schematics/src/entity/files/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts.template @@ -3,8 +3,6 @@ import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity'; import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model'; import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions'; -export const <%= pluralize(name) %>FeatureKey = '<%= pluralize(name) %>'; - export interface State extends EntityState<<%= classify(name) %>> { // additional entities state properties } @@ -15,39 +13,47 @@ export const initialState: State = adapter.getInitialState({ // additional entity state properties }); -export const reducer = createReducer( - initialState, - on(<%= classify(name) %>Actions.add<%= classify(name) %>, - (state, action) => adapter.addOne(action.<%= camelize(name) %>, state) - ), - on(<%= classify(name) %>Actions.upsert<%= classify(name) %>, - (state, action) => adapter.upsertOne(action.<%= camelize(name) %>, state) - ), - on(<%= classify(name) %>Actions.add<%= classify(name) %>s, - (state, action) => adapter.addMany(action.<%= camelize(name) %>s, state) - ), - on(<%= classify(name) %>Actions.upsert<%= classify(name) %>s, - (state, action) => adapter.upsertMany(action.<%= camelize(name) %>s, state) - ), - on(<%= classify(name) %>Actions.update<%= classify(name) %>, - (state, action) => adapter.updateOne(action.<%= camelize(name) %>, state) - ), - on(<%= classify(name) %>Actions.update<%= classify(name) %>s, - (state, action) => adapter.updateMany(action.<%= camelize(name) %>s, state) - ), - on(<%= classify(name) %>Actions.delete<%= classify(name) %>, - (state, action) => adapter.removeOne(action.id, state) - ), - on(<%= classify(name) %>Actions.delete<%= classify(name) %>s, - (state, action) => adapter.removeMany(action.ids, state) - ), - on(<%= classify(name) %>Actions.load<%= classify(name) %>s, - (state, action) => adapter.setAll(action.<%= camelize(name) %>s, state) - ), - on(<%= classify(name) %>Actions.clear<%= classify(name) %>s, - state => adapter.removeAll(state) - ), -); +const feature = createFeature({ + '<%= pluralize(name) %>', + reducer: createReducer( + initialState, + on(<%= classify(name) %>Actions.add<%= classify(name) %>, + (state, action) => adapter.addOne(action.<%= camelize(name) %>, state) + ), + on(<%= classify(name) %>Actions.upsert<%= classify(name) %>, + (state, action) => adapter.upsertOne(action.<%= camelize(name) %>, state) + ), + on(<%= classify(name) %>Actions.add<%= classify(name) %>s, + (state, action) => adapter.addMany(action.<%= camelize(name) %>s, state) + ), + on(<%= classify(name) %>Actions.upsert<%= classify(name) %>s, + (state, action) => adapter.upsertMany(action.<%= camelize(name) %>s, state) + ), + on(<%= classify(name) %>Actions.update<%= classify(name) %>, + (state, action) => adapter.updateOne(action.<%= camelize(name) %>, state) + ), + on(<%= classify(name) %>Actions.update<%= classify(name) %>s, + (state, action) => adapter.updateMany(action.<%= camelize(name) %>s, state) + ), + on(<%= classify(name) %>Actions.delete<%= classify(name) %>, + (state, action) => adapter.removeOne(action.id, state) + ), + on(<%= classify(name) %>Actions.delete<%= classify(name) %>s, + (state, action) => adapter.removeMany(action.ids, state) + ), + on(<%= classify(name) %>Actions.load<%= classify(name) %>s, + (state, action) => adapter.setAll(action.<%= camelize(name) %>s, state) + ), + on(<%= classify(name) %>Actions.clear<%= classify(name) %>s, + state => adapter.removeAll(state) + ), + ) +}); + +export const { + name, + reducer, +} = feature; export const { selectIds, diff --git a/modules/schematics/src/entity/index.spec.ts b/modules/schematics/src/entity/index.spec.ts index ce530f7fc1..9fbc3ad722 100644 --- a/modules/schematics/src/entity/index.spec.ts +++ b/modules/schematics/src/entity/index.spec.ts @@ -211,7 +211,7 @@ describe('Entity Schematic', () => { `${projectPath}/src/app/foo.reducer.ts` ); - expect(fileContent).toMatch(/foosFeatureKey = 'foos'/); + expect(fileContent).toMatch(/'foos',/); }); it('should create a const for the action creator', async () => { diff --git a/modules/schematics/src/reducer/files/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts.template b/modules/schematics/src/reducer/files/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts.template index f9055a82d8..2a8fe0f27c 100644 --- a/modules/schematics/src/reducer/files/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts.template +++ b/modules/schematics/src/reducer/files/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts.template @@ -1,7 +1,9 @@ -import { Action, createReducer, on } from '@ngrx/store'; +import { Action,<% if(feature) { %> createFeature,<% } %> createReducer, on } from '@ngrx/store'; <% if(feature) { %>import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %> -export const <%= camelize(name) %>FeatureKey = '<%= camelize(name) %>'; +<% if(!feature) { %> + export const <%= camelize(name) %>FeatureKey = '<%= camelize(name) %>'; +<% } %> export interface State { @@ -11,11 +13,21 @@ export const initialState: State = { }; -export const reducer = createReducer( - initialState, <% if(feature) { %> - on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>s, state => state), -<% if(api) { %> on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sSuccess, (state, action) => state), - on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sFailure, (state, action) => state), -<% } %><% } %> -); +const feature = createFeature({ + '<%= camelize(name) %>', + reducer: <% } else {%>export const reducer = <% } %>createReducer( + initialState, + on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>s, state => state), + <% if(api) { %> on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sSuccess, (state, action) => state), + on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sFailure, (state, action) => state), + <% } %> + )<% if(feature) { %>, +});<% } else {%>;<% } %> + +<% if(feature) { %> + export const { + name, + reducer, + } = feature; +<% } %> diff --git a/modules/schematics/src/reducer/index.spec.ts b/modules/schematics/src/reducer/index.spec.ts index edfdc13dba..a60acb950b 100644 --- a/modules/schematics/src/reducer/index.spec.ts +++ b/modules/schematics/src/reducer/index.spec.ts @@ -111,7 +111,8 @@ describe('Reducer Schematic', () => { `${projectPath}/src/app/foo.reducer.ts` ); - expect(fileContent).toMatch(/export const reducer = createReducer\(/); + expect(fileContent).toMatch(/const feature = createFeature\(/); + expect(fileContent).toMatch(/reducer: createReducer\(/); expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/); }); @@ -127,7 +128,8 @@ describe('Reducer Schematic', () => { `${projectPath}/src/app/foo.reducer.ts` ); - expect(fileContent).toMatch(/export const reducer = createReducer\(/); + expect(fileContent).toMatch(/const feature = createFeature\(/); + expect(fileContent).toMatch(/reducer: createReducer\(/); expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/); expect(fileContent).toMatch( /on\(FooActions.loadFoosSuccess, \(state, action\) => state\)/