Skip to content

Commit

Permalink
feat(schematics): Use createFeature in feature-Schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Endres committed Feb 17, 2023
1 parent 8769b15 commit 70039da
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
2 changes: 1 addition & 1 deletion modules/schematics/src/entity/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

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

0 comments on commit 70039da

Please sign in to comment.