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

Where to pass onSuccess, onComplete, etc? #78

Open
djalmaaraujo opened this issue Feb 8, 2018 · 5 comments
Open

Where to pass onSuccess, onComplete, etc? #78

djalmaaraujo opened this issue Feb 8, 2018 · 5 comments

Comments

@djalmaaraujo
Copy link

I am trying to have callbacks, but it's not working in the object:

    type: ORDER_SUBMIT,
    onSuccess({ getState, dispatch}){},
    payload: {
      request: {
        method: 'put',
        url: `/tickets/${orderData.orderTicketCode}`,
        data: {}
      },
      onSuccess({ getState, dispatch }) {
 
      },
      meta: {
        onSuccess({ getState, dispatch}) {
        }
      }
    }

Where is the correct location?

@azza85
Copy link

azza85 commented Feb 9, 2018

@djalmaaraujo I came here to ask the same question. I can see an issue here which has a little information about it. #7

I think one of the below ways might be what you are after.

Just append _SUCCESS or _FAIL to your action eg ORDER_SUBMIT_SUCCESS or ORDER_SUBMIT_FAIL (these are built in to the module)

I have a saveComment function which is triggered when the user click save.

export function saveComment (formData) {
  return {
    type: SAVING_COMMENT,
    payload: {
      request: {
        url: API.saveComment(),
        method: 'POST',
        headers: API.getPostHeaders(),
        data: formData
      }
    }
  }
}

I then have

    case SAVING_COMMENT:
      return {
        ...state,
        sendingComment: true
      }
    case SAVING_COMMENT_SUCCESS:
      return {
        ...state,
        commentText: '',
        sendingComment: false,
        saveSuccessfull: true
      }

Or If you are using dispatch (see the res output below)

export function saveCommentUpdateFeed (saveData, listFilters) {
  return function (dispatch) {
    Promise.all([
      dispatch(saveComment (saveData))
      .then(res => console.log(res)) //you can add something here on success
    ])
   .then(() => {
      setTimeout(() => {
        dispatch(getComments(listFilters)) // this updates comment list with saved comment from above
      }, 500)
    })
  }
}

res output

{
  "type": "SAVING_COMMENT_SUCCESS",
  "payload": {
    "data": {},
    "status": 201,
    "statusText": "CREATED",
    "headers": {
      "content-type": "application/json"
    },
    "config": {
      "transformRequest": {},
      "transformResponse": {},
      "timeout": 0,
      "xsrfCookieName": "XSRF-TOKEN",
      "xsrfHeaderName": "X-XSRF-TOKEN",
      "maxContentLength": -1,
      "headers": {}
    },
    "request": {}
  },
  "meta": {
    "previousAction": {
      "type": "SAVING_COMMENT",
      "payload": {
        "request": {
          "url": "/savingcomment/",
          "method": "POST",
          "headers": {
            "Content-Type": "application/json",
            "Accept": "application/json"
          },
          "data": {}
        }
      }
    }
  }
}

@djalmaaraujo
Copy link
Author

Thanks @azza85. I still feel odd that we need a setting to have the .catch() to work. Why this is not the default?

@marcusmolchany
Copy link

you'll want to do something like this:

    type: ORDER_SUBMIT,
    payload: {
      request: {
        method: 'put',
        url: `/tickets/${orderData.orderTicketCode}`,
        data: {}
      },
      options: {
        onSuccess({ getState, dispatch, response }) {
           dispatch({ type: `${ORDER_SUBMIT}_SUCCESS`, payload: response.data });
        },
        onError({ getState, dispatch, error }) {
           dispatch({ type: `${ORDER_SUBMIT}_FAILURE`, payload: error.response });
        },
      }
    }

@karthik446
Copy link

@marcusmolchany Thank you. This should've been in the documentation.

@MichaelVessia
Copy link

manually dispatching a new action in onSuccess with a payload field caused an infinite loop of dispatches since my middleware would pick it up as a new request to be intercepted. changing payload to something else like data avoids this.

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

5 participants