Skip to content

Commit

Permalink
NgRx AppStore with RouterState and Devtools.
Browse files Browse the repository at this point in the history
  • Loading branch information
baumblatt committed May 31, 2020
1 parent e20de81 commit 5db8e2b
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ npm-debug.log*
.DS_Store

# npm pack file
angular-mat-baum-1.0.0-beta.1.tgz
angular-mat-baum-*.tgz
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@angular-devkit/core": "^9.1.7",
"@angular-devkit/schematics": "^9.1.7",
"@ngrx/schematics": "^9.2.0",
"@schematics/angular": "^9.1.7",
"typescript": "~3.8.2"
},
Expand Down
76 changes: 75 additions & 1 deletion src/application/app-after.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
} from '@angular-devkit/schematics';
import {createCustomTheme} from "./theme";

import {
addImportToModule, InsertChange, insertImport,
} from '@ngrx/schematics/schematics-core';
import * as ts from 'typescript';

export function factory(_options: App): Rule {
return (_tree: Tree, _context: SchematicContext) => {

Expand Down Expand Up @@ -56,7 +61,8 @@ export function factory(_options: App): Rule {
...strings,
path: `projects/${name}/src/app`
})]), MergeStrategy.AllowCreationConflict);
}, () => {
},
() => {
return mergeWith(apply(url('./files/styles'), [template({
..._options,
...strings,
Expand All @@ -73,6 +79,74 @@ export function factory(_options: App): Rule {
(tree) => {
tree.overwrite(`projects/${name}/src/styles.scss`, createCustomTheme(strings.dasherize(name)))
},
() => {
return mergeWith(apply(url('./files/store'), [template({
..._options,
...strings,
path: `projects/${name}/src/app`
})]), MergeStrategy.AllowCreationConflict);
},
(tree) => {
const source = ts.createSourceFile(
`projects/${name}/src/app/app.module.ts`,
// @ts-ignore
tree.read(`projects/${name}/src/app/app.module.ts`).toString(),
ts.ScriptTarget.Latest, true
);

const changes = addImportToModule(
source,
`projects/${name}/src/app/app.module.ts`,
`StoreModule.forRoot(reducers, { metaReducers, runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true }})`,
'@ngrx/store'
);

changes.push(...addImportToModule(
source,
`projects/${name}/src/app/app.module.ts`,
`StoreDevtoolsModule.instrument({maxAge: 25, logOnly: environment.production})`,
'@ngrx/store-devtools'
));

changes.push(...addImportToModule(
source,
`projects/${name}/src/app/app.module.ts`,
`StoreRouterConnectingModule.forRoot({ serializer: CustomSerializer })`,
'@ngrx/router-store'
));

changes.push(insertImport(
source,
`projects/${name}/src/app/app.module.ts`,
'reducers, metaReducers',
'./store/reducers/global.reducers'
));

changes.push(insertImport(
source,
`projects/${name}/src/app/app.module.ts`,
'environment',
'../environments/environment'
));

changes.push(insertImport(
source,
`projects/${name}/src/app/app.module.ts`,
'CustomSerializer',
'./store/reducers/custom-route-serializer'
));

const recorder = tree.beginUpdate(`projects/${name}/src/app/app.module.ts`);

for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
tree.commitUpdate(recorder);

return tree;
},
])
}
}
1 change: 0 additions & 1 deletion src/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function factory(_options: App): Rule {
path: `projects/${name}/src/app`,
}),
() => {
console.log('defining routes');
return mergeWith(apply(url('./files/routers'), [template({
..._options,
...strings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Params, RouterStateSnapshot } from '@angular/router';
import { RouterStateSerializer } from '@ngrx/router-store';

export interface RouterStateUrl {
url: string;
params: Params;
queryParams: Params;
}

export class CustomSerializer implements RouterStateSerializer<RouterStateUrl> {
serialize(routerState: RouterStateSnapshot): RouterStateUrl {
let route = routerState.root;

while (route.firstChild) {
route = route.firstChild;
}

const {
url,
root: { queryParams },
} = routerState;
const { params } = route;

// Only return an object including the URL, params and query params
// instead of the entire snapshot
return { url, params, queryParams };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { routerReducer, RouterReducerState } from '@ngrx/router-store';
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import { RouterStateUrl } from './custom-route-serializer';
import { environment } from '../../../environments/environment';

export interface AppState {
router: RouterReducerState<RouterStateUrl>;
}

export const reducers: ActionReducerMap<AppState> = {
router: routerReducer
};


export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [] : [];

0 comments on commit 5db8e2b

Please sign in to comment.