All files / apps/timeOff/src/screens/Preview hooks.ts

89.65% Statements 26/29
50% Branches 3/6
85.71% Functions 12/14
89.28% Lines 25/28

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 112 113 114 115 116                                            1x       1x         2x 2x     2x   2x 2x     2x   2x               2x   2x             1x             1x   1x 1x     1x 1x       1x           1x                 2x   2x   1x 1x       1x 1x                        
import { useMemo } from 'react';
 
import { useMutation } from '@tanstack/react-query';
 
import { REQUEST_POLICY, runRequestEffect } from '@repo/services/effectRequest';
 
import { useHolidayCalendar } from '@/hooks/useHolidayCalendar';
import { useTimeOffEmployeeDirectory } from '@/hooks/useTimeOffEmployees';
 
import { mapSelectedIdsToAvatarItems } from '@/utils/avatar';
import {
  buildSafeReasonText,
  buildSummaryDateText,
  buildSummaryValueText,
} from '@/utils/timeOffSummary';
 
import { TimeOffRequestPayload } from '@/types/timeOff';
 
import { useTimeOffV2Runtime } from '../TimeOffV2/runtime';
 
import { UsePreviewSummaryParams } from './types';
 
const ENDPOINTS = {
  LIST_REQUESTS: '/api/tickets',
};
 
export const usePreviewSummary = ({
  startDate = null,
  endDate = null,
  reason,
}: UsePreviewSummaryParams) => {
  const calendarYear = useMemo(
    () => startDate?.getFullYear() ?? new Date().getFullYear(),
    [startDate],
  );
  const { holidays, makeUpWorkdays } = useHolidayCalendar(calendarYear);
 
  const summaryDateText = useMemo(
    () => buildSummaryDateText(startDate),
    [startDate],
  );
  const summaryValueText = useMemo(
    () =>
      buildSummaryValueText({
        startDate,
        endDate,
        holidays,
        makeUpWorkdays,
      }),
    [endDate, holidays, makeUpWorkdays, startDate],
  );
  const safeReason = useMemo(() => buildSafeReasonText(reason), [reason]);
 
  return {
    summaryDateText,
    summaryValueText,
    safeReason,
  };
};
 
export const usePreviewPeople = ({
  approvers,
  observers,
}: {
  approvers: string[];
  observers: string[];
}) => {
  const { employeesById } = useTimeOffEmployeeDirectory();
 
  const approversPreview = useMemo(
    () => mapSelectedIdsToAvatarItems(approvers, employeesById),
    [approvers, employeesById],
  );
  const observersPreview = useMemo(
    () => mapSelectedIdsToAvatarItems(observers, employeesById),
    [observers, employeesById],
  );
 
  return {
    approversPreview,
    observersPreview,
  };
};
 
export const useCreateTimeOffRequest = (
  onSuccess?: () => void,
  onError?: (error: unknown) => void,
) => {
  const {
    state: { baseApiUrl },
    meta: {
      api: { mainHttp, requestWithAuthGuard },
    },
  } = useTimeOffV2Runtime();
 
  return useMutation({
    mutationFn: (payload: TimeOffRequestPayload) => {
      return runRequestEffect(async () => {
        Iif (!baseApiUrl) {
          throw new Error('baseApiUrl is required to submit time off request');
        }
 
        await requestWithAuthGuard(() =>
          mainHttp.post(ENDPOINTS.LIST_REQUESTS, payload),
        );
      }, REQUEST_POLICY.WRITE);
    },
    onSuccess: () => {
      onSuccess?.();
    },
    onError: error => {
      onError?.(error);
    },
  });
};