javascriptjirafetch-apiforge

Trying to call an external endpoint inside a jira issue panel app


In my code, when I try to call an external endpoint from the Jira issue panel app, I get a log "try" and the fetch doesn't happen.

I would appreciate it, if someone could help me.

import ForgeUI, {
  IssuePanel,
  useProductContext,
  useEffect,
} from "@forge/ui";
import { url } from "./constants";
import API, { fetch } from "@forge/api";

const Panel = () => {
  const productContext = useProductContext();
  const fetchTaskItems = async (issueID) => {
    const requestOptions = {
      method: "GET",
      mode: "cors",
      headers: { "Content-Type": "application/json" },
    };

    try {
      console.log("try");
      const res = await fetch(
        url + `experience/task/item/${issueID}`,
        requestOptions
      );
      console.log(res);
    } catch (err) {
      console.log(err.message);
    }
  };

  useEffect(() => {
    const currentIssueId = productContext.platformContext.issueKey;
    console.log("Current issue ID:", currentIssueId);
    fetchTaskItems(currentIssueId);
  }, []);


Solution

  • You can import userAction from ForgeUI

    import ForgeUI, {useAction} from "@forge/ui";
    

    Then call your fetchTaskItems like this

    const [req] = useAction(
      (value) => value,
      async () => await fetchTaskItems()
    );
    

    let me know if it works,