Sined4ik
Sined4ik

Reputation: 119

call dispatch func inside dispatch func react redux

I recently started working with redux (I worked with mobx before) and this causes me some problems, I try to call refresh after a successful response, but it is not called, where did I go wrong? (the refresh function makes a request to the backend and updates the date), (the editSupply function sends the changed data)

export function refresh() {
  return (dispatch, getState) => {
    const state = getState()

    const storeId = state.profile.currentStore.id
    dispatch(actions.setLoading(true))
    return apiGet(stockApi, buildRefreshParameters(storeId, state.stock))
      .onOk((res) => {
        dispatch(
          actions.setData(
            res.data.response.supplies.map((s) => ({
              ...s,
              id: s.stockId,
              salePrice: s.salePrice,
              remainder: s.remainder,
              purchases: s.purchases.map((p) => ({
                ...p,
                purchasePrice: p.price,
              })),
            })),
          ),
        )
        dispatch(actions.setTotalPages(res.data.response.pages))
      })
      .useDefaultErrorHandlers(dispatch)
      .afterAll(() => {
        dispatch(actions.setLoading(false))
      })
      .startSingle()
  }
}
export function editSupply(editedData) {
  return (dispatch, {}) => {
    forEachObjIndexed((data, id) => {
      apiPut(stockApi + "/" + id, data)
        .onOk(() => {
          dispatch(
            showMessage("Товар успешно отредактирован", 
            alertTypes.success),
          )
          refresh()
        })
        .useDefaultErrorHandlers(dispatch)
        .start()
    }, editedData)
  }

}

Upvotes: 1

Views: 48

Answers (1)

Mezbaul
Mezbaul

Reputation: 1272

call refresh() like this:

dispatch(refresh())

When you call another action from within an action, you wrap them with dispatch(). Your refresh() looks like another action as it accepts dispatch and getState as arguments.

Upvotes: 2

Related Questions