Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dictionary support to createEntityAdapter many methods #444

Merged
merged 5 commits into from
Mar 26, 2020

Conversation

msutkowski
Copy link
Member

@msutkowski msutkowski commented Mar 26, 2020

Overview

This PR allows a user to pass in an object with the shape of { 1: { id: 1, other: 'value' } directly to the upsertMany, addMany, and setAll methods of an entity adapter. This ultimately makes integration with a normalization library like normalizr much simpler.

Todo

  • Review updateMany
  • Review tests
  • Expand type tests

addMany<S extends EntityState<T>>(
state: PreventAny<S, T>,
entities: PayloadAction<T[]>
entities: T[] | Record<string, T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use EntityId instead of string, since IDs could be numbers.

Hmm. It looks like normalizr's types are:

{ [key:string]: { [key:string]: T } | undefined}

Do these mesh okay? Should we be using the Dictionary<T> type instead?

Copy link
Member Author

@msutkowski msutkowski Mar 26, 2020

Choose a reason for hiding this comment

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

That's a good question. { [key: string]: T} types should play nicely with Record<EntityId, T> for the most part being that it's the same thing without the index key signature features. I'll try experimenting some, but maybe @phryneas has an idea here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just wanted to leave a record of where I got to. I struggled a lot getting normalizr to give me the actual types of the processed data. If we define what the ReturnType should be, there are no issues and the types pass through as expected without errors in the reducer.

export const fetchArticle = createAsyncThunk<
  {
    articles: { [key: string]: Article };
    users: { [key: string]: Author };
    comments: { [key: string]: Comment };
  },
  number,
  any
>("articles/fetchArticle", async id => {
  const data = await fakeAPI.articles.show(id);
  // normalize the data so reducers can responded to a predictable payload, in this case: `action.payload = { users: { 1: { ... } }, articles: {}, comments: {} }`
  const normalized = normalize(data, articleEntity);
  return normalized.entities as any; // cast as any to avoid the type issues
});

Copy link
Member Author

Choose a reason for hiding this comment

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

Quick update: the normalizr types don't infer anything at all, so this about the 'cleanest' way I can get this work:

export const fetchArticle = createAsyncThunk(
  "articles/fetchArticle",
  async (id: number) => {
    const data = await fakeAPI.articles.show(id);
    // normalize the data so reducers can responded to a predictable payload, in this case: `action.payload = { users: {}, articles: {}, comments: {} }`
    const normalized = normalize<
      any,
      {
        articles: { [key: string]: Article };
        users: { [key: string]: Author };
        comments: { [key: string]: Comment };
      }
    >(data, articleEntity);
    return normalized.entities;
  }
);

Copy link
Collaborator

Choose a reason for hiding this comment

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

For the "Usage Guide" page, we can keep things as plain JS, so we don't have to worry about that here.

Perhaps we could make a brief note of this in the "Usage with TS" page, and possibly point to a TS sandbox.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I've already converted the sandbox mentioned in #441 to TS specifically for this code sample above. If this PR is approved, I'll update the usage docs and sandbox references there in a cleanup pass.

@phryneas
Copy link
Member

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants