Skip to content

Commit f6ce20f

Browse files
authored
feat(schematics): Use createActionGroup in schematics (#3791)
1 parent cdaeeb6 commit f6ce20f

File tree

10 files changed

+59
-90
lines changed

10 files changed

+59
-90
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { createAction, props } from '@ngrx/store';
1+
import { createActionGroup, emptyProps, props } from '@ngrx/store';
22

3-
export const <%= prefix %><%= classify(name) %>s = createAction(
4-
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s'
5-
);
6-
7-
<% if (api) { %>export const <%= prefix %><%= classify(name) %>sSuccess = createAction(
8-
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Success',
9-
props<{ data: unknown }>()
10-
);<% } %>
11-
12-
<% if (api) { %>export const <%= prefix %><%= classify(name) %>sFailure = createAction(
13-
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Failure',
14-
props<{ error: unknown }>()
15-
);<% } %>
3+
export const <%= classify(name) %>Actions = createActionGroup({
4+
source: '<%= classify(name) %>',
5+
events: {
6+
'<%= classify(prefix) %> <%= classify(name) %>s': emptyProps(),
7+
<% if (api) { %>'<%= classify(prefix) %> <%= classify(name) %>s Success': props<{ data: unknown }>(),<% } %>
8+
<% if (api) { %>'<%= classify(prefix) %> <%= classify(name) %>s Failure': props<{ error: unknown }>(),<% } %>
9+
}
10+
});

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ describe('Action Schematic', () => {
8282
`${projectPath}/src/app/foo.actions.ts`
8383
);
8484

85-
expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
86-
expect(fileContent).toMatch(/\[Foo\] Load Foos'/);
85+
expect(fileContent).toMatch(
86+
/export const FooActions = createActionGroup\(/
87+
);
88+
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
89+
expect(fileContent).toMatch(/Load Foos'/);
8790
});
8891

8992
it('should create success/error actions when the api flag is set', async () => {
@@ -97,10 +100,13 @@ describe('Action Schematic', () => {
97100
`${projectPath}/src/app/foo.actions.ts`
98101
);
99102

100-
expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
101-
expect(fileContent).toMatch(/\[Foo\] Load Foos Success/);
103+
expect(fileContent).toMatch(
104+
/export const FooActions = createActionGroup\(/
105+
);
106+
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
107+
expect(fileContent).toMatch(/Load Foos Success/);
102108
expect(fileContent).toMatch(/props<{ data: unknown }>\(\)/);
103-
expect(fileContent).toMatch(/\[Foo\] Load Foos Failure/);
109+
expect(fileContent).toMatch(/Load Foos Failure/);
104110
expect(fileContent).toMatch(/props<{ error: unknown }>\(\)/);
105111
});
106112

@@ -121,10 +127,11 @@ describe('Action Schematic', () => {
121127
`${projectPath}/src/app/foo.actions.ts`
122128
);
123129
expect(fileContent).toMatch(
124-
new RegExp(`export const ${prefix}Foos = createAction`)
130+
new RegExp(`export const FooActions = createActionGroup`)
125131
);
132+
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
126133
expect(fileContent).toMatch(
127-
new RegExp(`'\\[Foo] ${capitalize(prefix)} Foos'`)
134+
new RegExp(`'${capitalize(prefix)} Foos': emptyProps\\(\\),`)
128135
);
129136
}
130137
);
@@ -158,7 +165,7 @@ describe('Action Schematic', () => {
158165
);
159166

160167
expect(fileContent).toMatch(
161-
/export const loadFoosSuccess = createAction\(\r?\n?\s*'\[Foo\] Load Foos Success'\r?\n?\s*,/
168+
/'Load Foos Success': props<\{ data: unknown }>\(\),/
162169
);
163170
});
164171

@@ -176,7 +183,7 @@ describe('Action Schematic', () => {
176183
);
177184

178185
expect(fileContent).toMatch(
179-
/export const loadFoosFailure = createAction\(\r?\n?\s*'\[Foo\] Load Foos Failure'\r?\n?\s*,/
186+
/'Load Foos Failure': props<\{ error: unknown }>\(\),/
180187
);
181188
});
182189
});

modules/schematics/src/effect/files/__name@dasherize@if-flat__/__name@dasherize__.effects.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { Injectable } from '@angular/core';
22
import { Actions, <%= effectMethod %><% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
33
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';
44
import { Observable, EMPTY, of } from 'rxjs';
5-
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
5+
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
66
<% if (feature && !api) { %>import { concatMap } from 'rxjs/operators';
77
import { Observable, EMPTY } from 'rxjs';
8-
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
8+
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
99

1010
@Injectable()
1111
export class <%= classify(name) %>Effects {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ describe('Effect Schematic', () => {
279279
280280
import { concatMap } from 'rxjs/operators';
281281
import { Observable, EMPTY } from 'rxjs';
282-
import * as FooActions from '../../actions/foo/foo.actions';
282+
import { FooActions } from '../../actions/foo/foo.actions';
283283
284284
@Injectable()
285285
export class FooEffects {
@@ -313,7 +313,7 @@ describe('Effect Schematic', () => {
313313
314314
import { concatMap } from 'rxjs/operators';
315315
import { Observable, EMPTY } from 'rxjs';
316-
import * as FooActions from './foo.actions';
316+
import { FooActions } from './foo.actions';
317317
318318
@Injectable()
319319
export class FooEffects {
@@ -370,7 +370,7 @@ describe('Effect Schematic', () => {
370370
import { Actions, createEffect, ofType } from '@ngrx/effects';
371371
import { catchError, map, concatMap } from 'rxjs/operators';
372372
import { Observable, EMPTY, of } from 'rxjs';
373-
import * as FooActions from './foo.actions';
373+
import { FooActions } from './foo.actions';
374374
375375
376376
@Injectable()
@@ -409,7 +409,7 @@ describe('Effect Schematic', () => {
409409
410410
import { concatMap } from 'rxjs/operators';
411411
import { Observable, EMPTY } from 'rxjs';
412-
import * as FooActions from './foo.actions';
412+
import { FooActions } from './foo.actions';
413413
414414
@Injectable()
415415
export class FooEffects {
@@ -447,7 +447,7 @@ describe('Effect Schematic', () => {
447447
import { Actions, createEffect, ofType } from '@ngrx/effects';
448448
import { catchError, map, concatMap } from 'rxjs/operators';
449449
import { Observable, EMPTY, of } from 'rxjs';
450-
import * as FooActions from './foo.actions';
450+
import { FooActions } from './foo.actions';
451451
452452
453453
@Injectable()
@@ -470,7 +470,7 @@ describe('Effect Schematic', () => {
470470
constructor(private actions$: Actions) {}
471471
}
472472
"
473-
`);
473+
`);
474474
});
475475

476476
it('should inject the effect service correctly', async () => {
@@ -501,7 +501,7 @@ describe('Effect Schematic', () => {
501501
import { Actions, createEffect, ofType } from '@ngrx/effects';
502502
import { catchError, map, concatMap } from 'rxjs/operators';
503503
import { Observable, EMPTY, of } from 'rxjs';
504-
import * as FooActions from './foo.actions';
504+
import { FooActions } from './foo.actions';
505505
506506
507507
@Injectable()
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
1-
import { createAction, props } from '@ngrx/store';
1+
import { createActionGroup, emptyProps, props } from '@ngrx/store';
22
import { Update } from '@ngrx/entity';
33

44
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
55

6-
export const load<%= classify(name) %>s = createAction(
7-
'[<%= classify(name) %>/API] Load <%= classify(name) %>s',
8-
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
9-
);
10-
11-
export const add<%= classify(name) %> = createAction(
12-
'[<%= classify(name) %>/API] Add <%= classify(name) %>',
13-
props<{ <%= camelize(name) %>: <%= classify(name) %> }>()
14-
);
15-
16-
export const upsert<%= classify(name) %> = createAction(
17-
'[<%= classify(name) %>/API] Upsert <%= classify(name) %>',
18-
props<{ <%= camelize(name) %>: <%= classify(name) %> }>()
19-
);
20-
21-
export const add<%= classify(name) %>s = createAction(
22-
'[<%= classify(name) %>/API] Add <%= classify(name) %>s',
23-
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
24-
);
25-
26-
export const upsert<%= classify(name) %>s = createAction(
27-
'[<%= classify(name) %>/API] Upsert <%= classify(name) %>s',
28-
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
29-
);
30-
31-
export const update<%= classify(name) %> = createAction(
32-
'[<%= classify(name) %>/API] Update <%= classify(name) %>',
33-
props<{ <%= camelize(name) %>: Update<<%= classify(name) %>> }>()
34-
);
35-
36-
export const update<%= classify(name) %>s = createAction(
37-
'[<%= classify(name) %>/API] Update <%= classify(name) %>s',
38-
props<{ <%= camelize(name) %>s: Update<<%= classify(name) %>>[] }>()
39-
);
40-
41-
export const delete<%= classify(name) %> = createAction(
42-
'[<%= classify(name) %>/API] Delete <%= classify(name) %>',
43-
props<{ id: string }>()
44-
);
45-
46-
export const delete<%= classify(name) %>s = createAction(
47-
'[<%= classify(name) %>/API] Delete <%= classify(name) %>s',
48-
props<{ ids: string[] }>()
49-
);
50-
51-
export const clear<%= classify(name) %>s = createAction(
52-
'[<%= classify(name) %>/API] Clear <%= classify(name) %>s'
53-
);
6+
export const <%= classify(name) %>Actions = createActionGroup({
7+
source: '<%= classify(name) %>/API',
8+
events: {
9+
'Load <%= classify(name) %>s': props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>(),
10+
'Add <%= classify(name) %>': props<{ <%= camelize(name) %>: <%= classify(name) %> }>(),
11+
'Upsert <%= classify(name) %>': props<{ <%= camelize(name) %>: <%= classify(name) %> }>(),
12+
'Add <%= classify(name) %>s': props<{ <%= camelize(name) %>: <%= classify(name) %>[] }>(),
13+
'Upsert <%= classify(name) %>s': props<{ <%= camelize(name) %>: <%= classify(name) %>[] }>(),
14+
'Update <%= classify(name) %>': props<{ <%= camelize(name) %>: Update<<%= classify(name) %>> }>(),
15+
'Update <%= classify(name) %>s': props<{ <%= camelize(name) %>s: Update<<%= classify(name) %>>[] }>(),
16+
'Delete <%= classify(name) %>': props<{ id: string }>(),
17+
'Delete <%= classify(name) %>s': props<{ ids: string[] }>(),
18+
'Clear <%= classify(name) %>s': emptyProps(),
19+
}
20+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
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';
4-
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
4+
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
55

66
export const <%= pluralize(name) %>FeatureKey = '<%= pluralize(name) %>';
77

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ describe('Entity Schematic', () => {
215215
const fileContent = tree.readContent(
216216
`${projectPath}/src/app/foo.actions.ts`
217217
);
218-
expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
219-
expect(fileContent).toMatch(/\[Foo\/API\] Load Foos'/);
218+
expect(fileContent).toMatch(
219+
/export const FooActions = createActionGroup\(/
220+
);
221+
expect(fileContent).toMatch(/'Load Foos'/);
220222
expect(fileContent).toMatch(/props<\{ foos: Foo\[\] }>\(\)/);
221223
});
222224

@@ -230,7 +232,7 @@ describe('Entity Schematic', () => {
230232
`${projectPath}/src/app/foo.reducer.ts`
231233
);
232234
expect(fileContent).toMatch(
233-
/import \* as FooActions from '\.\/foo.actions';/
235+
/import { FooActions } from '\.\/foo.actions';/
234236
);
235237
expect(fileContent).toMatch(/on\(FooActions.addFoo,/);
236238
expect(fileContent).toMatch(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ describe('Feature Schematic', () => {
225225
expect(fileContent).toMatch(
226226
/import { Observable, EMPTY, of } from 'rxjs';/
227227
);
228-
expect(fileContent).toMatch(
229-
/import \* as FooActions from '.\/foo.actions';/
230-
);
228+
expect(fileContent).toMatch(/import { FooActions } from '.\/foo.actions';/);
231229

232230
expect(fileContent).toMatch(/export class FooEffects/);
233231
expect(fileContent).toMatch(/loadFoos\$ = createEffect\(\(\) => {/);

modules/schematics/src/reducer/files/__name@dasherize@if-flat__/__name@dasherize__.reducer.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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';
2+
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
33

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('Reducer Schematic', () => {
245245
);
246246

247247
expect(content).toMatch(
248-
/import \* as FooActions from '..\/..\/actions\/foo\/foo.actions';/
248+
/import { FooActions } from '..\/..\/actions\/foo\/foo.actions';/
249249
);
250250
});
251251

0 commit comments

Comments
 (0)