const fetchActions = {
  get: function ({ file = "", data = {} }) {
    return new Promise((resolve, reject) => {
      (async () => {
        try {
          $(".loader").show();
          let params = [];
          for (let key in data) {
            params.push(`${key}=${encodeURIComponent(data[key])}`);
          }
          let init = {
              method: "GET",
              headers: {
                "Content-Type": "application/json",
              },
            },
            response = await fetch(
              `main/code/${file}.php?${params.join("&")}`,
              init
            );

          if (response.ok) {
            $(".loader").hide();
            let json = await response.json();
            resolve(json);
          } else {
            reject(response.statusText);
          }
        } catch (err) {
          reject(err.message);
        }
      })();
    });
  },
  post: function ({ file = "", data = {} }) {
    return new Promise((resolve, reject) => {
      (async () => {
        try {
          $(".loader").show();
          let init = {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify(data),
            },
            response = await fetch(`main/code/${file}.php`, init);
          if (response.ok) {
            $(".loader").hide();
            let json = await response.json();
            resolve(json);
          } else {
            reject(response.statusText);
          }
        } catch (err) {
          reject(err.message);
        }
      })();
    });
  },
};
