async function fetchFromServer()

in frontend/app/common/FilterableList.tsx [35:87]


  async function fetchFromServer(searchParam: string) {
    const getUrl =
      props.unfilteredContentFetchUrl +
      "?" +
      props.fetchUrlFilterQuery +
      "=" +
      searchParam;
    const credentialsValue = props.allowCredentials ? "include" : "omit";

    let searchDoc = null;
    try {
      if (props.makeSearchDoc) {
        searchDoc = props.makeSearchDoc(searchParam);
        if (searchDoc == null) {
          setContentFromServer([]);
          return;
        }
      }
    } catch (e) {
      console.log("Could not build search doc: ", e);
      setContentFromServer([]);
      return;
    }

    const result = await (searchDoc
      ? authenticatedFetch(props.unfilteredContentFetchUrl, {
          method: "PUT",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(searchDoc),
          credentials: credentialsValue,
        })
      : authenticatedFetch(getUrl, {
          method: "GET",
          credentials: credentialsValue,
        }));

    const content = await result.json();

    try {
      if (!result.ok) {
        console.error(`FilterableList got a server ${result.status} error`);
        setContentFromServer([]);
        return;
      }

      const convertedContent = props.unfilteredContentConverter
        ? props.unfilteredContentConverter(content)
        : defaultContentConverter(content);
      setContentFromServer(convertedContent);
    } catch (err) {
      console.error("Could not convert content: ", err);
    }
  }