All files / src/stock-locations stock-locations-table.resource.ts

52.38% Statements 11/21
0% Branches 0/6
33.33% Functions 3/9
55% Lines 11/20

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                1x   1x 1x   1x   1x   1x 1x                                                 1x 1x                         1x                               1x                                            
import { useMemo, useState } from 'react';
import useSWR from 'swr';
import { type FetchResponse, openmrsFetch, restBaseUrl, usePagination } from '@openmrs/esm-framework';
import { useTranslation } from 'react-i18next';
import { type StockOperationFilter } from '../stock-operations/stock-operations.resource';
import { useStockTagLocations } from '../stock-lookups/stock-lookups.resource';
 
export function useStockLocationPages(filter: StockOperationFilter) {
  const { stockLocations, error, isLoading, mutate } = useStockTagLocations();
 
  const pageSizes = [10, 20, 30, 40, 50];
  const [currentPageSize, setPageSize] = useState(10);
 
  const { goTo, results: paginatedQueueEntries, currentPage } = usePagination(stockLocations, currentPageSize);
 
  const { t } = useTranslation();
 
  const tableHeaders = useMemo(
    () => [
      {
        id: 0,
        header: t('name', 'Name'),
        key: 'name',
      },
      {
        id: 1,
        header: t('tags', 'Tags'),
        key: 'tags',
      },
      {
        id: 2,
        header: t('childLocations', 'Child Locations'),
        key: 'childLocations',
      },
      {
        id: 3,
        header: t('actions', 'Actions'),
        key: 'actions',
      },
    ],
    [t],
  );
 
  const tableRows = useMemo(() => {
    return stockLocations.map((location) => ({
      id: location?.uuid,
      key: `key-${location?.uuid}`,
      uuid: `${location?.uuid}`,
      name: `${location?.name}`,
      tags:
        location?.meta.tag
          ?.filter((tag) => tag.code !== 'SUBSETTED')
          .map((p) => p.code)
          ?.join(', ') ?? '',
      childLocations: location?.childLocations?.map((p) => p.display)?.join(', ') ?? '',
    }));
  }, [stockLocations]);
  return {
    items: stockLocations,
    mutate,
    currentPage,
    currentPageSize,
    paginatedQueueEntries,
    goTo,
    pageSizes,
    isLoading,
    error,
    setPageSize,
    tableHeaders,
    tableRows,
  };
}
 
export const useLocationTags = () => {
  const url = `${restBaseUrl}/locationtag/`;
 
  const { data, isLoading, mutate } = useSWR<{ data }, Error>(url, openmrsFetch);
  const results = data?.data?.results ? data?.data?.results : [];
  return {
    locationTagList: results,
    loading: isLoading,
    mutate,
  };
};
interface LocationName {
  name: string;
}
export async function saveLocation({ locationPayload }): Promise<FetchResponse<LocationName>> {
  const response: FetchResponse = await openmrsFetch(`${restBaseUrl}/location/`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: locationPayload,
  });
  return response;
}