From 70039da0c2c1c2983dc2faf33ccc065d08eee218 Mon Sep 17 00:00:00 2001 From: Marco Endres Date: Fri, 17 Feb 2023 11:37:07 +0100 Subject: [PATCH] feat(schematics): Use createFeature in feature-Schematics --- ...erize@group-reducers__.reducer.ts.template | 78 ++++++++++--------- modules/schematics/src/entity/index.spec.ts | 2 +- .../__name@dasherize__.reducer.ts.template | 32 +++++--- modules/schematics/src/reducer/index.spec.ts | 8 +- 4 files changed, 71 insertions(+), 49 deletions(-) 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..498d0b0ad5 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,43 +13,51 @@ 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) - ), -); +export const <%= pluralize(name) %>Feature = createFeature({ + name: '<%= 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) + ), + ) + extraSelectors: ({ select<%= capitalize(pluralize(name)) %>State }) => ({ + ...adapter.getSelectors(select<%= capitalize(pluralize(name)) %>State) + }), +}); export const { + name, + reducer, selectIds, selectEntities, selectAll, selectTotal, -} = adapter.getSelectors(); +} = feature; 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..689ea9413e 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'; -<% if(feature) { %>import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %> +import { Action,<% if(feature) { %> createFeature,<% } %> createReducer, on } from '@ngrx/store'; +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), -<% } %><% } %> -); +export const <%= camelize(name) %>Feature = createFeature({ + name: '<%= 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..0b6c803cc9 100644 --- a/modules/schematics/src/reducer/index.spec.ts +++ b/modules/schematics/src/reducer/index.spec.ts @@ -111,7 +111,9 @@ describe('Reducer Schematic', () => { `${projectPath}/src/app/foo.reducer.ts` ); - expect(fileContent).toMatch(/export const reducer = createReducer\(/); + expect(fileContent).toMatch(/export const fooFeature = createFeature\(/); + expect(fileContent).toMatch(/name: 'foo',/); + expect(fileContent).toMatch(/reducer: createReducer\(/); expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/); }); @@ -127,7 +129,9 @@ describe('Reducer Schematic', () => { `${projectPath}/src/app/foo.reducer.ts` ); - expect(fileContent).toMatch(/export const reducer = createReducer\(/); + expect(fileContent).toMatch(/export const fooFeature = createFeature\(/); + expect(fileContent).toMatch(/name: 'foo',/); + expect(fileContent).toMatch(/reducer: createReducer\(/); expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/); expect(fileContent).toMatch( /on\(FooActions.loadFoosSuccess, \(state, action\) => state\)/