All files / react-app/src/api useGetItem.ts

72% Statements 18/25
100% Branches 0/0
62.5% Functions 5/8
72% Lines 18/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53        73x 1416x   73x 679x 679x 345x   344x 344x       73x 510x 510x 267x   249x 249x       73x       38x   18x         73x                            
import { useQuery, useQueryClient, UseQueryOptions } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { opensearch, ReactQueryApiError, SEATOOL_STATUS } from "shared-types";
 
export const getItem = async (id: string): Promise<opensearch.main.ItemResult> =>
  await API.post("os", "/item", { body: { id } });
 
export const idIsApproved = async (id: string) => {
  try {
    const record = await getItem(id);
    return record._source.seatoolStatus == SEATOOL_STATUS.APPROVED;
  } catch (e) {
    console.error(e);
    return false;
  }
};
 
export const canBeRenewedOrAmended = async (id: string) => {
  try {
    const record = await getItem(id);
    return ["New", "Renew"].includes(record._source.actionType);
  } catch (e) {
    console.error(e);
    return false;
  }
};
 
export const useGetItem = (
  id: string,
  options?: UseQueryOptions<opensearch.main.ItemResult, ReactQueryApiError>,
) => {
  return useQuery<opensearch.main.ItemResult, ReactQueryApiError>(
    ["record", id],
    () => getItem(id),
    options,
  );
};
 
export const useGetItemCache = (id: string | undefined) => {
  const queryClient = useQueryClient();
  const data = (() => {
    const data = queryClient.getQueryCache().find(["record", id])?.state
      .data as opensearch.main.ItemResult;
    return data?._source;
  })();
 
  const refetch = () => {
    queryClient.refetchQueries(["record", id]);
  };
 
  return { data, refetch };
};