function postsFetchData() {
  const token = localStorage.getItem('token');
  const requestBody = {
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      authorization: token,
    },
  };
  return async (dispatch) => {
    dispatch(postsHasErrored(false));
    // Dispatch Loading Status to Component
    dispatch(postsIsLoading(true));
    // Get Data from API
    await axios.get('https://akin-react-auth-api.herokuapp.com/todos', requestBody)
      .then((response) => {
        dispatch(postsIsLoading(false));
        // Get Reponse
        const apiResponse = response.data;
        // Handle Error Response
        if (apiResponse.status !== 200) {
          return dispatch(postsHasErrored(true));
        }
        const posts = apiResponse.todos;
        return dispatch(postsFetchDataSuccess(posts));
      })
      .catch(() => dispatch(postsHasErrored(true)));
  };
}
// Action to Fetch all Todos from API
Action to Fetch all Todos from API
Action to Fetch all Todos from API
function postsFetchData() {
  const token = localStorage.getItem('token');
  const requestBody = {
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      authorization: token,
    },
  };
  return async (dispatch) => {
    dispatch(postsHasErrored(false));
    dispatch(postsIsLoading(true));
    await axios.get('https://akin-react-auth-api.herokuapp.com/todos', requestBody)
      .then((response) => {
        dispatch(postsIsLoading(false));
        const apiResponse = response.data;
        if (apiResponse.status !== 200) {
          return dispatch(postsHasErrored(true));
        }
        const posts = apiResponse.todos;
        return dispatch(postsFetchDataSuccess(posts));
      })
      .catch(() => dispatch(postsHasErrored(true)));
  };
}
function postsFetchData ( ) { const token = localStorage . getItem ( 'token' ) ; const requestBody = { headers : { Accept : 'application/json' , 'Content-Type' : 'application/json' , authorization : token , } , } ; return async ( dispatch ) => { dispatch ( postsHasErrored ( false ) ) ; dispatch ( postsIsLoading ( true ) ) ; await axios . get ( 'https://akin-react-auth-api.herokuapp.com/todos' , requestBody ) . then ( ( response ) => { dispatch ( postsIsLoading ( false ) ) ; const apiResponse = response . data ; if ( apiResponse . status !== 200 ) { return dispatch ( postsHasErrored ( true ) ) ; } const posts = apiResponse . todos ; return dispatch ( postsFetchDataSuccess ( posts ) ) ; } ) . catch ( ( ) => dispatch ( postsHasErrored ( true ) ) ) ; } ; }
Action to Fetch all Todos from API
Action to Fetch all Todos from API
// Dispatch Loading Status to Component // Get Data from API // Get Reponse // Handle Error Response