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

90.32% Statements 28/31
50% Branches 3/6
86.66% Functions 13/15
90% Lines 27/30

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                              1x       1x         2x 2x     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,
  calculateQuickSelectDaysOff,
} from '@/utils/timeOffSummary';
 
import { TimeOffRequestPayload } from '@/types/timeOff';
 
import { useTimeOffRuntime } from '../TimeOff/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]);
 
  const daysOff = useMemo(
    () =>
      calculateQuickSelectDaysOff(
        startDate,
        endDate,
        undefined,
        holidays,
        makeUpWorkdays,
      ),
    [endDate, holidays, makeUpWorkdays, startDate],
  );
 
  return {
    summaryDateText,
    summaryValueText,
    safeReason,
    daysOff,
  };
};
 
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 },
    },
  } = useTimeOffRuntime();
 
  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);
    },
  });
};