cpt_3n0x
cpt_3n0x

Reputation: 55

Redux - Returns call function informations to update props / state

I am an intermediate developer on React and I would like to have advice on the best practice to update props with SET_STATE in reducer.

This is my file nomenclature :

redux/
├── events/
│   ├── reducer.js
│   ├── actions.js
│   ├── sagas.js
├── services/
│   ├── events.js

When I dispatch an action in sagas.js, the action function calls another function in events.js that calls the API. I would like to update the props top revent reload of my web page afetr each modification.

Now when I call a function in events.js with yield call(...) [in sagas.js], I always receive undifined while I return the API response data.

I would like to receive this information to call my SET_STATE action in my reducer to update it without relaod

This is my files :

reducer.js

import actions from './actions'

const initialState = {
  uniqueEvent: {},
  logistic: []
}

export default function eventsReducer(state = initialState, action) {
  switch (action.type) {
    case actions.SET_STATE:
    {
      return { ...state, ...action.payload }
    }
    default:
      return state
  }
}

actions.js

const actions = {
  SET_STATE: 'events/SET_STATE',
  UPDATE_EVENT: 'events/UPDATE_EVENT',
  ADD_EVENT_LOGISTIC: 'events/ADD_EVENT_LOGISTIC',
}

export default actions

sagas.js

import { all, takeEvery, put, call } from 'redux-saga/effects'
import { pdateEvent, addEventLogistic } from 'services/events'
import actions from './actions'


export function* UPDATE_EVENT({ payload: { event } }) {
  console.log(JSON.stringify(`UPDATE_EVENT`))

  const upEvent = yield call(updateEvent, event)
  if(upEvent){
    yield put({
        type: 'events/SET_STATE',
        payload: {
          uniqueEvent: upEvent,
        },
      })
  } else {
     console.log(JSON.stringify('REDUX UPDATE_EVENT NOT DONE'))
   }
}

export function* ADD_EVENT_LOGISTIC({ payload: { activist, information, eventId } }) {
  console.log(JSON.stringify(`ADD_EVENT_LOGISTIC`))

  const logisticToAdd = {user: activist, description: information, event: eventId}

  const eventLogistic = yield call(addEventLogistic, logisticToAdd)
  if (eventLogistic) {
    yield put({
        type: 'events/SET_STATE',
        payload: {
          logistic: eventLogistic,
        },
      })
  } else {
    console.log(JSON.stringify('REDUX EVENT LOGISTIC NOT DONE'))
  }
}

export default function* rootSaga() {
  yield all([
    takeEvery(actions.UPDATE_EVENT, UPDATE_EVENT),
    takeEvery(actions.ADD_EVENT_LOGISTIC,ADD_EVENT_LOGISTIC),
  ])
}

And my events.js (api call):

import { notification } from 'antd'

const axios = require('axios')

export function updateEvent(value) {
  axios.put(`http://api.xxxx.xx/xx/xxxx/events/event/${value.id}/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
        message: error.code,
        description: error.message,
      })
    })
}

export function addEventLogistic(value) {
  axios.post(`http://api.xxxx.xx/xx/xxxx/events/logistic/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
         message: error.code,
         description: error.message,
      })
    })
}

export default async function defaulFunction() {
  return true
}

I would like the addEventLogstic() function in events.js return the JSON from the request (I did some tests and I get it well) but in sagas.js the variable declared with yield call(...) does not get any value (whereas I would like the JSON of the API).

Do you have any thoughts? Thank you very much for your precious help

Upvotes: 1

Views: 66

Answers (1)

HMR
HMR

Reputation: 39350

I suspect it's your updateEvent and addEventLogistic function not returning anything. Add a return statement so they do:

export function updateEvent(value) {
  //added return statement here
  return axios.put(`http://api.xxxx.xx/xx/xxxx/events/event/${value.id}/`, value, {
    headers: {
      Authorization: `Token ${localStorage.getItem('auth_token')}`,
    },
  })
    .then(response => {
      return response.data
    })
    .catch(function(error) {
      notification.error({
        message: error.code,
        description: error.message,
      })
    })
}

Upvotes: 2

Related Questions