Skip to content

Commit

Permalink
there are no entity methods creators in ba sing se
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 18, 2024
1 parent 5f5fe79 commit 31c4794
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 621 deletions.
2 changes: 1 addition & 1 deletion docs/api/createEntityAdapter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ In other words, they accept a state that looks like `{ids: [], entities: {}}`, a

These CRUD methods may be used in multiple ways:

- They may be passed as case reducers directly to `createReducer` and `createSlice`. (also see the [`create.entityMethods`](./createSlice#createentitymethods-entitymethodscreator) slice creator which can assist with this)
- They may be passed as case reducers directly to `createReducer` and `createSlice`.
- They may be used as "mutating" helper methods when called manually, such as a separate hand-written call to `addOne()` inside of an existing case reducer, if the `state` argument is actually an Immer `Draft` value.
- They may be used as immutable update methods when called manually, if the `state` argument is actually a plain JS object or array.

Expand Down
99 changes: 0 additions & 99 deletions docs/api/createSlice.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -845,105 +845,6 @@ reducers: (create) => {
:::
##### `create.entityMethods` (`entityMethodsCreator`)
Creates a set of reducers for managing a normalized entity state, based on a provided [adapter](./createEntityAdapter).
```ts
import {
createEntityAdapter,
buildCreateSlice,
entityMethodsCreator,
} from '@reduxjs/toolkit'

const createAppSlice = buildCreateSlice({
creators: { entityMethods: entityMethodsCreator },
})

interface Post {
id: string
text: string
}

const postsAdapter = createEntityAdapter<Post>()

const postsSlice = createAppSlice({
name: 'posts',
initialState: postsAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(postsAdapter),
}),
})

export const { setOne, upsertMany, removeAll, ...etc } = postsSlice.actions
```
:::caution
Because this creator returns an object of multiple reducer definitions, it should be spread into the final object returned by the `reducers` callback.
:::
**Parameters**
- **adapter** The [adapter](./createEntityAdapter) to use.
- **config** The configuration object. (optional)
The configuration object can contain some of the following options:
**`selectEntityState`**
A selector to retrieve the entity state from the slice state. Defaults to `state => state`, but should be provided if the entity state is nested.
```ts no-transpile
const postsSlice = createAppSlice({
name: 'posts',
initialState: { posts: postsAdapter.getInitialState() },
reducers: (create) => ({
...create.entityMethods(postsAdapter, {
selectEntityState: (state) => state.posts,
}),
}),
})
```
**`name`, `pluralName`**
It's often desirable to modify the reducer names to be specific to the data type being used. These options allow you to do that.
```ts no-transpile
const postsSlice = createAppSlice({
name: 'posts',
initialState: postsAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(postsAdapter, {
name: 'post',
}),
}),
})

const { addOnePost, upsertManyPosts, removeAllPosts, ...etc } =
postsSlice.actions
```
`pluralName` defaults to `name + 's'`, but can be provided if this isn't desired.
```ts no-transpile
const gooseSlice = createAppSlice({
name: 'geese',
initialState: gooseAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(gooseAdapter, {
name: 'goose',
pluralName: 'geese',
}),
}),
})

const { addOneGoose, upsertManyGeese, removeAllGeese, ...etc } =
gooseSlice.actions
```
### Writing your own creators
In version v2.2.0 (TODO), we introduced a system for including your own creators.
Expand Down
72 changes: 12 additions & 60 deletions packages/toolkit/src/entities/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { Draft } from 'immer'
import type { PayloadAction } from '../createAction'
import type { GetSelectorsOptions } from './state_selectors'
import type { CastAny, Id as Compute } from '../tsHelpers'
import type { CaseReducerDefinition } from '../createSlice'
import type { CaseReducer } from '../createReducer'

/**
* @public
Expand Down Expand Up @@ -160,51 +158,12 @@ export interface EntityStateAdapter<T, Id extends EntityId> {
/**
* @public
*/
export type EntitySelectors<
T,
V,
Id extends EntityId,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
> = Compute<
{
[K in `select${Capitalize<Single>}Ids`]: (state: V) => Id[]
} & {
[K in `select${Capitalize<Single>}Entities`]: (state: V) => Record<Id, T>
} & {
[K in `selectAll${Capitalize<Plural>}`]: (state: V) => T[]
} & {
[K in `selectTotal${Capitalize<Plural>}`]: (state: V) => number
} & {
[K in `select${Capitalize<Single>}ById`]: (
state: V,
id: Id,
) => Compute<UncheckedIndexedAccess<T>>
}
>

export type DefaultPlural<Single extends string> = Single extends ''
? ''
: `${Single}s`

export type EntityReducers<
T,
Id extends EntityId,
State = EntityState<T, Id>,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
> = {
[K in keyof EntityStateAdapter<
T,
Id
> as `${K}${Capitalize<K extends `${string}One` ? Single : Plural>}`]: EntityStateAdapter<
T,
Id
>[K] extends (state: any) => any
? CaseReducerDefinition<State, PayloadAction>
: EntityStateAdapter<T, Id>[K] extends CaseReducer<any, infer A>
? CaseReducerDefinition<State, A>
: never
export interface EntitySelectors<T, V, Id extends EntityId> {
selectIds: (state: V) => Id[]
selectEntities: (state: V) => Record<Id, T>
selectAll: (state: V) => T[]
selectTotal: (state: V) => number
selectById: (state: V, id: Id) => Compute<UncheckedIndexedAccess<T>>
}

/**
Expand All @@ -228,19 +187,12 @@ export interface EntityAdapter<T, Id extends EntityId>
extends EntityStateAdapter<T, Id>,
EntityStateFactory<T, Id>,
Required<EntityAdapterOptions<T, Id>> {
getSelectors<
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
getSelectors(
selectState?: undefined,
options?: GetSelectorsOptions<Single, Plural>,
): EntitySelectors<T, EntityState<T, Id>, Id, Single, Plural>
getSelectors<
V,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
options?: GetSelectorsOptions,
): EntitySelectors<T, EntityState<T, Id>, Id>
getSelectors<V>(
selectState: (state: V) => EntityState<T, Id>,
options?: GetSelectorsOptions<Single, Plural>,
): EntitySelectors<T, V, Id, Single, Plural>
options?: GetSelectorsOptions,
): EntitySelectors<T, V, Id>
}
161 changes: 0 additions & 161 deletions packages/toolkit/src/entities/slice_creator.ts

This file was deleted.

Loading

0 comments on commit 31c4794

Please sign in to comment.