-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): add api success/failure effects/actions to ng gener…
…ate feature (#1530)
- Loading branch information
1 parent
48a2370
commit e17a787
Showing
16 changed files
with
295 additions
and
21 deletions.
There are no files selected for viewing
17 changes: 15 additions & 2 deletions
17
modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import { Action } from '@ngrx/store'; | ||
|
||
export enum <%= classify(name) %>ActionTypes { | ||
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s' | ||
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s', | ||
<% if (api) { %>Load<%= classify(name) %>sSuccess = '[<%= classify(name) %>] Load <%= classify(name) %>s Success',<% } %> | ||
<% if (api) { %>Load<%= classify(name) %>sFailure = '[<%= classify(name) %>] Load <%= classify(name) %>s Failure',<% } %> | ||
} | ||
|
||
export class Load<%= classify(name) %>s implements Action { | ||
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s; | ||
} | ||
<% if (api) { %> | ||
export class Load<%= classify(name) %>sSuccess implements Action { | ||
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sSuccess; | ||
constructor(public payload: { data: any }) { } | ||
} | ||
|
||
export type <%= classify(name) %>Actions = Load<%= classify(name) %>s; | ||
export class Load<%= classify(name) %>sFailure implements Action { | ||
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sFailure; | ||
constructor(public payload: { error: any }) { } | ||
} | ||
<% } %> | ||
<% if (api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s | Load<%= classify(name) %>sSuccess | Load<%= classify(name) %>sFailure;<% } %> | ||
<% if (!api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s;<% } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
modules/schematics/src/effect/files/__name@dasherize@if-flat__/__name@dasherize__.effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Actions, Effect<% if (feature) { %>, ofType<% } %> } from '@ngrx/effects'; | ||
<% if (feature) { %>import { <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %> | ||
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';<% } %> | ||
<% if (feature && api) { %>import { EMPTY, of } from 'rxjs';<% } %> | ||
<% if (feature && api) { %>import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %> | ||
<% if (feature && !api) { %>import { <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %> | ||
|
||
@Injectable() | ||
export class <%= classify(name) %>Effects { | ||
<% if (feature) { %> | ||
<% if (feature && api) { %> | ||
@Effect() | ||
load<%= classify(name) %>s$ = this.actions$.pipe( | ||
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s), | ||
concatMap(() => | ||
/** An EMPTY observable only emits completion. Replace with your own observable API request */ | ||
EMPTY.pipe( | ||
map(data => new Load<%= classify(name) %>sSuccess({ data })), | ||
catchError(error => of(new Load<%= classify(name) %>sFailure({ error })))) | ||
) | ||
); | ||
<% } %> | ||
<% if (feature && !api) { %> | ||
@Effect() | ||
load<%= classify(name) %>s$ = this.actions$.pipe(ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s)); | ||
<% } %> | ||
<% if (feature && api) { %> | ||
constructor(private actions$: Actions<<%= classify(name) %>Actions>) {} | ||
<% } else { %> | ||
constructor(private actions$: Actions) {} | ||
<% } %> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.