All files / SASQViewComponent SASQViewComponent.js

90% Statements 18/20
62.5% Branches 5/8
75% Functions 6/8
89.47% Lines 17/19

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 97 98 99 100 101              26x                 10x               10x 10x 10x   10x 10x   10x           10x     10x   10x 10x                 10x                                 10x             10x   2x                 26x                          
import { forwardRef, useImperativeHandle } from 'react';
import PropTypes from 'prop-types';
 
import { useQuery } from 'react-query';
 
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
 
const SASQViewComponent = forwardRef(
  (
    {
      fetchParameters,
      history,
      id,
      location,
      match,
      path,
      viewQueryPromise = ({ endpoint, ky, resourceId }) => ky.get(`${endpoint}/${resourceId}`).json(),
      viewResponseTransform = (response) => response,
      ViewComponent,
      viewQueryNamespaceGenerator = ({
        namespace,
        componentId,
        id: passedId,
      }) => {
        const queryNamespace = [namespace, 'SASQ'];
        Eif (componentId) {
          queryNamespace.push(componentId);
        }
        queryNamespace.push('view');
        queryNamespace.push(passedId);
 
        return queryNamespace;
      },
      ...props
    },
    ref
  ) => {
    const { 0: namespace } = useNamespace();
 
    // If itemEndpoint is available, use that, otherwise use standard endpoint
    const endpoint = fetchParameters?.itemEndpoint ?? fetchParameters?.endpoint;
 
    const ky = useOkapiKy();
    const { data = {}, ...rest } = useQuery(
      viewQueryNamespaceGenerator({
        componentId: id,
        namespace,
        endpoint,
        id: match?.params?.id,
        match,
      }),
      () => {
        return viewQueryPromise({
          ky,
          resourceId: match?.params?.id,
          endpoint,
        });
      },
      {
        enabled: !!match?.params?.id,
        // select is a parameter supported by useQuery to transform the response
        // Could be possible to instead pass this down along with the queryOptions
        // Additionally this is assuming the viewResponseTransform is a func as opposed to an object
        select: (selectData) => {
          return viewResponseTransform(selectData);
        },
      }
    );
 
    useImperativeHandle(ref, () => ({
      queryProps: {
        data,
        ...rest,
      },
    }));
 
    return (
      <ViewComponent
        onClose={() => history.push(`${path}${location.search}`)}
        queryProps={{ ...rest }}
        resource={data}
        {...props}
      />
    );
  }
);
 
SASQViewComponent.propTypes = {
  fetchParameters: PropTypes.object,
  history: PropTypes.object,
  id: PropTypes.string,
  location: PropTypes.object,
  match: PropTypes.object,
  viewQueryPromise: PropTypes.func,
  viewResponseTransform: PropTypes.func,
  path: PropTypes.string.isRequired,
  ViewComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
};
 
export default SASQViewComponent;