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

100% Statements 17/17
100% Branches 0/0
100% Functions 5/5
100% Lines 17/17

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        79x 1407x   79x 679x 679x 345x   344x 344x       79x 510x 510x 267x   249x 249x       79x       19x   10x        
import { useQuery, 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,
  );
};