function submitNewWorkout(data, history) {
  return function(dispatch) {
    return fetch(`/api/workouts`, {
      method: 'POST',
      headers: {
        'Content-type': 'application/json'
      },
      body: JSON.stringify(data)
    })
      .then(handleErrors)
      .then(resp => {
        return resp.json();
      })
      .then(workout => {
        // Dispatch action that adds JSON response to store
        dispatch({type: 'ADD_WORKOUT', payload: workout});
        history.push('/workouts');
      })
    .catch((error) => {
      // Dispatch error to store if validation fails
      dispatch({type: 'ERROR'})
    })
  }
}
// Submits workout data to Rails API which persists to the db
Submits workout data to Rails API which persists to the db
Submits workout data to Rails API which persists to the db
function submitNewWorkout(data, history) {
  return function(dispatch) {
    return fetch(`/api/workouts`, {
      method: 'POST',
      headers: {
        'Content-type': 'application/json'
      },
      body: JSON.stringify(data)
    })
      .then(handleErrors)
      .then(resp => {
        return resp.json();
      })
      .then(workout => {
        dispatch({type: 'ADD_WORKOUT', payload: workout});
        history.push('/workouts');
      })
    .catch((error) => {
      dispatch({type: 'ERROR'})
    })
  }
}
function submitNewWorkout ( data , history ) { return function ( dispatch ) { return fetch ( ` ` , { method : 'POST' , headers : { 'Content-type' : 'application/json' } , body : JSON . stringify ( data ) } ) . then ( handleErrors ) . then ( resp => { return resp . json ( ) ; } ) . then ( workout => { dispatch ( { type : 'ADD_WORKOUT' , payload : workout } ) ; history . push ( '/workouts' ) ; } ) . catch ( ( error ) => { dispatch ( { type : 'ERROR' } ) } ) } }
Submits workout data to Rails API which persists to the db
Submits workout data to Rails API which persists to the db
// Dispatch action that adds JSON response to store // Dispatch error to store if validation fails