import promise from "redux-promise-middleware";

const store = createStore(
  reducer,
  composeWithDevTools(applyMiddleware(thunk, promise))
);

// redux-promise-middleware
const GET_USERS = "redux/users/GET_USERS";

export const GET_USERS_PENDING = "redux/users/GET_USERS_PENDING";
export const GET_USERS_FULFILLED = "redux/users/GET_USERS_FULFILLED";
export const GET_USERS_REJECTED = "redux/users/GET_USERS_REJECTED";

export default function reducer(state = initialState, action) {
  if (action.type === GET_USERS_START || action.type === GET_USERS_PENDING) {
    return {
      ...state,
      loading: true,
      error: null,
    };
  }

  if (action.type === GET_USERS_FULFILLED) {
    return {
      ...state,
      loading: false,
      data: action.payload,
    };
  }

  if (action.type === GET_USERS_REJECTED) {
    return {
      ...state,
      loading: false,
      error: action.payload,
    };
  }
  return state;
}

// redux-promise-middleware
export function getUsersPromise() {
  return {
    type: GET_USERS,
    payload: async () => {
      const res = await axios.get("<https://api.github.com/users>");
      return res.data;
    },
  };
}

getUsersPromise() 함수를 필요한 Container 에서 호출.