import { actionPromise, store } from "../../../store/store";

const getGQL = (url, headers = {}) => (query = "", variables = {}) =>
  fetch(url, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify({ query, variables }),
  }).then((res) => res.json());

const actionMessagePromise = () => {
  var promise = getGQL("http://localhost:3330/graphql")(
    `query allMessage{
      getAllMessages {
        id, message, autorId {
          id, login
        }, partnerId {
          id, login
        }
      }
    }`
  );
  return actionPromise("message", promise);
};

const actionMessage = () => {
  return async (dispatch) => {
    var token = await dispatch(actionMessagePromise());
    // console.log(token);
  };
};

export { actionMessage };
