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

How to get response data after mutation without custom reducer. #305

Closed
svindler opened this issue Jan 13, 2020 · 7 comments
Closed

How to get response data after mutation without custom reducer. #305

svindler opened this issue Jan 13, 2020 · 7 comments

Comments

@svindler
Copy link

Hello,

I'm using the networkReduxer, but after reading the docs couple times can't find an easy way to get the data after a mutation without creating a custom reducer.

These are my actions:

const doFetchQuestion = id => ({
  type: FETCH_QUESTION,
  request: {
    url: `/api/questions/${id}`,
  },
  meta: {
    abortOn: CLEAR_QUESTION,
    resetOn: [CLEAR_QUESTION],
    mutations: {
      [SAVE_QUESTION]: {
        updateData: true
      },
    },
  },
});

const doClearQuestion = () => ({
  type: CLEAR_QUESTION,
});

const doSaveQuestion = question => ({
  type: SAVE_QUESTION,
  request: {
    url: `/api/questions`,
    method: 'POST',
    data: question,
  },
});

Please point me to the right directon.

Thanks!

@klis87
Copy link
Owner

klis87 commented Jan 13, 2020

With networkReducer u put mutations not to query but mutation, like:

const doSaveQuestion = question => ({
  type: SAVE_QUESTION,
  request: {
    url: `/api/questions`,
    method: 'POST',
    data: question,
  },
  meta: 
   id: question.id,
   mutations: {
      [FETCH_QUESTION]: {
       updateData: (state, action) =>
          state.data.map(v => v.id === action.meta.id ? action.data : v),
      },
    },
});

I can totally understand though it is confusing. In the future version only networkReducer will be supported, so it will be easier to explain in docs.

Also there won't be any internal state in networkReducer, so this id: question.id, in meta wont be necessary anymore as normal javascript closure will suffice. Here won't as mutations config is added only once for each action of a given type, so id would be fixed, hence this meta trick so u can get id from responseAction in updateData. Please live with it for now, next release will fixed all those problems.

@svindler
Copy link
Author

Thank you for your quick reply.

I trigger this action on form submission, so I don't have an ID yet. I tried the mutation in the doSaveQuestion action but the updateData function doesn't gets called.

@klis87
Copy link
Owner

klis87 commented Jan 14, 2020

See example especially this file and line to see mutations in action https://github.com/klis87/redux-saga-requests/blob/master/examples/mutations/src/store/actions.js#L61

@klis87
Copy link
Owner

klis87 commented Jan 14, 2020

Regarding id I though doSaveQuestion is for updating. If this is for creation, then update data could just append new question so id is not necessary then

@svindler
Copy link
Author

I'm using mutations with delete, they work as they should, but no luck with post requests. Thanks!

@klis87
Copy link
Owner

klis87 commented Jan 14, 2020

Logic should be exactly the same, if u still have issues pls make minimal reproducing repo so I could take a look

@klis87 klis87 mentioned this issue Jan 17, 2020
44 tasks
@klis87
Copy link
Owner

klis87 commented Mar 22, 2020

@svindler let me know if sth does not work, it really should, especially in v0.27.0

@klis87 klis87 closed this as completed Mar 22, 2020
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

2 participants