Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
import { <%= classify(name) %>SearchQuery } from './<%= dasherize(name) %>.reducer';

export enum <%= classify(name) %>ActionTypes {
Insert<%= classify(name) %> = '[<%= classify(name) %>] Insert',
Insert<%= classify(name) %>Success = '[<%= classify(name) %>] Insert Success',
Insert<%= classify(name) %>Fail = '[<%= classify(name) %>] Insert Fail',
Create<%= classify(name) %> = '[<%= classify(name) %>] Create',
Create<%= classify(name) %>Success = '[<%= classify(name) %>] Insert Success',
Create<%= classify(name) %>Fail = '[<%= classify(name) %>] Insert Fail',

SearchAll<%= classify(name) %>Entities = '[<%= classify(name) %>] Search',
SearchAll<%= classify(name) %>EntitiesSuccess = '[<%= classify(name) %>] Search Success',
Expand All @@ -28,20 +28,20 @@ export enum <%= classify(name) %>ActionTypes {
Select<%= classify(name) %>ById = '[<%= classify(name) %>] Select By ID'
}

// ========================================= INSERT
// ========================================= CREATE

export class Insert<%= classify(name) %> implements Action {
readonly type = <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>;
export class Create<%= classify(name) %> implements Action {
readonly type = <%= classify(name) %>ActionTypes.Create<%= classify(name) %>;
constructor(public payload: { <%= name %>: <%= classify(name) %> }) {}
}

export class Insert<%= classify(name) %>Success implements Action {
readonly type = <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>Success;
export class Create<%= classify(name) %>Success implements Action {
readonly type = <%= classify(name) %>ActionTypes.Create<%= classify(name) %>Success;
constructor(public payload: { result: <%= classify(name) %> }) {}
}

export class Insert<%= classify(name) %>Fail implements Action {
readonly type = <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>Fail;
export class Create<%= classify(name) %>Fail implements Action {
readonly type = <%= classify(name) %>ActionTypes.Create<%= classify(name) %>Fail;
constructor(public payload: { error: string }) {}
}

Expand Down Expand Up @@ -127,9 +127,9 @@ export class Select<%= classify(name) %>ById implements Action {
}

export type <%= classify(name) %>Actions =
| Insert<%= classify(name) %>
| Insert<%= classify(name) %>Success
| Insert<%= classify(name) %>Fail
| Create<%= classify(name) %>
| Create<%= classify(name) %>Success
| Create<%= classify(name) %>Fail
| SearchAll<%= classify(name) %>Entities
| SearchAll<%= classify(name) %>EntitiesSuccess
| SearchAll<%= classify(name) %>EntitiesFail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { cold, hot } from 'jasmine-marbles';
import { Observable } from 'rxjs';

import {
Insert<%= classify(name) %>,
Insert<%= classify(name) %>Success,
Insert<%= classify(name) %>Fail,
Create<%= classify(name) %>,
Create<%= classify(name) %>Success,
Create<%= classify(name) %>Fail,
SearchAll<%= classify(name) %>Entities,
SearchAll<%= classify(name) %>EntitiesSuccess,
SearchAll<%= classify(name) %>EntitiesFail,
Expand Down Expand Up @@ -52,15 +52,15 @@ describe('<%= classify(name) %>Effects', () => {
service = TestBed.get(<%= classify(name) %>Service);
});

it('should be created', () => {
it('should be constructed', () => {
expect(effects).toBeTruthy();
});

describe('insert', () => {
it('should return Insert<%= classify(name) %>Success action with entity on success', () => {
describe('create', () => {
it('should return Create<%= classify(name) %>Success action with entity on success', () => {
const entity = generate<%= classify(name) %>();
const insertAction = new Insert<%= classify(name) %>({ <%= name %>: entity });
const successAction = new Insert<%= classify(name) %>Success({ result: entity });
const insertAction = new Create<%= classify(name) %>({ <%= name %>: entity });
const successAction = new Create<%= classify(name) %>Success({ result: entity });

actions = hot('a-', { a: insertAction });
service.create.and.returnValue(cold('-e|', { e: entity }));
Expand All @@ -69,10 +69,10 @@ describe('<%= classify(name) %>Effects', () => {
expect(effects.insert).toBeObservable(expected);
});

it('should return Insert<%= classify(name) %>Fail with error object on failure', () => {
it('should return Create<%= classify(name) %>Fail with error object on failure', () => {
const entity = generate<%= classify(name) %>();
const insertAction = new Insert<%= classify(name) %>({ <%= name %>: entity });
const failAction = new Insert<%= classify(name) %>Fail({ error: 'fail' });
const insertAction = new Create<%= classify(name) %>({ <%= name %>: entity });
const failAction = new Create<%= classify(name) %>Fail({ error: 'fail' });

actions = hot('i-', { i: insertAction });
service.create.and.returnValue(cold('-#|', {}, { message: 'fail'}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { Update } from '@ngrx/entity';

import {
<%= classify(name) %>ActionTypes,
Insert<%= classify(name) %>,
Insert<%= classify(name) %>Success,
Insert<%= classify(name) %>Fail,
Create<%= classify(name) %>,
Create<%= classify(name) %>Success,
Create<%= classify(name) %>Fail,
SearchAll<%= classify(name) %>Entities,
SearchAll<%= classify(name) %>EntitiesSuccess,
SearchAll<%= classify(name) %>EntitiesFail,
Expand All @@ -38,16 +38,16 @@ import { <%= classify(name) %>Service } from './<%= dasherize(name) %>.service';
@Injectable()
export class <%= classify(name) %>Effects {

// ========================================= INSERT
// ========================================= CREATE
@Effect()
insert: Observable<Action> = this.actions$
create: Observable<Action> = this.actions$
.pipe(
ofType<Insert<%= classify(name) %>>(<%= classify(name) %>ActionTypes.Insert<%= classify(name) %>),
ofType<Create<%= classify(name) %>>(<%= classify(name) %>ActionTypes.Create<%= classify(name) %>),
exhaustMap((action) =>
this.service.create(action.payload.<%= name %>).pipe(
map((<%= name %>: <%= classify(name) %>) => new Insert<%= classify(name) %>Success({ result: <%= name %> })),
map((<%= name %>: <%= classify(name) %>) => new Create<%= classify(name) %>Success({ result: <%= name %> })),
catchError(({ message }) =>
of(new Insert<%= classify(name) %>Fail({ error: message }))
of(new Create<%= classify(name) %>Fail({ error: message }))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe('<%= name %>Reducer', () => {
});
});

describe('upon Insert<%= classify(name) %>', () => {
describe('upon Create<%= classify(name) %>', () => {
it('should set loading to true and clear any error', () => {
const action = new actions.Insert<%= classify(name) %>({ <%= name %>: generate<%= classify(name) %>() });
const action = new actions.Create<%= classify(name) %>({ <%= name %>: generate<%= classify(name) %>() });

expect(<%= name %>Reducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
...initialState,
Expand All @@ -42,10 +42,10 @@ describe('<%= name %>Reducer', () => {
});
});

describe('upon Insert<%= classify(name) %>Success', () => {
describe('upon Create<%= classify(name) %>Success', () => {
it('should add the given <%= classify(name) %>, set loading to false, and clear any error', () => {
const result = generate<%= classify(name) %>();
const action = new actions.Insert<%= classify(name) %>Success({ result });
const action = new actions.Create<%= classify(name) %>Success({ result });

expect(<%= name %>Reducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
...initialState,
Expand All @@ -56,10 +56,10 @@ describe('<%= name %>Reducer', () => {
});
});

describe('upon Insert<%= classify(name) %>Fail', () => {
describe('upon Create<%= classify(name) %>Fail', () => {
it('should set loading to true and echo the error', () => {
const error = 'test insert error';
const action = new actions.Insert<%= classify(name) %>Fail({ error });
const action = new actions.Create<%= classify(name) %>Fail({ error });

expect(<%= name %>Reducer(initialState, action)).toEqual({
...initialState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export interface <%= classify(name) %>State extends EntityState<<%= classify(nam
query: <%= classify(name) %>SearchQuery;
}

export const adapter: EntityAdapter<<%= classify(name) %>> = createEntityAdapter<<%= classify(name) %>>();
export const <%= name %>Adapter: EntityAdapter<<%= classify(name) %>> = createEntityAdapter<<%= classify(name) %>>();

export const initialState: <%= classify(name) %>State = adapter.getInitialState({
export const initial<%= classify(name)%>State: <%= classify(name) %>State = adapter.getInitialState({
// additional <%= name %> state properties
selectedId: null,
loading: false,
Expand All @@ -32,27 +32,27 @@ export const initialState: <%= classify(name) %>State = adapter.getInitialState(
}
});

export function <%= name %>Reducer(state = initialState, action: <%= classify(name) %>Actions): <%= classify(name) %>State {
export function <%= name %>Reducer(state = initial<%= classify(name)%>State, action: <%= classify(name) %>Actions): <%= classify(name) %>State {
switch (action.type) {
case <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>:
case <%= classify(name) %>ActionTypes.Create<%= classify(name) %>:
return {
...state,
loading: true,
error: ''
};

case <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>Success:
case <%= classify(name) %>ActionTypes.Create<%= classify(name) %>Success:
return {
...adapter.addOne(action.payload.result, state),
loading: false,
error: ''
};

case <%= classify(name) %>ActionTypes.Insert<%= classify(name) %>Fail:
case <%= classify(name) %>ActionTypes.Create<%= classify(name) %>Fail:
return {
...state,
loading: false,
error: '<%= classify(name) %> insert failed: ' + action.payload.error
error: '<%= classify(name) %> create failed: ' + action.payload.error
};

case <%= classify(name) %>ActionTypes.SearchAll<%= classify(name) %>Entities:
Expand Down
40 changes: 23 additions & 17 deletions src/ngrx-entity/__files__/__name@dasherize@if-flat__/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
import { createSelector, createFeatureSelector } from '@ngrx/store';

import * as from<%= classify(name) %>State from './<%= dasherize(name) %>.reducer';
import {
<%= name %>Reducer,
getSelectedId,
getLoading,
getError,
getQuery
} from './<%= dasherize(name) %>.reducer';
import { <%= classify(name) %>State } from './<%= dasherize(name) %>.reducer';

export const get<%= classify(name) %>State = createFeatureSelector<<%= classify(name) %>State>('<%= name %>');

export const {
selectIds: getAll<%= classify(name) %>Ids,
selectEntities: getAll<%= classify(name) %>EntitiesAsMap,
selectAll: getAll<%= classify(name) %>EntitiesAsArray,
selectTotal: getTotal<%= classify(name) %>Entities
} = from<%= classify(name) %>State.adapter.getSelectors(get<%= classify(name) %>State);
selectIds: <%= name %>Ids,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be plural?

selectEntities: <%= name %>Entities,
selectAll: <%= name %>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be plural?

selectTotal: <%= name %>Count
} = <%= name %>Adapter.getSelectors(get<%= classify(name) %>State);

export const getSelected<%= classify(name) %>Id = createSelector(
export const current<%= classify(name) %>Id = createSelector(
get<%= classify(name) %>State,
from<%= classify(name) %>State.getSelectedId
getSelectedId
);

export const getSelected<%= classify(name) %> = createSelector(
getSelected<%= classify(name) %>Id,
getAll<%= classify(name) %>EntitiesAsMap,
export const current<%= classify(name) %> = createSelector(
current<%= classify(name) %>Id,
<%= name %>Entities,
(selected<%= classify(name) %>Id, <%= name %>Entities) =>
selected<%= classify(name) %>Id && <%= name %>Entities[selected<%= classify(name) %>Id]
);

export const getLoading = createSelector(
export const <%= name %>Loading = createSelector( // TODO: Need to pluraliae name
get<%= classify(name) %>State,
from<%= classify(name) %>State.getLoading
getLoading
);

export const getError = createSelector(
export const <%= name %>Error = createSelector(
get<%= classify(name) %>State,
from<%= classify(name) %>State.getError
getError
);

export const getQuery = createSelector(
export const <%= name %>Query = createSelector(
get<%= classify(name) %>State,
from<%= classify(name) %>State.getQuery
getQuery
);