{"size": 2529, "ext": "js", "max_stars_repo_path": "06-Adding-Authentication-Login-With-Hooks-Support/_experimental/M5-End-Of_Module/src/useAxiosFetch.js", "max_stars_repo_name": "vamshi467/ReactHooks", "max_stars_repo_stars_event_min_datetime": "2020-10-23T05:12:55.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-09T10:29:55.000Z", "max_issues_repo_path": "06-Adding-Authentication-Login-With-Hooks-Support/_experimental/M5-End-Of_Module/src/useAxiosFetch.js", "max_issues_repo_name": "vamshi467/ReactHooks", "max_issues_repo_issues_event_min_datetime": "2020-07-19T04:24:58.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-09T01:20:32.000Z", "max_forks_repo_path": "06-Adding-Authentication-Login-With-Hooks-Support/_experimental/M5-End-Of_Module/src/useAxiosFetch.js", "max_forks_repo_name": "vamshi467/ReactHooks", "max_forks_repo_forks_event_min_datetime": "2020-07-07T08:23:09.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-14T11:52:22.000Z"}
{"max_stars_count": 7, "max_issues_count": 8, "max_forks_count": 3, "avg_line_length": 23.4166666667, "max_line_length": 91, "alphanum_fraction": 0.5508105971}
import React, { useState, useEffect, useReducer } from "react";
import axios from "axios";

const dataFetchReducer = (state, action) => {
  switch (action.type) {
    case "FETCH_INIT":
      return { ...state, isLoading: true, isError: false };
    case "FETCH_SUCCESS":
      return {
        ...state,
        isLoading: false,
        hasErrored: false,
        errorMessage: "",
        data: action.payload
      };
    case "FETCH_FAILURE":
      return {
        ...state,
        isLoading: false,
        hasErrored: true,
        errorMessage: "Data Retrieve Failure"
      };
    case "REPLACE_DATA":
      const newData = state.data.map(rec => {
        return rec.id === action.replacerecord.id ? action.replacerecord : rec;
      });
      return {
        ...state,
        isLoading: false,
        hasErrored: false,
        errorMessage: "",
        data: newData
      };
    default:
      throw new Error();
  }
};

const useAxiosFetch = (initialUrl, initialData) => {
  const [url] = useState(initialUrl);

  const [state, dispatch] = useReducer(dataFetchReducer, {
    isLoading: false,
    hasErrored: false,
    errorMessage: "",
    data: initialData
  });

  useEffect(() => {
    let didCancel = false;

    // axios.interceptors.response.use(function (response) {
    //   // Do something with response data
    //   debgger;
    //   return response;
    // }, function (error) {
    //   // Do something with response error
    //   debugger;
    //   return Promise.reject(error);
    // });

    const fetchData = async () => {
      dispatch({ type: "FETCH_INIT" });

      try {
        let result = null;
        result = await axios.get(url);

        if (!didCancel) {
          dispatch({ type: "FETCH_SUCCESS", payload: result.data });
        }
      } catch (err) {
        // tnis err object does not work well, see interceptor above but no luck with that.
        if (!didCancel) {
          dispatch({ type: "FETCH_FAILURE" });
        }
      }
    };

    fetchData();

    return () => {
      didCancel = true;
    };
  }, [url]);

  const updateDataRecord = record => {
    debugger;
    // const newData = state.data.map(rec => {
    //   return rec.id === speakerRec.id ? speakerRec : rec;
    // });
    // dispatch({
    //   type: "REPLACE_DATA",
    //   newdata: newData
    // });

    // record must have "id"
    dispatch({
      type: "REPLACE_DATA",
      replacerecord: record
    })
  };

  return { ...state, updateDataRecord };
};

export default useAxiosFetch;
