Skip to content

PoC: Schematics to create a main feature reducer file #3302

Open
@moniuch

Description

@moniuch

I would like to ask you to consider providing a schematic which I don't believe exists yet, but which to me seems like the missing bit in the puzzle.

The artifact of the schematic would be a main feature reducer file (say, [name].feature-reducer.ts) holding an empty state interface, and an empty reducer map.
This would be the home for the slices to come, this would be the file which you would reference in the feature schematic (ie. the --reducers parameter).

An example of the output would be (let me base on the code from SO):

export interface UserModuleState {}
export interface State extends fromRoot.State {}
export const reducers = {};

export const selectUserModuleState = createFeatureSelector<UserModuleState>('userModule');

This would be a home for the future slices: search, detail, detailBase, which you would add using every time using @ngrx/schematics:feature --reducers user.feature-reducer.ts, and end up with:

export interface UserModuleState {
  search: fromSearch.State;  
  detail: fromUserDetail.State;
  detailBase: fromDetailBase.State;
}

export interface State extends fromRoot.State {
    userModule: UserModuleState;    
}

export const reducers = {    
    search: fromSearch.reducer,
    detail: fromUserDetail.reducer,
    detailBase : fromDetailBase.reducer
};

// ...
// + suitable edits in the declaring module

IMO, there is no schematic available currently that produces this code. Having it available would make the whole store-building flow very intuitive and free from guesswork:

  • first, you create feature-reducer:
    ng generate @ngrx/schematics:feature-reducer
    which generates [name].feature-reducer.ts
  • then for each slice, a feature composed into feature reducer:
    ng generate @ngrx/schematics:feature --reducers [name].feature-reducer.ts

Currently, from what I was able to find out in my experiments, setting up a feature reducer requires manual work and model-defining hassle.

If accepted, I would be willing to submit a PR for this feature

[ ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions