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

Put getters together with action types #8

Open
Romms opened this issue Mar 25, 2018 · 0 comments
Open

Put getters together with action types #8

Romms opened this issue Mar 25, 2018 · 0 comments

Comments

@Romms
Copy link

Romms commented Mar 25, 2018

If I have two action types with different payload structure I have to describe the types and functions for selecting the data from these action in different places

const todos = map({
  addActionTypes: ['ADD_TODO', 'ADD_NOTE_WITH_TODO'],
  keyGetter: action => {
    if (action.type === 'ADD_NOTE_WITH_TODO') {
      return action.payload.note.todo.id;
    }

    if (action.type === 'ADD_TODO') {
      return action.payload.id;
    }
  },
  itemGetter: action => {
    if (action.type === 'ADD_NOTE_WITH_TODO') {
      return action.payload.note.todo;
    }

    if (action.type === 'ADD_TODO') {
      return action.payload;
    }
  },
});

That looks pretty bad because we divide describing action type and related logic.

What about describing all related getter methods together with these types?

const todos = map({
  addActionTypes: [
    { 
      type: 'ADD_TODO' 
    },
    {
      type: 'ADD_NOTE_WITH_TODO',
      keyGetter: action => action.payload.note.todo.id,
      itemGetter: action => action.payload.note.todo,
    },
  ],
});
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

No branches or pull requests

1 participant