import type { Article } from "../domain/articleEntity";
import { getArticle as getArticleAPI } from "./articleAPIService";
import * as actionTypes from "./articleActionTypes";
import { dispatchStatus } from "../../store/index";

const setArticleAction = (article: Article) => (dispatch: any) =>
  dispatch({ type: actionTypes.SET_ARTICLE, article });

const getArticleAction = (id: string) => (dispatch: any) => {
  dispatch({ type: actionTypes.GET_ARTICLE });

  return getArticleAPI(id)
    .then((article) => {
      dispatchStatus(actionTypes.GET_ARTICLE, ".success", article)(dispatch);
      return article;
    })
    .catch((reason) => {
      dispatchStatus(actionTypes.GET_ARTICLE, ".failure", reason)(dispatch);
      return reason;
    });
};

export { setArticleAction, getArticleAction };
