All files / apps/timeOff/src/screens/ChooseApproversAndObservers/hooks useApproverObserverData.ts

90% Statements 18/20
90% Branches 18/20
66.66% Functions 4/6
100% Lines 18/18

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 102 103 104 105 106 107 108 109 110 111                              1x 1x   1x   4x   4x       4x               4x                   4x       4x                   4x   4x 4x         4x 4x 4x       4x   4x                                   4x                                    
import { useMemo } from 'react';
 
import { RequestFor, useGetMyRequestsShared } from '@repo/hooks/useUserQueries';
 
import {
  useGetOnBehalfUserTimeOffRequest,
  useTimeOffEmployeeOptions,
  useTimeOffEmployeeRelationshipSuggestions,
  useTimeOffExtraUsers,
} from '@/hooks/useTimeOffEmployees';
 
import { useTimeOffStateSelector } from '@/screens/TimeOff/context';
 
import { resolveDefaultParticipantIds } from '../utils';
 
const REQUEST_CATEGORY = 'time-off';
const REQUEST_LIMIT = 10;
 
export const useApproverObserverData = () => {
  const { employeeOptions, meEmail, isLoading, isError, isManager } =
    useTimeOffEmployeeOptions();
 
  const { isOnBehalf } = useTimeOffStateSelector(state => ({
    isOnBehalf: state.isOnBehalf,
  }));
 
  const { form } = useTimeOffStateSelector(state => ({
    form: state.form,
  }));
 
  const {
    data: myTimeOffRequests,
    isLoading: isLoadingMyTimeOffRequests,
    isError: isErrorMyTimeOffRequests,
  } = useGetMyRequestsShared({
    category: REQUEST_CATEGORY,
    limit: REQUEST_LIMIT,
    requestFor: isOnBehalf ? RequestFor.ON_BEHALF : RequestFor.ME,
  });
 
  const {
    onBeHalfTimeOffRequests,
    isLoadingOnBehalfUserTimeOffRequest,
    isGetOnBehalfUserTimeOffRequestError,
  } = useGetOnBehalfUserTimeOffRequest(form.requester, isOnBehalf);
 
  // Fetch relationships independently from request history.
  const { suggestedIds: relationshipSuggestedIds } =
    useTimeOffEmployeeRelationshipSuggestions({
      email: meEmail,
      enabled: Boolean(meEmail),
    });
 
  // Fetch independently to keep on-behalf search responsive when opened.
  const {
    data: extraUserOptions,
    isLoading: isLoadingExtraUsers,
    isError: isErrorExtraUsers,
  } = useTimeOffExtraUsers({ disabled: !isOnBehalf });
 
  const requestItems = useMemo(
    () => myTimeOffRequests?.items ?? [],
    [myTimeOffRequests?.items],
  );
 
  const hasLoadedMyTimeOffRequests =
    !isLoadingMyTimeOffRequests && !isErrorMyTimeOffRequests;
  const isFirstRequest = useMemo(
    () => hasLoadedMyTimeOffRequests && requestItems.length === 0,
    [hasLoadedMyTimeOffRequests, requestItems.length],
  );
 
  const { defaultApprovers, defaultObservers } = useMemo(
    () =>
      resolveDefaultParticipantIds({
        isFirstRequest: isFirstRequest && !isOnBehalf,
        relationshipSuggestedIds,
        items: isOnBehalf
          ? (onBeHalfTimeOffRequests?.items ?? [])
          : requestItems,
        meEmail,
      }),
    [
      isFirstRequest,
      isOnBehalf,
      meEmail,
      onBeHalfTimeOffRequests?.items,
      relationshipSuggestedIds,
      requestItems,
    ],
  );
 
  return {
    employeeOptions,
    extraUserOptions,
    defaultApprovers,
    defaultObservers,
    isManager,
    isLoading:
      isLoading ||
      isLoadingMyTimeOffRequests ||
      isLoadingOnBehalfUserTimeOffRequest,
    isError:
      isError ||
      isErrorMyTimeOffRequests ||
      isGetOnBehalfUserTimeOffRequestError,
    isLoadingExtraUsers,
    isErrorExtraUsers,
  };
};