Skip to content

Commit

Permalink
feat(store): use createFeature in feature schematics (#3776)
Browse files Browse the repository at this point in the history
Closes #3741
  • Loading branch information
faragos authored Feb 27, 2023
1 parent 6219fec commit 9b3647f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, createReducer, on } from '@ngrx/store';
import { createFeature, createReducer, on } from '@ngrx/store';
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';
Expand Down Expand Up @@ -49,9 +49,17 @@ export const reducer = createReducer(
),
);

export const <%= pluralize(name) %>Feature = createFeature({
name: <%= pluralize(name) %>FeatureKey,
reducer,
extraSelectors: ({ select<%= capitalize(pluralize(name)) %>State }) => ({
...adapter.getSelectors(select<%= capitalize(pluralize(name)) %>State)
}),
});

export const {
selectIds,
selectEntities,
selectAll,
selectTotal,
} = adapter.getSelectors();
} = <%= pluralize(name) %>Feature;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 {<% 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) %>';

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

export const reducer = createReducer(
initialState,
<% if(feature) { %>
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),
<% } %><% } %>
on(<%= classify(name) %>Actions.<%= prefix %><%= classify(name) %>sFailure, (state, action) => state),<% } %><% } %>
);
<% if(feature) { %>
export const <%= camelize(name) %>Feature = createFeature({
name: <%= camelize(name) %>FeatureKey,
reducer,
});
<% } %>
8 changes: 6 additions & 2 deletions modules/schematics/src/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: fooFeatureKey,/);
expect(fileContent).toMatch(/= createReducer\(/);
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/);
});

Expand All @@ -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: fooFeatureKey,/);
expect(fileContent).toMatch(/= createReducer\(/);
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\)/);
expect(fileContent).toMatch(
/on\(FooActions.loadFoosSuccess, \(state, action\) => state\)/
Expand Down

0 comments on commit 9b3647f

Please sign in to comment.