Skip to content
This repository was archived by the owner on Nov 27, 2019. It is now read-only.

Commit

Permalink
docs(entity): Grammatical and consistency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeHowellDev authored and brandonroberts committed Apr 26, 2018
1 parent 7cd10db commit aa3cfbf
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions docs/entity/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

A method for returning a generic entity adapter for a single entity state collection. The
returned adapter provides many [methods](#adapter-methods) for performing operations
against the collection type. The method takes an object for configuration with 2 properties.
against the collection type. The method takes an object with 2 properties for configuration.

- `selectId`: A `method` for selecting the primary id for the collection
- `sortComparer`: A compare function used to [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) the collection. The comparer function is only needed if the collection needs to be sorted before being displayed. Set to `false` to leave the collection unsorted, which is more performant during CRUD operations.
* `selectId`: A `method` for selecting the primary id for the collection.
* `sortComparer`: A compare function used to [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) the collection. The comparer function is only needed if the collection needs to be sorted before being displayed. Set to `false` to leave the collection unsorted, which is more performant during CRUD operations.

Usage:

Expand Down Expand Up @@ -60,11 +60,11 @@ export interface State extends EntityState<User> {

export const initialState: State = adapter.getInitialState({
// additional entity state properties
selectedUserId: null
selectedUserId: null,
});

export function reducer(state = initialState, action): State {
switch(action.type) {
switch (action.type) {
default: {
return state;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ export enum UserActionTypes {
UPDATE_USERS = '[User] Update Users',
DELETE_USER = '[User] Delete User',
DELETE_USERS = '[User] Delete Users',
CLEAR_USERS = '[User] Clear Users'
CLEAR_USERS = '[User] Clear Users',
}

export class LoadUsers implements Action {
Expand Down Expand Up @@ -180,19 +180,20 @@ export class ClearUsers implements Action {
}

export type UserActionsUnion =
| LoadUsers
| AddUser
| UpsertUser
| AddUsers
| UpsertUsers
| UpdateUser
| UpdateUsers
| DeleteUser
| DeleteUsers
| ClearUsers;
| LoadUsers
| AddUser
| UpsertUser
| AddUsers
| UpsertUsers
| UpdateUser
| UpdateUsers
| DeleteUser
| DeleteUsers
| ClearUsers;
```

`user.reducer.ts`

```ts
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { User } from './user.model';
Expand All @@ -207,13 +208,10 @@ export const adapter: EntityAdapter<User> = createEntityAdapter<User>();

export const initialState: State = adapter.getInitialState({
// additional entity state properties
selectedUserId: null
selectedUserId: null,
});

export function reducer(
state = initialState,
action: UserActionsUnion
): State {
export function reducer(state = initialState, action: UserActionsUnion): State {
switch (action.type) {
case UserActionTypes.ADD_USER: {
return adapter.addOne(action.payload.user, state);
Expand All @@ -229,7 +227,7 @@ export function reducer(

case UserActionTypes.UPSERT_USERS: {
return adapter.upsertMany(action.payload.users, state);
}
}

case UserActionTypes.UPDATE_USER: {
return adapter.updateOne(action.payload.user, state);
Expand Down Expand Up @@ -274,7 +272,7 @@ export const {
selectAll: selectAllUsers,

// select the total user count
selectTotal: selectUserTotal
selectTotal: selectUserTotal,
} = adapter.getSelectors();
```

Expand All @@ -289,24 +287,43 @@ Usage:
`reducers/index.ts`

```ts
import { createSelector, createFeatureSelector, ActionReducerMap } from '@ngrx/store';
import {
createSelector,
createFeatureSelector,
ActionReducerMap,
} from '@ngrx/store';
import * as fromUser from './user.reducer';

export interface State {
users: fromUser.State;
}

export const reducers: ActionReducerMap<State> = {
users: fromUser.reducer
users: fromUser.reducer,
};

export const selectUserState = createFeatureSelector<fromUser.State>('users');

export const selectUserIds = createSelector(selectUserState, fromUser.selectUserIds);
export const selectUserEntities = createSelector(selectUserState, fromUser.selectUserEntities);
export const selectAllUsers = createSelector(selectUserState, fromUser.selectAllUsers);
export const selectUserTotal = createSelector(selectUserState, fromUser.selectUserTotal);
export const selectCurrentUserId = createSelector(selectUserState, fromUser.getSelectedUserId);
export const selectUserIds = createSelector(
selectUserState,
fromUser.selectUserIds
);
export const selectUserEntities = createSelector(
selectUserState,
fromUser.selectUserEntities
);
export const selectAllUsers = createSelector(
selectUserState,
fromUser.selectAllUsers
);
export const selectUserTotal = createSelector(
selectUserState,
fromUser.selectUserTotal
);
export const selectCurrentUserId = createSelector(
selectUserState,
fromUser.getSelectedUserId
);

export const selectCurrentUser = createSelector(
selectUserEntities,
Expand Down

0 comments on commit aa3cfbf

Please sign in to comment.