Skip to content

Commit 2624963

Browse files
author
Peter Smith
committed
wip ngrx
1 parent 532b937 commit 2624963

File tree

11 files changed

+13267
-25
lines changed

11 files changed

+13267
-25
lines changed
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {
2-
chain,
3-
externalSchematic,
4-
Rule,
5-
apply,
6-
url,
7-
template,
8-
move,
1+
import {
2+
chain,
3+
externalSchematic,
4+
Rule,
5+
apply,
6+
url,
7+
template,
8+
move,
99
mergeWith,
1010
noop,
1111
} from '@angular-devkit/schematics';
@@ -14,13 +14,18 @@ import { strings } from '@angular-devkit/core';
1414
import { DomainOptions } from './schema';
1515
import { addDomainToLintingRules } from '../utils/update-linting-rules';
1616

17-
export default function(options: DomainOptions): Rule {
17+
export default function (options: DomainOptions): Rule {
1818
const libFolder = strings.dasherize(options.name);
1919

20-
const templateSource = apply(url('./files'), [
21-
template({}),
22-
move(`libs/${libFolder}/domain/src/lib`)
23-
]);
20+
const templateSource = options.ngrx
21+
? apply(url('./ngrx-files'), [
22+
template({}),
23+
move(`libs/${libFolder}/domain/src/lib`),
24+
])
25+
: apply(url('./files'), [
26+
template({}),
27+
move(`libs/${libFolder}/domain/src/lib`),
28+
]);
2429

2530
return chain([
2631
externalSchematic('@nrwl/angular', 'lib', {
@@ -34,12 +39,12 @@ export default function(options: DomainOptions): Rule {
3439
}),
3540
addDomainToLintingRules(options.name),
3641
mergeWith(templateSource),
37-
(!options.addApp) ?
38-
noop() :
39-
externalSchematic('@nrwl/angular', 'app', {
40-
name: options.name,
41-
tags: `domain:${options.name},type:app`,
42-
style: 'scss',
43-
}),
42+
!options.addApp
43+
? noop()
44+
: externalSchematic('@nrwl/angular', 'app', {
45+
name: options.name,
46+
tags: `domain:${options.name},type:app`,
47+
style: 'scss',
48+
}),
4449
]);
4550
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createAction, props } from '@ngrx/store';
2+
import { <%= className %> } from '../entities/<%= fileName %>';
3+
4+
export const load<%= className %> = createAction(
5+
'[<%= className %>] Load <%= className %>'
6+
);
7+
8+
export const load<%= className %>Success = createAction(
9+
'[<%= className %>] Load <%= className %> Success',
10+
props<{ <%= propertyName %>: <%= className %>[] }>()
11+
);
12+
13+
export const load<%= className %>Failure = createAction(
14+
'[<%= className %>] Load <%= className %> Failure',
15+
props<{ error: any }>()
16+
);
17+
18+
/**
19+
* TODO: reconcile: className, fileName, propertyName may be referencing pluralized name and we may want a singular. especially for imports.
20+
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Injectable } from '@angular/core';
2+
import { createEffect, Actions, ofType } from '@ngrx/effects';
3+
import { catchError, map, switchMap } from 'rxjs/operators';
4+
import { of } from 'rxjs';
5+
import * as <%= className %>Actions from './<%= fileName %>.actions';
6+
7+
@Injectable()
8+
export class <%= className %>Effects {
9+
load<%= className %>$ = createEffect(() => this.actions$.pipe(
10+
ofType(<%= className %>Actions.load<%= className %>),
11+
switchMap((action)) =>
12+
this.backend.load().pipe(
13+
map((<%= propertyName %>) =>
14+
<%= className =>Actions.load<%= className %>Success({ <%= propertyName => })
15+
),
16+
catchError((error) =>
17+
of(<%= className =>Actions.load<%= className %>Failure({ error }))
18+
)
19+
)
20+
);
21+
22+
constructor(
23+
private actions$: Actions,
24+
private backend: <%= className %>DataService
25+
) { }
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { select, Store, Action } from '@ngrx/store';
4+
5+
import * as from<%= className %> from './<%= fileName %>.reducer';
6+
import * as <%= className %>Selectors from './<%= fileName %>.selectors';
7+
8+
@Injectable()
9+
export class <%= className %>Facade {
10+
11+
loaded$ = this.store.pipe(select(<%= className %>Selectors.get<%= className %>Loaded));
12+
all<%= className %>$ = this.store.pipe(select(<%= className %>Selectors.getAll<%= className %>));
13+
selected<%= className %>$ = this.store.pipe(select(<%= className %>Selectors.getSelected));
14+
15+
constructor(private store: Store<from<%= className %>.<%= className %>PartialState>) { }
16+
17+
dispatch(action: Action) {
18+
this.store.dispatch(action);
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createReducer, on, Action } from '@ngrx/store';
2+
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
3+
4+
import * as <%= className %>Actions from './<%= fileName %>.actions';
5+
import { <%= className %>Entity } from './<%= fileName %>.models';
6+
7+
export const <%= className.toUpperCase() %>_FEATURE_KEY = '<%= propertyName %>';
8+
9+
export interface State extends EntityState<<%= className %>Entity> {
10+
selectedId ?: string | number; // which <%= className %> record has been selected
11+
loaded : boolean; // has the <%= className %> list been loaded
12+
error ?: string | null; // last known error (if any)
13+
}
14+
15+
export interface <%= className %>PartialState {
16+
readonly [<%= className.toUpperCase() %>_FEATURE_KEY]: State;
17+
}
18+
19+
export const <%= propertyName %>Adapter: EntityAdapter<<%= className %>Entity> = createEntityAdapter<<%= className %>Entity>();
20+
21+
export const initialState: State = <%= propertyName %>Adapter.getInitialState({
22+
// set initial required properties
23+
loaded : false
24+
});
25+
26+
const <%= propertyName %>Reducer = createReducer(
27+
initialState,
28+
on(<%= className %>Actions.load<%= className %>,
29+
state => ({ ...state, loaded: false, error: null })
30+
),
31+
on(<%= className %>Actions.load<%= className %>Success,
32+
(state, { <%= propertyName %> }) => <%= propertyName %>Adapter.upsertMany(<%= propertyName %>, { ...state, loaded: true })
33+
),
34+
on(<%= className %>Actions.load<%= className %>Failure,
35+
(state, { error }) => ({ ...state, error })
36+
),
37+
);
38+
39+
export function reducer(state: State | undefined, action: Action) {
40+
return <%= propertyName %>Reducer(state, action);
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createFeatureSelector, createSelector } from '@ngrx/store';
2+
import { <%= className.toUpperCase() %>_FEATURE_KEY, State, <%= className %>PartialState, <%= propertyName %>Adapter } from './<%= fileName %>.reducer';
3+
4+
// Lookup the '<%= className %>' feature state managed by NgRx
5+
export const get<%= className %>State = createFeatureSelector<<%= className %>PartialState, State>(<%= propertyName.toUpperCase() %>_FEATURE_KEY);
6+
7+
const { selectAll, selectEntities } = <%= propertyName %>Adapter.getSelectors();
8+
9+
export const get<%= className %>Loaded = createSelector(
10+
get<%= className %>State,
11+
(state: State) => state.loaded
12+
);
13+
14+
export const get<%= className %>Error = createSelector(
15+
get<%= className %>State,
16+
(state: State) => state.error
17+
);
18+
19+
export const getAll<%= className %> = createSelector(
20+
get<%= className %>State,
21+
(state: State) => selectAll(state)
22+
);
23+
24+
export const get<%= className %>Entities = createSelector(
25+
get<%= className %>State,
26+
(state: State) => selectEntities(state)
27+
);
28+
29+
export const getSelectedId = createSelector(
30+
get<%= className %>State,
31+
(state: State) => state.selectedId
32+
);
33+
34+
export const getSelected = createSelector(
35+
get<%= className %>Entities,
36+
getSelectedId,
37+
(entities, selectedId) => selectedId && entities[selectedId]
38+
);

libs/ddd/src/schematics/domain/ngrx-files/application/.gitkeep

Whitespace-only changes.

libs/ddd/src/schematics/domain/ngrx-files/entities/.gitkeep

Whitespace-only changes.

libs/ddd/src/schematics/domain/ngrx-files/infrastructure/.gitkeep

Whitespace-only changes.

libs/ddd/src/schematics/domain/schema.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
},
2121
"type": {
2222
"type": "string",
23-
"enum": [
24-
"internal",
25-
"buildable",
26-
"publishable"
27-
],
23+
"enum": ["internal", "buildable", "publishable"],
2824
"description": "A type to determine if and how to build the library.",
2925
"default": "buildable"
26+
},
27+
"ngrx": {
28+
"type": "boolean",
29+
"default": false,
30+
"description": "Add ngrx for the domain",
31+
"x-prompt": "Would you like to add NgRx to your domain?"
3032
}
3133
},
3234
"required": ["name"]

0 commit comments

Comments
 (0)